-
-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add possibility to start bot via async context manager (#1801)
* feat: Add possibility to start bot via async context manager * fix: Add type hints to __aexit__ * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * doc: Add async context manager to changelog * 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". * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: Lala Sabathil <lala@pycord.dev> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Lala Sabathil <lala@pycord.dev>
- Loading branch information
1 parent
f553fde
commit 8a913c3
Showing
3 changed files
with
53 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
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
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,29 @@ | ||
import asyncio | ||
|
||
import discord | ||
from discord.ext import commands | ||
|
||
bot = commands.Bot( | ||
command_prefix=commands.when_mentioned_or("!"), | ||
intents=discord.Intents.default(), | ||
) | ||
|
||
|
||
@bot.event | ||
async def on_ready(): | ||
print(f"Logged in as {bot.user} (ID: {bot.user.id})") | ||
print("------") | ||
|
||
|
||
@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}!") | ||
|
||
|
||
async def main(): | ||
async with bot: | ||
await bot.start("TOKEN") | ||
|
||
|
||
asyncio.run(main()) |