Coding a discord bot with python part 2!

Continue the journey of coding a discord bot

Posted by AwesomeAg on 04-21-2020

Before we start, if you have not seen the first part of this, click here
Open your main.py file. It should look like this:

1
2
3
4
5
6
7
8
import discord
import keep_alive
client = discord.Client()
bot = commands.Bot(command_prefix="!", description='My new bot!') #You can change the prefix if you want


keep_alive.keep_alive()
await bot.run("TOKEN")

Commands

now let’s make our first command!
Discord commands use decorators, so in this case, we will use @bot.command(). To define our command, we will use async def commmand(). All commands use the parameter ctx, though users do not enter anything in it. Enter the following into your code:

1
2
3
@bot.command()
async def greet(ctx): #in this command, ctx is the only parameter as we don't need users to type anything else.
await ctx.send("Hello!")

To pull all of what happened in that 1 line execution, I’ll just say the basics.

  • Most commands like ctx.send() need to be awaited to work.
  • ctx.send() is a function that will make the bot send a message with the content supplied.
    So if we ran this command, we would get an output of Hello! from the bot, sent to us in the server.
    ctx doesn’t only send messages, but it get’s information from messages too. So, let’s say that you wanted to get the name of the author that sent the command message. We could use ctx.author to store that as a string. But to send mentions, we would use
1
mention = "<@" + ctx.author.id + ">"

Let’s implement this into our code.
Using

1
2
3
@bot.command()
async def greet(ctx):
await ctx.send("Hello!")

we can add the mention to the command by replacing await ctx.send("Hello!") with await ctx.send("Greetings, <@{}>!".format(ctx.author.id))
In this example, I used format to make it easier.
So, we’ve made a command, but what if we want to liven it up with EMOJIS? well, using emojis in discord.py is simple and it has some perks!
To start with the basics, you can just send emojis in discord by using the emoji name that discord uses in between 2 colons. So, a thumbs up would be :thumbsup:. We can add this to the greet command now:

1
2
3
@bot.command()
async def greet(ctx):
await ctx.send(":smiley: :wave: GREETINGS, <@{}>!".format(ctx.author.id))

Which would return,
greeting
when you ran the command. Congrats! You’ve made you’re first command!
When I said earlier that there were perks to having a discord bot when it came to emojis, I meant that you can send emojis from other servers, and also send animated emojis. So basically, you would have nitro perks. You can do this by finding the name and ID of a custom emoji, and putting it in this format:
<:emojiname:emojiid>
but for animated ones, it’s:
<a:emojiname:emojiid>

Embed messages

Discord bots can send embed messages. Such a message would look like:
embedex
we can create an embed by storing it in a variable:

1
2
3
4
myembed = discord.Embed(title="yourtitle", description="adescription", color=0xFFFFFF) #the color can be any hex code color.
#we can also add fields with descriptions and names
myembed.add_field(name="asubtitle",value="Content",inline=False) #or inline=true
await ctx.send(embed=myembed) #now to ctx.send it

This would return something like this:
embedexample
You can also set the images and footers of embeds:

1
2
3
4
5
6
7
# set image:
myembed.set_image(url="pathtoimage/urltoimage")
# or an image at the top
myembed.set_thumbnail(url="url/pathtoimage")
# and a footer
myembed.set_footer(text="hehe",icon_url="url.hehe.com/hehe.png")
# the footer will show a small image from the path and then text

Kewl! You’ve gotten pretty far with this now! I might make a part 3, and be sure to check out my blog post on how to host your bot on repl.it with uptime robot.