i said iād make one of these ages ago, the idea was to start this early so that i didnāt have to cover much, but i got ahead of myself.Ā
anyway, since this is a very long post, iāll put the rest under the cut for yāall.
( Disclaimer; iām not a professional programmer, this is just a hobby of mine. iām self-taught and some of the stuff i post here isnāt the best way you could make a bot. if you actually wanna learn from professionals invest into some courses )
so, for the record; benrybot (passbotry) is a bot coded on python using the discord.py API. itās quite small, i only started work on it five days ago.
if yāall are interested in making a bot yourselves, the discord.py website has everything you need to install it and get started; bithub and stackoverflow are your friends.
anyways, iām explaining the code from the start;
these are all the imports iām using for the bot, they always go at the top of your code so that python recognizes and unpacks/processes all these imports before touching any of the other code.Ā imports are basically like additional code packs that you download and then can use on python.
most of these are just here to let me access discord.pyās features, except theĀ ārandomā import which is a randomizing commandset,
these lines of code and basically an authentication process telling me what the bot is online, and setting itās status ( you can see i tried to label it with comments (the red writing))
-
now hereās where the actual commands begin;
ā@client.event ā is the line that lets the bot know this is the start of a command, referred to as an event.Ā Ā āclient.Ā ā is the prefix to most lines of code featuring the bot.
āasync def on_message(message):ā tells the program that the next line is defining what the bot will do during the command/event.Ā āon_messageā means this event is triggered when any message is sent on the server.
ā if message.content ==Ā ābenbot testā:Ā āĀ just means if thereās a message that saysĀ ābenbot testā, weāll initiate the next line of code. if not, the bot does nothing.
ā await client.send_message(message.channel, āsuccessā)ā.Ā āawaitā lets the bot multitask, instead of waiting to finish any other processes it skips to the await line,Ā Ā āclient.sned_messageā send the message,Ā ā(message.channel,ā means it will be sent in the same channel andĀ āsuccessā was the content of our message.
so the result of these lines of code is:
the bot processes thatĀ ābenbot testā has been sent and replies in the same channel with itās predetermined response (please donāt mind the botās nickname on this example.)
anyway, thatād be it for part one of benrybotās behind the scenes.Ā
on the next post iāll go into detail on benrybotās more complex commands, formatting and the random.choice function.