From dbdbc9b194f13464cdc01549822d84d40f1edec2 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 26 Nov 2022 08:50:19 +0100 Subject: [PATCH 1/6] feat: Add possibility to start bot via async context manager --- discord/client.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/discord/client.py b/discord/client.py index 654219a0bd..dce7e83292 100644 --- a/discord/client.py +++ b/discord/client.py @@ -253,6 +253,22 @@ def __init__( VoiceClient.warn_nacl = False _log.warning("PyNaCl is not installed, voice will NOT be supported") + async def __aenter__(self) -> Client: + loop = asyncio.get_running_loop() + self.loop = loop + self.http.loop = loop + self._connection.loop = loop + + self._ready = asyncio.Event() + + return self + + async def __aexit__(self) -> None: + if not self.is_closed(): + await self.close() + + # internals + def _get_websocket( self, guild_id: int | None = None, *, shard_id: int | None = None ) -> DiscordWebSocket: From 38e1b0b047fe11d603825ef83c8e389b159cc3b3 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 15 Jan 2023 10:06:02 +0100 Subject: [PATCH 2/6] fix: Add type hints to __aexit__ --- discord/client.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/discord/client.py b/discord/client.py index dce7e83292..1b160eb1a5 100644 --- a/discord/client.py +++ b/discord/client.py @@ -31,6 +31,7 @@ import sys import traceback from typing import TYPE_CHECKING, Any, Callable, Coroutine, Generator, Sequence, TypeVar +from types import TracebackType import aiohttp @@ -263,7 +264,11 @@ async def __aenter__(self) -> Client: return self - async def __aexit__(self) -> None: + async def __aexit__(self, + exc_t: BaseException | None, + exc_v: BaseException | None, + exc_tb: TracebackType | None + ) -> None: if not self.is_closed(): await self.close() From 92f60ac0d618cf17070b5e9224bd2568a2660365 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Jan 2023 09:06:43 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- discord/client.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/discord/client.py b/discord/client.py index 1b160eb1a5..27e94d7c2f 100644 --- a/discord/client.py +++ b/discord/client.py @@ -30,8 +30,8 @@ import signal import sys import traceback -from typing import TYPE_CHECKING, Any, Callable, Coroutine, Generator, Sequence, TypeVar from types import TracebackType +from typing import TYPE_CHECKING, Any, Callable, Coroutine, Generator, Sequence, TypeVar import aiohttp @@ -264,10 +264,11 @@ async def __aenter__(self) -> Client: return self - async def __aexit__(self, + async def __aexit__( + self, exc_t: BaseException | None, exc_v: BaseException | None, - exc_tb: TracebackType | None + exc_tb: TracebackType | None, ) -> None: if not self.is_closed(): await self.close() From ca883b346ef86a228f7fdf6dd3df797dae47ff0b Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 2 Apr 2023 11:35:31 +0200 Subject: [PATCH 4/6] doc: Add async context manager to changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd044d7072..7ff949d277 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ These changes are available on the `master` branch, but have not yet been releas ### Added +- Added possibility to start bot via async context manager. + ([#1801](https://github.com/Pycord-Development/pycord/pull/1801)) - Added new events `on_bridge_command`, `on_bridge_command_completion`, and `on_bridge_command_error`. ([#1916](https://github.com/Pycord-Development/pycord/pull/1916)) From 2f7680fb12f9841af09777a81cf124316244d46a Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 2 Apr 2023 11:56:39 +0200 Subject: [PATCH 5/6] 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". --- examples/basic_async_bot.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 examples/basic_async_bot.py diff --git a/examples/basic_async_bot.py b/examples/basic_async_bot.py new file mode 100644 index 0000000000..0bdb89b0fa --- /dev/null +++ b/examples/basic_async_bot.py @@ -0,0 +1,30 @@ +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()) From 6de8987d0500bfab8b235f44c7fcb633b777ccfe Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 7 Apr 2023 11:18:53 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/basic_async_bot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/basic_async_bot.py b/examples/basic_async_bot.py index 0bdb89b0fa..a72859fb37 100644 --- a/examples/basic_async_bot.py +++ b/examples/basic_async_bot.py @@ -3,7 +3,6 @@ import discord from discord.ext import commands - bot = commands.Bot( command_prefix=commands.when_mentioned_or("!"), intents=discord.Intents.default(),