-
-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: Add example to start bot with async context manager
Add basic example to start the bot with an async context manager. The example contains a `/hello` slash command which responds with a "Hellp @author".
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import asyncio | ||
|
||
import discord | ||
from discord.ext import commands | ||
|
||
class Bot(commands.AutoShardedBot): | ||
def __init__(self): | ||
super().__init__( | ||
command_prefix="!", | ||
intents=discord.Intents.default(), | ||
) | ||
|
||
async def on_ready(self): | ||
print("Ready called!") | ||
|
||
def main(): | ||
asyncio.run(start_bot_session()) | ||
|
||
async def start_bot_session(): | ||
async with Bot() as bot: | ||
# NOTE: | ||
# Better add those slash commands to a cog (check examples/app_commands/slash_cog). | ||
# But for the purpose of simplicity, we just define the command here. | ||
@bot.slash_command(guild_ids=[...]) # Create a slash command. | ||
async def hello(ctx: discord.ApplicationContext): | ||
"""Say hello to the bot""" # The command description can be supplied as the docstring | ||
await ctx.respond(f"Hello {ctx.author.mention}!") | ||
|
||
await bot.start("TOKEN") | ||
|
||
if __name__ == "__main__": | ||
main() |