[FIX] aiohttp DeprecationWarning

- Passing a connector to Bot()
- Using that connector for sessions in extensions

Fixes #141
This commit is contained in:
0x5c 2020-01-07 05:36:09 -05:00
parent 4e73fa3734
commit 8bfaaf4af6
No known key found for this signature in database
GPG Key ID: 82039FC95E3FE970
8 changed files with 45 additions and 10 deletions

View File

@ -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):

View File

@ -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 = ''):

View File

@ -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)

View File

@ -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):

View File

@ -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):

19
main.py
View File

@ -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

3
utils/__init__.py Normal file
View File

@ -0,0 +1,3 @@
"""
Various utilities for the bot.
"""

16
utils/connector.py Normal file
View File

@ -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)