Skip to main content

More Features

So, you just created your first Pycord bot! Now, let's add some more features to it. This will include adding event handlers, waiting for user response, styling the messages, and more.

Events

Event Handlers

Events in Discord are a way to listen for certain actions. For example, if you want to know when a user joins your server so you could send a welcome message, you can use the on_member_join event.

First, you need to ask Discord to send you events. This is done via "Intents". Read up the Intents page for more information.

Once you understand what intents are, you can enable the events you need, or just use the default ones with discord.Intents.all().

Now that that's done, let's add an event handler for when a user joins the server. We will use the on_member_join event. We will send a private message to the user welcoming them to the server.

@bot.event
async def on_member_join(member):
await member.send(
f'Welcome to the server, {member.mention}! Enjoy your stay here.'
)

We use the discord.Bot.event decorator to add the event handler.

The on_member_join event is called when a user joins the server. The member parameter is the user that joined. Different events have different names and parameters. You can find all of them here.

So, that's how you add event handlers!

Waiting for User Response

Let's say you want to create a Guess-the-Number game (where the user has to guess a number between 1-10). You need to send a message to a user and wait for them to respond. You can do this with the wait_for method.

@bot.command()
async def gtn(ctx):
"""A Slash Command to play a Guess-the-Number game."""

await ctx.respond('Guess a number between 1 and 10.')
guess = await bot.wait_for('message', check=lambda message: message.author == ctx.author)

if int(guess.content) == 5:
await ctx.send('You guessed it!')
else:
await ctx.send('Nope, try again.')

wait_for takes one argument, which is the event type. The event type is the name of the event you want to wait for. In this case, it's message. It could also be reaction to wait for a reaction to be added.

We pass a keyword argument to wait_for called check. The function may look complicated if you're a beginner. We pass a lambda function, which simplifies the code a bit.

The lambda function takes one parameter, message. When Pycord receives a message, it passes it to the check function. If the function returns True, the message is returned. If the function returns False, the message is ignored and the bot waits for another message.

Here, we check if the message is from the user that sent the command. We simply use message.author == ctx.author. This will check if the author of the message was the person who invoked the command.

Styling Messages

Embeds

Embeds are a Discord feature that allows applications to format their messages in cool embedded format, enabling your bot to lay out messages with a lot of text into neat fields.

Guide BotBot06/27/2022
The Pycord Guide is a detailed guide that explains how to use Pycord and all of its features.
Getting Started
The Getting Started section explains how you can get a brand new bot created and running from scratch.
Interactions
Interactions with Pycord
Extensions
Pycord's various Extensions
Popular Topics
Other Popular Topics
And More!
We have so much more! Just explore, and you will find everything you need. If you want another page added, open an issue on the GitHub.

Creating embeds is simple! Just create an instance of discord.Embed and edit it to your liking. Once you're done, send it!

import discord

bot = discord.Bot()

@bot.command()
async def hello(ctx):
embed = discord.Embed(
title="My Amazing Embed",
description="Embeds are super easy, barely an inconvenience.",
color=discord.Colour.blurple(), # Pycord provides a class with default colors you can choose from
)
embed.add_field(name="A Normal Field", value="A really nice field with some information. **The description as well as the fields support markdown!**")

embed.add_field(name="Inline Field 1", value="Inline Field 1", inline=True)
embed.add_field(name="Inline Field 2", value="Inline Field 2", inline=True)
embed.add_field(name="Inline Field 3", value="Inline Field 3", inline=True)

embed.set_footer(text="Footer! No markdown here.") # footers can have icons too
embed.set_author(name="Pycord Team", icon_url="https://example.com/link-to-my-image.png")
embed.set_thumbnail(url="https://example.com/link-to-my-thumbnail.png")
embed.set_image(url="https://example.com/link-to-my-banner.png")

await ctx.respond("Hello! Here's a cool embed.", embed=embed) # Send the embed with some text

bot.run("TOKEN")
Guide Manused /hello
Guide BotBot06/27/2022
My Amazing Embed
Embeds are super easy, barely an inconvenience.
A Normal Field
A really nice field with some information. The description as well as the fields support markdown!
Inline Field 1
Inline Field 1
Inline Field 2
Inline Field 2
Inline Field 3
Inline Field 3

This simple command sends replies to a slash command with an embed. Let's break it down:

embed = discord.Embed(
title="My Amazing Embed",
description="Embeds are super easy, barely an inconvenience.",
color=discord.Colour.blurple(),
)

This command creates an embed. We use the Embed class to create an embed object with the title "My Amazing Embed", the description "Embeds are super easy, barely an inconvenience.", and the color blurple, Discord's main theme color.

discord.Colour is a class full of classmethods that return color values. While the official, documented name of this is discord.Colour, discord.Color works as well.

embed.add_field(title="A Normal Field", value="A really nice field with some information. **The description as well as the fields support markdown!**")
embed.add_field(title="Inline Field 1", value="Inline Field 1", inline=True)
embed.add_field(title="Inline Field 2", value="Inline Field 2", inline=True)
embed.add_field(title="Inline Field 3", value="Inline Field 3", inline=True)

This small section shows off embed fields. You can add fields to embeds with the add_field method of the discord.Embed class. These consist of three keyword arguments: title, value, and inline. title and value are both required arguments, which inline defaults to False if it's not defined. The inline argument specifies whether or not space will be divided among the inline fields. There could be a mix of fields where inline has different values too.

embed.set_footer(text="Footer! No markdown here.") # footers can have icons too
embed.set_author(name="Pycord Team", icon_url="https://example.com/link-to-my-image.png")
embed.set_thumbnail(url="https://example.com/link-to-my-thumbnail.png")
embed.set_image(url="https://example.com/link-to-my-banner.png")

In this section, we're adding unique items to the embed. These items are:

  • Footer - With the set_footer() method, you can set a small footer that holds a message. This has text and icon_url kwargs.
  • Author - With the set_author method, you can set an author for the embed. This is a small text field at the top of the embed. This includes name, url and icon_url kwargs.
  • Thumbnail - With the set_thumbnial method, you can set a small image to reside at the top-right of the embed. This has a single url kwarg.
  • Image - With the set_image method, you can set an image to sit at the bottom of an embed. This has a single url kwarg.

There are a lot more methods and attributes you can use to configure embeds. Here, we just covered the basics. Also remember that all of these values are not necessary in an embed. An embed may only contain a few of these, for example, only a description, or a title and a description, and so on.

Markdown

Markdown is a type of markup language that's limited in terms of formatting yet simple. Discord allows for a watered-down version of markdown to be in their messages. This includes:

*italics*__*underlined italics*__
**bold**__**underlined bold**__
***bold italics***__***underlined bold italics***__
__underlined__~~strikethrough~~

Sadly, Discord does not support other types, such as hyperlinks. The only supported places for hyperlinks are in embeds and messages sent through webhooks.

Adding markdown to your embeds or messages will give your bot the sparkle it needs.

Here is an example for a hyperlink in embeds.

[Click here!](https://pycord.dev/)

Inside the embed, the example above will look like this: Click here!

caution

Some embed fields, such as the footer, do not support markdown at all, including bold and italics.

Code Markdown

For code markdown, you can choose between Code Text and Code Blocks.

  • `Code Text`
  • ```

Code Blocks ```

Code Blocks support different programming languages, such as Python.

If you start your Code Block with ```python, you can send readable Python code to Discord.