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 BotBot10/16/2023
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 BotBot10/16/2023
Hello! Here's a cool embed.
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(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)

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 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_thumbnail 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, 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.

Text Markdown

Text formatting can be used in messages and in most embed parts, as explained in the dedicated section below.

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

Code Markdown

To create a single-line code block without syntax highlight, wrap the text between single backticks. This code block will only add a dark background to the text.

`print("Hello world!")`

To create a multi-line code block without syntax highlight, wrap the text between triple backticks on first and last line. This type of code block will encase the code inside a box.

```
message = "Hello world!"
print(message)
```

To create a multi-line block with syntax highlight, add the name of the language you are using right after the triple backticks on the first line. For example, for Python you can write either "python" or "py".

```python
message = "Hello world!"
print(message)
```

If you want to use syntax highlight for a single line of code, you still have to format it like a multi-line block but the code must be on a separate line than the triple backticks.

Quote Markdown

To create a single-line quote block, start the line with > followed by a space.

> This is a single-line quote.

To create a multi-line quote block, write >>> followed by a space. All text from that point onwards will be included in that quote block.

>>> This is a
multi-line quote.

Spoiler Markdown

To hide a spoiler, encase the text between double pipes. The users will have to click on it before being able to see the text contained in it.

||spoiler||

Link formatting only works in embeds and messages sent through webhooks, by using the following syntax:

[Pycord](https://pycord.dev/)

Inside the supported elements that have just been mentioned, the example above will look like this: Pycord

Outside them, the link will still be clickable but the formatting will be ignored, therefore the example above will look similarly to this: [Pycord](https://pycord.dev/)

Embed Markdown

Adding markdown to your embeds or messages will give your bot the sparkle it needs, however, markdown is limited inside embeds. Use the following table as a reference and keep in mind that embed title and filed names will always show in bold.

PartTextLink
Embed AuthorNoNo
Embed TitleYesNo
Embed DescriptionYesYes
Field NameYesNo
Field ValueYesYes
Embed FooterNoNo