Day 2 - Let's create some Python code!

·

4 min read

So, we've done a little prep work in Discord for our bot, let's look at a basic set of Python code that'll essentially get our bot basically working:

import discord
from discord.ext import commands

What these 2 lines do:

  • Import the discord library (in this case discord.py, which I will show you how to import on your own system in the next tutorial)

  • From the discord library, import the 'commands' function and enable you to use it without having to type discord.ext.commands each time you want to reference it and use it's methods. This is common practice and is outside the scope of this particular tutorial

bot = commands.Bot(command_prefix='!')

Here we set the prefix for commands in the server - it is usual to use the ! character, but you can use $ or % or any special character you wish. For the purposes of this tutorial, I suggest you use ! too so that you can follow along without changing your code.

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user}')

@bot.event - this tells Python that the next function we are placing will be a Bot Event.

async def on_ready(): - this async keyword is used because bot events will be triggered at any time when the bot is running and not during the sequential functioning of the program so are triggered by the program and the async keyword helps this to happen.

print(f'Logged in as {bot.user}') - this will print the bot's username. In this case, it will print 'Logged in as KittyBot'

Printing with string formatting is outside the scope of this tutorial but essentially, when adding an 'f' before the quotation marks will take anything inside curly braces { } and substitute that for the variable name. For example, if I was to type this:

name = 'Kitty'
print(f'My name is {name}!')

..it would print 'My name is Kitty!'

@bot.command(name='hello', help='Responds with a greeting')
async def hello(ctx):
    await ctx.send(f'Hello, {ctx.author}!')

This time, a bot command as denoted by @bot.command.

The text after denotes the name of the command, in this case 'hello' which would it would detect when someone typed !hello in discord, as it uses our bot prefix of '!' and the event name of hello. The help keyword after it is purely so we can see what we are using it for, the user would not see this.

Another async command, the function name called the same as the command in this case hello, and then in brackets, 'ctx'

ctx

What is ctx?

ctx is the context of the message that is sent to the bot. This could be additional words after the command, or details about the author of the command, their username, discord ID etc.

You'll see this a lot in the code we will produce so it's important that it's mentioned here.

Following on, we have await ctx.send(f'Hello, {ctx.author}!')

The await keyword essentially sends the message to discord but allows discord to post it once it is ready. ctx.send tells discord we want to send a message, and the message is Hello, and then the author of our ctx, or our context. Essentially, we want to say Hello, and then the username of the person who posted the command, replying to them with a welcoming and friendly Hello!

Finally, at the very end of our code, we want to add:

bot.run('YOUR_BOT_TOKEN')

Which is running our bot. What is 'YOUR_BOT_TOKEN'?

Well next time, you will find out! To summarise then, our complete code is:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user}')

@bot.command(name='hello', help='Responds with a greeting')
async def hello(ctx):
    await ctx.send(f'Hello, {ctx.author}!')

bot.run('YOUR_BOT_TOKEN')

We learnt...

  • How to setup our discord bot in a file

  • How @ commands tell our code what our bot needs to do

  • A little information about async and await (I suggest you look into these keywords further if you don't understand them! Please comment on this post if you need help finding further resources).

Next time...

  • Installing the discord library on our PC

  • Creating our discord python file

  • Getting our BOT TOKEN from Discord

  • Running our bot and testing it