Main File
So far, what we’ve had as a project looks like this
- go.mod
- go.sum
Not so much going on huh?
main.go
Add a new file called main.go, this is our main entrypoint for our bot.
Here’s a code snippet you can use to get started:
| |
That’s a lot of code, but don’t worry if you don’t understand everything right now. We’ll break it down
First we create a new DisGo client using our bot token
| |
This creates a new DisGo client that will provide all the functionality we need to interact with the Discord API.
Next we set some options for the gateway connection, including which intents we want to enable.
| |
Here we specify that we want to receive events related to guilds (servers), guild messages, and direct messages.
We’ve also set up an event listener for the Ready event, which is triggered when the bot successfully connects to Discord.
After creating the client, we set up a deferred function to ensure that the client is properly closed when the application exits:
| |
We then try to connect to the gateway
| |
And if everything works correctly, our bot is now ready to run and connect to Discord!
Running your application
If you’ve been following along, your project structure should now look like this
- go.mod
- go.sum
- main.go
Head to the terminal and run the following command inside your project directory to run your bot
go run main.goIf everything is set up correctly, you should see a message indicating that your bot is connected with the username you’ve set for it.
Bot is Connected as YourBotNameCongratulations! You’ve successfully set up the main file for your DisGo bot and connected it to Discord.
Now we have a bot, but it doesn’t do much yet. In the next section, we’ll start adding some functionality to our bot!