Coding A Discord Bot Using Elixir And Alchemy

Alim Arslan Kaya
Geek Culture
Published in
3 min readJul 8, 2021

A getting started guide to Elixir and Alchemy.

In this story, you will learn how to make a simple Discord bot using Elixir and Alchemy.

Note:

I won’t be explaining what Elixir is here. If you want to learn more about Elixir, make sure to check out my story about it.

Let’s start!

Creating our bot

Here are the steps to create a Discord bot.

1. Make sure you’re logged on to the Discord website.

2. Navigate to the Applications page.

3. Click on the “New Application” button.

4. Give the application a name and click “Create”. I named my bot Allybot.

5. Go to the “Bot” tab and then click “Add Bot”. You will have to confirm by clicking “Yes, do it!”

6. Keep the default settings for Public Bot (checked) and Require OAuth2 Code Grant (unchecked).

7. Your bot has been created. The next step is to copy the token.

This token is your bot’s password so don’t share it with anybody. It could allow someone to log in to your bot and do all sorts of bad things.

You can regenerate the token if it accidentally gets shared, by the way.

8. Save the token somewhere, you will need it when we start coding the bot.

Coding our bot

Time to write some code! But first, we have to initialize a new project.

Initializing our project

mix new allybot --sup

This command will create a new Elixir app running under a supervisor. Easy, right? Also, you don’t have to name your bot “allybot”. You can change it to whatever you want.

You can clean up the generated project however you want. Just make sure to not delete anything except the test folder.

Adding Alchemy

Let’s add Alchemy to our project.

Open your mix.exs file, and add the line below inside deps .

{:alchemy, "~> 0.7.0", hex: :discord_alchemy}

This is the current latest Alchemy version. Make sure to check Alchemy docs to see if there are any new versions.

After that, open lib/allybot/application.ex and make sure it looks like the code below.

This basically starts your bot using the token and loads the command modules.

Commands Module

Let’s create our command module.

Create a new file called commands.ex inside the lib folder and add the following code inside of it.

This command will basically listen for “!ping” messages and reply to them with a “Pong!”.

But if you run the bot now, it will fail. Why though?

Because we haven’t created a config file and our token in it, of course!

Config

Let’s add our config now.

Create a folder called config and inside that folder, create a file called config.exs .

Then inside config/config.exs , add the following code.

This is how we set config variables in Elixir.

The last step is to actually set the Environment Variable. To do that, run the command below inside of your terminal.

export BOT_TOKEN=<your-bot-token>

And that’s it! You can now run your bot and test our ping command.

Final Thoughts

You’ve learned the basics of Alchemy. I hope that you’ve learned something new from this story.

If you want to learn more about Alchemy, make sure to take a look at its documentation.

And that’s it. Thanks for reading this story!

If you liked the story, make sure to clap to it! And feel free to ask me anything you want.

Follow me on Twitter:

Support me on Patreon:

--

--

Alim Arslan Kaya
Geek Culture

Fullstack Go, Rust, Elixir, Python and TypeScript Enthusiast.