Learn Before
Concept

Get your bot to send a "Hello world!" message

Now that pretty much everything is set up, we can get the bot to send a message after a user types a command. Every slash command must begin with some basic descriptions of the command:

@slash.slash( name="Hello", description="Responds with a Hello World message", guild_ids=GUILD_ID, )

@slash.slash tells the bot that we are creating a slash command and the arguments are how the user can interact.

  1. The name is what the user will actually use to call the command. In this case, they would type "/Hello" and the bot would respond.

  2. The description is a hint that the user sees when typing in the command. This helps users understand what the command does.

  3. guild_ids is a list of IDs in which the bot is allowed to send slash commands. This is required so that the bot can initialize slash commands in that server which the user can then interact with.

On the next line immediately following the ending parenthesis of the previous command we will define a function that is executed once the above command is called:

async def _hello(ctx); await ctx.send("Hello world!")
  1. async defines this function as asynchronous which is required when using any Discord functions that interact with the Discord application (sending a message, deleting a message, sending an embed, etc).

  2. ctx is an object that is received once a user calls the command. It contains the context of the interaction, including information such as what user used the command, what time it was used, what channel, etc.

  3. await is a required keyword in an asynchronous function as the command following the keyword must be awaited before continuing.

  4. ctx.send will send the message inside of the parenthesis to the channel in which the user used the slash command.

At the very bottom of your script, include the following line of code to run the bot using your token, replacing "client" with whatever you called your bot in the "Initializing bot variables" node:

client.run(TOKEN)

0

1

Updated 2021-07-18

Tags

Python Programming Language

Data Science