This tutorial assumes several things:
- You have installed curious successfully
- You have created a bot account successfully
This tutorial will go over writing a bot that echos all messages to your console when they are received by the bot.
As your bot is incredibly simple, it probably will not need more than a single file currently. Your basic bot skeleton layout should look something like:
$ ls --tree
└── bot.py
The bot.py
file will contain all of the code for the bot.
Open up bot.py
and add the essential import:
from curious.core.client import Client
This will import Client
, which is used to communicate with
Discord.
Next, you want to define your new bot object.
botto = Client()
This object will be used to receive events from Discord, such as the messages.
To process events from Discord, you need to subscribe to an event. This will call a function automatically every single time an event is received by the websocket connection to process the event.
To receive new messages automatically, we have to subscribe to the
message_create
event, using Client.event()
.
@botto.event("message_create")
async def my_handler(ctx, message):
pass
The handler function is decorated with the event decorator, which takes the event name to process as an argument.
All event handlers take one argument, the EventContext
which
contains a small amount of context about the event. Right now, this is not
needed for our purposes.
The second argument to the message_create
event is a Message
object, which represents a message sent by Discord. We are interested in
Message.content
, to print to the console.
Modify the body of the function so that it prints to the console the message content:
print("Content:", message.content)
The final stage to this basic tutorial is to run the bot.
The simplest method is to call Client.run()
on the bot with your
token as the argument.
botto.run("MjYwOTUwODE2NTM2NTI2ODQ5.Cz2mGQ.SKl78a6NT6SBpwYQrIDnR1olPqo")
When you type in a server that you and the bot account share, you will then see your messages pop up in the log for the bot.