DEV Community

Blue
Blue

Posted on

How to create a Discord Bot in Python [interactions.py]

So, you have seen those fancy bots with different slash commands on Discord and you want to make one for yourself, but you don't know where to start.

In this post, I will guide you how to make a simple Discord Bot in Python, using a library called interactions.py.
Requirements: You need to have a good view with Python first as making a Discord Bot isn't exactly considered as beginner-friendly.

Disclaimer: I will skip most part with creating an Application and inviting your bot to your server as it can be found in other online contents. We will walk straight in.

First, you need to install the library, using pip:
pip install -U discord-py-interactions

We will create a simple connection first so the bot can be alive. Create a new file in your IDE ending with .py and copy this piece of code.

import interactions

client = interactions.Client(token="your_bot_token_here")

@client.event
async def on_ready():
    print("Ready!")

client.start()
Enter fullscreen mode Exit fullscreen mode

Note: If you encounter an error ModuleNotFoundError, check your installed version of discord-py-interactions again. This library requires Python 3.8.6+.

The terminal should show "Ready!" and your bot should be online now, but wait, it doesn't do much. Let's add a basic command to it.

import interactions

client = interactions.Client(token="your_bot_token_here")

@client.command(
    name="ping",
    description="Ping pong",
    scope=your_guild_id_here
)
async def _ping(ctx: interactions.CommandContext):
    await ctx.send("Pong!")

@client.event
async def on_ready():
    print("Ready!")

client.start()
Enter fullscreen mode Exit fullscreen mode
  • @client.command(), this is called a decorator in Python. This handles the request to Discord API to register a new command.
  • name: The name of your command. Do notice that a command name cannot contain special character or space.
  • description: The description of the command.
  • scope: This is optional. Discord has 2 types of command: guild command and global command. Guild command will appear immediately in the registered guild and cannot be used in other guilds. Global command can be used in all servers that the bot is in, but it can takes up to 1 hour to appear in all guilds. Global command should be used when you have finished with the command while guild command should be used for testing purposes.
  • async def _ping(ctx: interactions.CommandContext):: This is known as a coroutine.
  • await ctx.send("Pong!"): Starting after the coroutine, the command will be executed. This line will send a message with the content "Hi there!" when someone invokes the slash command.

Now, re-run your bot and in your test server, use the command by typing / and the name. If the bot sends the message, congratulations, you just create a first command in interactions.py.

Stay tuned with upcoming post about interactions.py and how you can take advantage of the library to create an advanced bot for your server or a public one.

If you have any question with the library, let me know in the comment section or join the official interactions.py Discord server for further help.

Top comments (3)

Collapse
 
savioxavier profile image
Skyascii

Thank you Blue, very cool!

Collapse
 
v3ntus profile image
Ventus

Very neat 😎

Collapse
 
b1uedev profile image
Blue

Thanks. :)