Building a Golang Discord Bot- Part 2: Sticky Message Bot
Learn Golang with a real-world project
In the previous part of this series, we discussed the following:
- Setting up Golang on your machine,
- Setting up your project
- Installing required libraries (including Discord Bot wrapper DiscordGo)
- Setting up our new bot session, and finally
- Reading and respond to simple messages through our bot
If you haven’t yet read it, then do so from here:
Here in this tutorial, we will learn and implement the following concepts:
- How to send a message to a particular channel, and check if a message was sent in a particular channel
- Retrieve and save messages in a text file through the
oslibrary and handle errors appropriately - Send and delete specific messages upon specific events, and finally,
- Combine all of the above features to build a Sticky bot for a channel in our server
Hyped enough? Let’s get started! :)
What is a Sticky Message?
Consider this message from our bot at the bottom of a channel in our server.
Now, let’s write a new message and send it:

This is what happens when a sticky bot is enabled in the channel:

The following two things happens when we send a message:
- The bot deletes the previous “sticky” message
- The bot sends the new message in the channel, thus making sure that the message is always at the bottom of the channel, where it is always visible to any newcomer.
This is, in essence, the convenience of a sticky bot.
Let’s try sending another message:

Here is what happens after we send the above message:

Neat, isn’t it? Now that we understand the purpose and usage of the bot, let’s get started on building it!
Components of a Sticky bot
When the bot sends a new message and erases the previous one send by it to avoid duplication, there is obviously a need for tracking the previous messages sent by it.
Let’s create a global variable to do the same.
var persistentMessageID stringSince we only want our bot to act “sticky” in a specific channel, we first fetch the channel ID from our discord server and use it in our messageCreate function. We open a new block in the function like so:
if m.ChannelID == "yourChannelId" {Now, before we start writing this if block, we need to make a new file in the project directory called: lastTextSentDetails.txt. This will be the file we’ll use to store the last sent sticky messageID which will then persist across different bot sessions.
Hence, even if we stop the bot, the file will contain the last sent message’s ID which can then be used to delete the previous sticky message when the bot sends the next one.
For the very first attempt, let’s take the last sent sticky message’s ID and paste it into the text file. Something like this:

Now, let’s complete our if block.
Reading from and Writing to files in Go
We first want to read the last message ID from the file and delete that message.
Let’s add that.
if m.ChannelID == "yourChannelId" {
if persistentMessageID == "" {
// read the last message id from the file and then delete it
data, err_readfile := os.ReadFile("lastTextSentDetails.txt")
if err_readfile != nil {
fmt.Println("Error in reading file: ", err_readfile)
} else {
persistentMessageID = string(data)
}
}So if the global variable persistentMessageID is an empty string, read from the file and store it in the variable.
Now, we delete the message.
// delete the last message
err := s.ChannelMessageDelete("yourChannelId", persistentMessageID)
if err != nil {
fmt.Println("Erorr in deleting the previous sticky message: ", err)
}Perfect! We have the deletion part done. Now, let’s get to the writing and sending part.
We first send a new sticky message to the channel:
// send a new message to the bottom
messsage, err := s.ChannelMessageSend(m.ChannelID, "Here is a nice little sticky message.")
// if message wasn't sent due to some reason
if err != nil {
fmt.Println("Erorr in sending sticky message: ", err)
}Next, we store the ID of the new message sent in the global persistent variable:
// store the id of the new message sent in the persistent variable
persistentMessageID = messsage.IDAnd finally, we write this new message’s ID to the file:
// write persistentMessageID string to file
err = os.WriteFile("lastTextSentDetails.txt", []byte(persistentMessageID), 0644)
if err != nil {
fmt.Println("Error writing to file: ", err)
}In the
os.WriteFile()function, you must notice that the 0644 code is present to ensure that we overwrite the string to file and not append it. That’s the key difference. For appending, we need to first open the file using theOpenFile()function and then use the flagos.O_APPENDwith it to append to an existing file.
And there you go! We have a working Sticky Bot all ready to go!
Let’s test it, shall we?
First stop the bot and restart it. See that the ID in the lastTextSentDetails.txt file changes.
Now, send a new message:

turns into:

As we see, our bot is working as desired.
A few parting words…
In this tutorial, we learned to create a Sticky Bot to help “pin” a message to the bottom of our channel. We learned quite a few concepts such as reading and writing to files with a bit of error handling on the side as well as a bit more into the inner workings of the discordgo library.
If you want to explore a bit more yourself, you can read about reading and writing files in Golang from here.
If you want to check out the current bot code, you can check out the repository from here. :)
And as always, if you enjoyed this tutorial, subscribe to read all of my articles from here:
A few more articles from me that you may find interesting:
