AI Technology Meets Social Media: The Discord Bot Project

Search for a command to run...

No comments yet. Be the first to comment.
In Go, variables are passed by value, which means that when we pass variables as arguments to functions, Go makes a copy of those values for the function to use. However, the behaviour of Slice can be a bit confusing at first glance, especially if we...

The Go net/http package provides a robust set of functionality to allow us to deal with HTTP requests and responses. However, reading the response body is a common task that requires a bit of caution. In this blog, we will explore the pitfall I exper...

Today we are going to write our own custom json.Unmarshal() method. I find it very useful and can make our code more maintainable and readable if we can make good use of type alias and the json.Unmarshal method. Let's say we want to decode a JSON-enc...

Have you ever tried to maintain services between environments and got lost in all the variables? Or even broken things by accident? Today I'm going to give you some tips on how to speed up your workflow and make things easier. My focus will be mainly...

As someone who's been using ChatGPT for a while now, I've been exploring how to maximise its benefits and integrate it into my daily life. I've been working on a side project to develop a Discord bot that interacts with ChatGPT, similar to the one provided by OpenAI. At first, it was just an attempt to learn more about ChatGPT and bot development, but as I progressed I realised that other people might find it useful as well. It's fascinating to see how intelligent AI technology is finding its way into everyday platforms like Discord.

DiscordGo - provides low-level bindings to the Discord chat client API.
Go OpenAI - provides Go clients for the OpenAI API.
Above are two main packages (dicordgo and go-openai) use to build this ChatGPT Discord Bot. They have great examples of different kinds of use cases that we can build our own logic around.
Due to the character limit on Discord (2000 characters per message at the time of writing), it may be necessary for longer replies from ChatGPT to be split up and sent as multiple messages. It's important to note that when messages are split into multiple parts, the character may be truncated if we don't count the number of characters correctly, especially if we're dealing with languages like Mandarin or Japanese where one character can be multiple bytes. Therefore it's recommended to use the Unicode character count instead of simply counting the number of bytes like len(). This can be done using the utf8.RuneCountInString() function instead. By using this method we can ensure that each part of the message contains complete characters and avoid confusing or misleading messages. It's important to be aware of these language-specific nuances when working with text messages in Golang or any other programming language.
To find out more about managing Strings, bytes, runes, and characters in Go, please visit the Go Blog.
func SendMessageByChunk(message string, chunkLength int, send chan<- string) {
counter := 0
lastIndex := 0
for i, w := 0, 0; i < len(message); i += w {
counter++
_, width := utf8.DecodeRuneInString(message[i:])
w = width
// We reach the maximum chunk length
if counter == chunkLength {
chunk := message[lastIndex : i+w]
send <- chunk
lastIndex = i + w
counter = 0
}
}
// Make sure the last bit of the message is sent
if len(message) > 0 && lastIndex < len(message) {
send <- message[lastIndex:]
}
close(send)
}
In the above example, we create a function to send messages of a certain length (chunkLength). To handle Unicode characters correctly, we can use utf8.DecodeRuneInString() to decode the first UTF-8 encoded Unicode code point in the given string and get the number of bytes it occupies. We iterate through the string until all the characters (runes) are decoded.
Another limitation that may arise is Discord's application command timeout, which at the time of writing is set at 3 seconds. This means that if ChatGPT takes longer than 3 seconds to respond, users will receive an error message indicating that the application has not responded. As ChatGPT can take longer than that to respond, the bot needs to delay the response by setting the interaction callback type DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE to acknowledge that the event has been received and that a follow-up will come later. I also think it's good practice for the bot to let users know that their message has been received and is being processed. By implementing these strategies, ChatGPT can provide a better user experience on Discord.
The following code snippet from this project shows how we can defer or delay replies via DiscordGo.
// If we don't send a response in 3 seconds, the error 'The application did not respond' will appear.
// To avoid this, we send the following type of response
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
})
I've been a frequent user of ChatGPT ever since it was launched by OpenAI. Their Chat App has proven to be very useful in speeding up my daily tasks and even occasionally planning my travel itinerary. It has been really powerful and I no longer have to go through the hassle of searching endless websites and connecting the dots myself. Although the app's interface looks nice, with real-time updates, the connection can be unreliable at times. Another drawback I noticed is that users have to log in before they can use the app's features, which may seem like a minor inconvenience but could be a deal-breaker for those who value ease of use.
We did not cover all the details of building this project in this article but feel free to visit the GitHub page to play around with it π
The best way of learning about anything is by doing. - Richard Branson
If you're interested in running the bot for your Discord server or contributing to the development of the project, please visit the GitHub page. Welcome any suggestions or ideas for improving the code πͺπ½
Hopefully, you found something useful.
Thank you for reading.