Skip to content

Commit

Permalink
Close aiohttp client session on cog unload (#235)
Browse files Browse the repository at this point in the history
* Close `aiohttp.ClientSession` on `cog_unload` & some type hinting.

* `pre-commit run --all-files` 😃
  • Loading branch information
mellow-org authored Aug 24, 2024
1 parent 1adcd39 commit 750502d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ You can contact me in the Red 3rd party server in #support_flare-cogs
| Crypto | 0.1.0 | <details><summary>Buy and sell cyrptocurrencies with Red economy.</summary>Buy and sell cryptocurrencies with Red economy.</details> | flare(flare#0001), Flame, and TrustyJAID |
| DmInvites | 0.0.4 | <details><summary>Respond with the bot invite link when DM'd a server invite link.</summary>Respond with the bots invite link if the bot recieves a message containing a server invite.</details> | flare(flare#0001) |
| VerifyEmail | 0.1.1 | <details><summary>Secure your server by making users verify their account with an email.</summary></details> | flare(flare#0001) |
| F1 | 0.4.0 | <details><summary>Data related to F1.</summary>F1 data, races, drivers, constructors etc.</details> | flare(flare#0001) |
| F1 | 0.4.1 | <details><summary>Data related to F1.</summary>F1 data, races, drivers, constructors etc.</details> | flare(flare#0001) |
| Faceit | 0.2.0 | <details><summary>List stats of faceit users, game history or specific games and even ongoing games!</summary></details> | flare(flare#0001) |
| Forward | 1.2.9 | <details><summary>Forward messages sent to the bot to the bot owner or in a specified channel.</summary></details> | flare(flare#0001) and Aikaterna |
| Giveaways | 1.3.3 | <details><summary>Giveaway cog with features such duration or end timing, multipliers, role only acces, bank integration etc.</summary></details> | flare(flare#0001) |
Expand All @@ -46,7 +46,7 @@ You can contact me in the Red 3rd party server in #support_flare-cogs
| Jsk | 0.0.2 | <details><summary>Jishaku ported to Red.</summary></details> | flare(flare#0001) |
| Mod | 1.3.0 | <details><summary>Mod with custom messages.</summary>Core mod with the inclusion of custom messages for banning, kicking and unbanning.</details> | flare(flare#0001) |
| Covid | 0.0.3 | <details><summary>Grab the latest headlines, nationally or globally.</summary>Grab breaking headline around the world!</details> | flare(flare#0001) |
| Palette | 0.1.0 | <details><summary>Show colour palette of images, avatars etc.</summary>Show colour palette of images, avatars, emojis etc</details> | flare(flare#0001) and Kuro |
| Palette | 0.1.1 | <details><summary>Show colour palette of images, avatars etc.</summary>Show colour palette of images, avatars, emojis etc</details> | flare(flare#0001) and Kuro |
| PermChecker | 0.1.0 | <details><summary>This cog is a basic permission checker, used to check if a user/role/channel has a set permission.</summary></details> | flare(flare#0001) |
| R6 | 1.6.0 | <details><summary>Show R6 Statistics.</summary>List R6 Statistics from seasons, individual operators, all operators and more!</details> | flare(flare#0001) |
| RedditPost | 0.6.0 | <details><summary>Reddit Autoposting of new content.</summary></details> | flare(flare#0001) |
Expand Down
15 changes: 9 additions & 6 deletions f1/f1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import tabulate
from discord.mentions import AllowedMentions
from redbot.core import Config, commands
from redbot.core.bot import Red
from redbot.core.utils.chat_formatting import box, pagify

API_URL = "http://ergast.com/api/f1"
Expand All @@ -19,23 +20,25 @@
class F1(commands.Cog):
"""F1 data."""

__version__ = "0.4.0"
__version__ = "0.4.1"
__author__ = "flare"

def format_help_for_context(self, ctx):
"""Thanks Sinbad."""
pre_processed = super().format_help_for_context(ctx)
return f"{pre_processed}\nCog Version: {self.__version__}\nAuthor: {self.__author__}"

def __init__(self, bot):
self.bot = bot
self.session = aiohttp.ClientSession()
self.config = Config.get_conf(self, identifier=95932766180343808)
def __init__(self, bot: Red) -> None:
self.bot: Red = bot
self.session: aiohttp.ClientSession = aiohttp.ClientSession()
self.config: Config = Config.get_conf(self, identifier=95932766180343808)
self.config.register_guild(channel=None, role=None)
self.loop = asyncio.create_task(self.race_loop())
self.loop: asyncio.Task = asyncio.create_task(self.race_loop())

async def cog_unload(self):
self.loop.cancel()
if not self.session.closed:
asyncio.create_task(self.session.close())

async def race_loop(self):
await self.bot.wait_until_ready()
Expand Down

0 comments on commit 750502d

Please sign in to comment.