Skip to content

Examples


The following examples should work out-of-the-box and assume you have your bot set up.

For setup, see the Bot setup guide.

Legacy Examples

These features exists for compatibility with older bots. Prefer the new examples. Not sure what is the new equivalent of the legacy example? Check out Migrations

About these examples

Each example is self-contained and can be copied directly into your bot between setup and bot.run().

Table of Contents


Setting and Responding to a Command Prefix


Demonstrates responding to a message prefix command. Sends the message Pong! if !ping is sent.

Important

Using message prefix commands requires the PRIVILEGED intent MESSAGE_CONTENT!

@client.prefix_command
async def ping(bot: discord.Client, event: discord.MessageCreateEvent): # the function name is the name of the command!
    await event.message.send(f"Pong!")

Building an Embed


Demonstrate building an embed in response to a message prefix.

@client.prefix_command
async def example(bot: discord.Client, event: discord.MessageCreateEvent): # the function name is the name of the command!
    await event.message.send(
        discord.MessageBuilder()
            .add_embed(discord.EmbedBuilder('Example Embed')
                .set_user_author(event.interaction.member.user)
                .set_color('#d4af37')
                .set_timestamp()
            )
    )

Tip

While this example shows building an embed in response to a message prefix, you can also build embeds in response to InteractionEvent!