Skip to content

Commit

Permalink
example: Add example to start bot with async context manager
Browse files Browse the repository at this point in the history
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
Snawe committed Apr 7, 2023
1 parent ca883b3 commit a4b537d
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/basic_async_bot.py
@@ -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()

0 comments on commit a4b537d

Please sign in to comment.