mirror of
				https://github.com/miaowware/qrm2.git
				synced 2025-10-26 09:50:20 -04:00 
			
		
		
		
	Merge pull request #163 from classabbyamp/async-session
[FIX] aiohttp DeprecationWarning
This commit is contained in:
		
						commit
						60764cd733
					
				| @ -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): | ||||
|  | ||||
| @ -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 = ''): | ||||
|  | ||||
| @ -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) | ||||
|  | ||||
| @ -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): | ||||
|  | ||||
| @ -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
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								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 | ||||
|  | ||||
							
								
								
									
										3
									
								
								utils/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								utils/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | ||||
| """ | ||||
| Various utilities for the bot. | ||||
| """ | ||||
							
								
								
									
										16
									
								
								utils/connector.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								utils/connector.py
									
									
									
									
									
										Normal 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) | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user