diff --git a/exts/ae7q.py b/exts/ae7q.py index 6671c7c..414349a 100644 --- a/exts/ae7q.py +++ b/exts/ae7q.py @@ -17,6 +17,7 @@ WF4EMA: " import discord.ext.commands as commands +import aiohttp from bs4 import BeautifulSoup import common as cmn @@ -25,7 +26,7 @@ import common as cmn class AE7QCog(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot - self.session = bot.qrm.session + self.session = aiohttp.ClientSession(connector=bot.qrm.connector) @commands.group(name="ae7q", aliases=["ae"], category=cmn.cat.lookup) async def _ae7q_lookup(self, ctx: commands.Context): diff --git a/exts/image.py b/exts/image.py index c4f92d9..2c871d8 100644 --- a/exts/image.py +++ b/exts/image.py @@ -9,6 +9,8 @@ General Public License, version 2. import io +import aiohttp + import discord import discord.ext.commands as commands @@ -20,7 +22,7 @@ class ImageCog(commands.Cog): self.bot = bot self.bandcharts = cmn.ImagesGroup(cmn.paths.bandcharts / "meta.json") self.maps = cmn.ImagesGroup(cmn.paths.maps / "meta.json") - self.session = bot.qrm.session + self.session = aiohttp.ClientSession(connector=bot.qrm.connector) @commands.command(name="bandplan", aliases=['plan', 'bands'], category=cmn.cat.ref) async def _bandplan(self, ctx: commands.Context, region: str = ''): diff --git a/exts/qrz.py b/exts/qrz.py index 5cc94b8..67ffb22 100644 --- a/exts/qrz.py +++ b/exts/qrz.py @@ -21,7 +21,7 @@ import data.keys as keys class QRZCog(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot - self.session = bot.qrm.session + self.session = aiohttp.ClientSession(connector=bot.qrm.connector) self._qrz_session_init.start() @commands.command(name="call", aliases=["qrz"], category=cmn.cat.lookup) diff --git a/exts/study.py b/exts/study.py index faf40a0..f94aecf 100644 --- a/exts/study.py +++ b/exts/study.py @@ -10,6 +10,8 @@ General Public License, version 2. import random import json +import aiohttp + import discord.ext.commands as commands import common as cmn @@ -20,7 +22,7 @@ class StudyCog(commands.Cog): self.bot = bot self.lastq = dict() self.source = 'Data courtesy of [HamStudy.org](https://hamstudy.org/)' - self.session = bot.qrm.session + self.session = aiohttp.ClientSession(connector=bot.qrm.connector) @commands.command(name="hamstudy", aliases=['rq', 'randomquestion', 'randomq'], category=cmn.cat.study) async def _random_question(self, ctx: commands.Context, level: str = None): diff --git a/exts/weather.py b/exts/weather.py index 978d057..41ef18b 100644 --- a/exts/weather.py +++ b/exts/weather.py @@ -10,6 +10,8 @@ General Public License, version 2. import io import re +import aiohttp + import discord import discord.ext.commands as commands @@ -21,7 +23,7 @@ class WeatherCog(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot - self.session = bot.qrm.session + self.session = aiohttp.ClientSession(connector=bot.qrm.connector) @commands.command(name="bandconditions", aliases=['cond', 'condx', 'conditions'], category=cmn.cat.weather) async def _band_conditions(self, ctx: commands.Context): diff --git a/main.py b/main.py index c3b4ae4..573b4f3 100644 --- a/main.py +++ b/main.py @@ -11,17 +11,19 @@ General Public License, version 2. import sys import traceback +import asyncio from datetime import time, datetime import random from types import SimpleNamespace import pytz -import aiohttp import discord from discord.ext import commands, tasks +import utils.connector as conn import common as cmn + import info import data.options as opt import data.keys as keys @@ -38,13 +40,21 @@ debug_mode = opt.debug # Separate assignement in-case we define an override (te # --- Bot setup --- +# Loop/aiohttp stuff +loop = asyncio.get_event_loop() +connector = loop.run_until_complete(conn.new_connector()) + bot = commands.Bot(command_prefix=opt.prefix, description=info.description, - help_command=commands.MinimalHelpCommand()) + help_command=commands.MinimalHelpCommand(), + loop=loop, + connector=connector) +# Simple way to access bot-wide stuff in extensions. bot.qrm = SimpleNamespace() -bot.qrm.session = aiohttp.ClientSession(headers={'User-Agent': f'discord-qrm2/{info.release}'}) +# Let's store stuff here. +bot.qrm.connector = connector bot.qrm.debug_mode = debug_mode @@ -54,7 +64,6 @@ bot.qrm.debug_mode = debug_mode @commands.check(cmn.check_if_owner) async def _restart_bot(ctx: commands.Context): """Restarts the bot.""" - await bot.qrm.session.close() global exit_code await cmn.add_react(ctx.message, cmn.emojis.check_mark) print(f"[**] Restarting! Requested by {ctx.author}.") @@ -66,7 +75,6 @@ async def _restart_bot(ctx: commands.Context): @commands.check(cmn.check_if_owner) async def _shutdown_bot(ctx: commands.Context): """Shuts down the bot.""" - await bot.qrm.session.close() global exit_code await cmn.add_react(ctx.message, cmn.emojis.check_mark) print(f"[**] Shutting down! Requested by {ctx.author}.") @@ -246,6 +254,7 @@ except ConnectionResetError as ex: raise raise SystemExit("ConnectionResetError: {}".format(ex)) + # --- Exit --- # Codes for the wrapper shell script: # 0 - Clean exit, don't restart diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..c2387da --- /dev/null +++ b/utils/__init__.py @@ -0,0 +1,3 @@ +""" +Various utilities for the bot. +""" diff --git a/utils/connector.py b/utils/connector.py new file mode 100644 index 0000000..7fce782 --- /dev/null +++ b/utils/connector.py @@ -0,0 +1,16 @@ +""" +Wrapper to handle aiohttp connector creation. +--- +Copyright (C) 2020 Abigail Gold, 0x5c + +This file is part of discord-qrm2 and is released under the terms of the GNU +General Public License, version 2. +""" + + +import aiohttp + + +async def new_connector(*args, **kwargs) -> aiohttp.TCPConnector: + """*Yes, it's just a coro to instantiate a class.*""" + return aiohttp.TCPConnector(*args, **kwargs)