← Impulso Blog

My adventures building Discord bots with Golang: Challenges and discoveries

A developer shares what it was like to build Discord bots in Go, with examples of concurrency and challenges encountered along the way.

Hands typing on a laptop beside a notebook, glasses and a small plant.

Editorial translation by Impulso.

Hey, everyone! Today I decided to open up and share a bit about my adventures in the world of Discord bots using Golang.

I spend my days working with Ruby on Rails, but I have a bit of a crush on Golang (or have I fallen head over heels?). Seriously, there's something special about this language. It's the blend of simplicity and power that fascinates me. And what's better than getting to share what you know about something you love, right? That's why I love these events where we can swap ideas and learn together.

Why build Discord bots with Go?

Today we're kicking off a series of meetups where I'll show you how to build Discord bots with Golang. But don't think it's all fun and games! Building bots with Go is like unlocking a new level in the programming game. Plus, you'll pick up some skills that are real gems in the job market.

The Go language

First, let's talk about Go. Imagine a language as fast as a cheetah, as simple as "hello world," and as scalable as a startup that turns into a unicorn. Yep, that's Go's vibe. And companies' eyes light up when they see someone who knows the language inside out, especially for projects that need to be robust and efficient.

Plus, building bots gets you working directly with APIs, the backbone of modern applications. You'll deal with authentication, real-time events, asynchronous calls... all while talking to the Discord API. It's like learning to dance to several rhythms at once!

Concurrency in Go

And we can't forget about concurrency. Working with goroutines and channels in Go is almost like conducting an orchestra where every musician is playing a different tune, but somehow it all comes together in the end. That experience gets you ready to build systems that can take the strain, perfect for today's world, where everything happens in real time and data flies in every direction.

So get ready! What's coming up will level up your developer skills and might even open that magic door to new job opportunities.

Why is Go a good choice for Discord bots?

Think about it: You want to build a bot that'll be in thousands of channels, replying to messages, managing events, and maybe even telling a few bad jokes. So you want it to be fast, lightweight, and reliable, right? That's exactly where Go comes in!

Performance

Go is like that sports car everyone dreams of owning. Since it's a compiled language, it's ready to hit the gas. While interpreted languages are still thinking things through, Go gets straight to it. That means your bot will run smoothly, without hiccups, even when things get intense.

Simplicity

Another thing I love about Go is its simplicity. The language's creators wanted something straightforward, with no frills. With just 25 keywords, you can express complex ideas without getting lost in a sea of code. It's like writing a message to a friend, except the message is a program that does amazing things!

Concurrency

Now for the cherry on top: concurrency. As we know, bots need to do a thousand things at once, and Go makes that a walk in the park. With goroutines, you can run multiple tasks concurrently without breaking a sweat. It's almost magic!

Fast compilation

And to top it all off, compilation is super fast. Made a change? You're back up and running in seconds. That helps you move faster through the marathon of testing and tweaking.

At the end of the day, Go is a fantastic choice for Discord bots because it combines simplicity, performance, and efficient concurrency. So if you want a bot that won't let you down, you can bet on Go!

Downsides

That said, it's not all roses. Some devs find Go a little too simple and miss features like inheritance or more advanced functional programming. And if you're not careful with goroutines, you can get tangled up in problems like deadlocks or race conditions.

When not to use Go:

Go might not be the best choice for front-end development or for applications that need more complex data modeling. In those cases, languages like Python or Ruby might be a better fit.

To sum up, use Go when you need:

  • High performance
  • Efficient concurrency
  • Straightforward development
  • Scalable and distributed applications

Go really is like that multitool that solves a big problem when you least expect it!

Installing and setting up Go

To get started, go to the official Go website at golang.org/dl. Download the version for your operating system and architecture. If you're using WSL on an ARM64 system, remember to download the right version to save yourself a headache.

After downloading it, open your terminal and remove any older version:

sudo rm -rf /usr/local/go

Then extract the package you downloaded. In my case, it was go1.23.1.linux-amd64.tar.gz:

sudo tar -C /usr/local -xzf go1.23.1.linux-amd64.tar.gz

Set up your environment by adding Go to your PATH and setting GOPATH. I recommend using an IDE like VS Code or GoLand to make your life easier.

  • If you're in VSCode, use code ~/.zshrc or code ~/.bashrc depending on which shell you're using
  • Depending on your shell, you can also use a different editor. Just replace the command code with nano or vim, for example

The Discord API

The Discord API gives you plenty to work with if you want to build awesome bots. It lets you interact directly with servers, channels, messages, and users.

Authentication

First, you need a bot token. You get one by registering your bot in the Discord Developer Portal. Once you've created a new application, picked a cool name, and chosen a picture to give your bot a face, click Bot and then Reset Token. Don't go losing that token, okay? Make sure to store it in an environment variable or somewhere safe.

REST API

The REST API is where the magic happens. Basically, you make HTTP requests to specific endpoints to do things like send messages, manage servers, or manage members. It's all in JSON, simple and straightforward.

‍ Making requests from the terminal

You can test the API using curl in the terminal. For example, to get information about a channel:

curl https://discord.com/api/v10/channels/SEU\_ID\_DO\_CANAL \ -H "Authorization: Bot SEU_BOT_TOKEN"

To send a message:

curl -X POST https://discord.com/api/v10/channels/SEU\_ID\_DO\_CANAL/messages \ -H "Authorization: Bot SEU_BOT_TOKEN" \ -H "Content-Type: application/json" \ -d '{"content": "Hello, world!"}'

️ WebSockets and the Gateway API

To receive events in real time, you need to connect to the Gateway API via WebSockets. That way, your bot can respond instantly to new events, like messages or reactions.

Interactive components You can make your messages more dynamic with interactive components like buttons and select menus. Here are a few examples:

  • Action Row (Type 1)

It's a container for other components.

{ "content": "Here's a button:", "components": [ { "type": 1, "components": [ { "type": 2, "label": "Click here!", "style": 1, "custom_id": "botao_exemplo" } ] } ] }

  • Button (Type 2)

An interactive button.

{ "content": "Click the button below:", "components": [ { "type": 1, "components": [ { "type": 2, "label": "Click here!", "style": 1, "custom_id": "meu_botao" } ] } ] }

  • String Select (Type 3)

A dropdown menu with text options.

{ "content": "Select an option:", "components": [ { "type": 1, "components": [ { "type": 3, "custom_id": "select_opcoes", "options": [ { "label": "Option 1", "value": "valor_opcao_1" }, { "label": "Option 2", "value": "valor_opcao_2" }, { "label": "Option 3", "value": "valor_opcao_3" } ] } ] } ] }

  • User Select (Type 5)

Lets you select one or more users.

{ "content": "Select a user:", "components": [ { "type": 1, "components": [ { "type": 5, "custom_id": "select_usuario" } ] } ] }

  • Role Select (Type 6)

Lets you select a server role.

{ "content": "Select a role:", "components": [ { "type": 1, "components": [ { "type": 6, "custom_id": "select_role" } ] } ]}

  • Mentionable Select (Type 7)

Lets you select users or roles that can be mentioned.

{ "content": "Select a user or role that can be mentioned:", "components": [ { "type": 1, "components": [ { "type": 7, "custom_id": "select_mentionable" } ] } ] }

  • Channel Select (Type 8)

Lets you select channels on the server.

{ "content": "Select a channel:", "components": [ { "type": 1, "components": [ { "type": 8, "custom_id": "select_canal" } ] } ] }

A special bonus for anyone who made it this far

Building Discord bots with Go is quite a journey. You'll learn about the language and how to build efficient, scalable applications that are ready for the real world. And who knows? You might discover a new passion along the way. So let's get coding and see where this adventure takes us!

Oh, I almost forgot: click here to watch this really cool event we held in our Discord community. If you're not part of the community yet, just head to https://impulso.link/discord.

See you there,

Related articles