From 8f15feab94d862a319490925002267c63710a5fc Mon Sep 17 00:00:00 2001 From: Abigail Gold Date: Mon, 6 Jan 2020 23:27:48 -0500 Subject: [PATCH 1/4] update copyright for nuevo ans Fixes #150 --- README.md | 2 +- common.py | 2 +- exts/ae7q.py | 2 +- exts/base.py | 2 +- exts/fun.py | 2 +- exts/grid.py | 2 +- exts/ham.py | 2 +- exts/image.py | 2 +- exts/lookup.py | 2 +- exts/morse.py | 2 +- exts/qrz.py | 2 +- exts/study.py | 2 +- exts/weather.py | 2 +- info.py | 2 +- main.py | 2 +- resources/callsign_info.py | 2 +- resources/morse.py | 2 +- resources/phonetics.py | 2 +- resources/qcodes.py | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index e95facd..45736d1 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ $ run.sh ## Copyright -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-2020 Abigail Gold, 0x5c This program is released under the terms of the GNU General Public License, version 2. See `COPYING` for full license text. diff --git a/common.py b/common.py index bb46f3f..c924498 100644 --- a/common.py +++ b/common.py @@ -1,7 +1,7 @@ """ Common tools for the bot. --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-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. diff --git a/exts/ae7q.py b/exts/ae7q.py index 6671c7c..cdae837 100644 --- a/exts/ae7q.py +++ b/exts/ae7q.py @@ -1,7 +1,7 @@ """ ae7q extension for qrm --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-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. diff --git a/exts/base.py b/exts/base.py index d99cbc7..0970cf8 100644 --- a/exts/base.py +++ b/exts/base.py @@ -1,7 +1,7 @@ """ Base extension for qrm --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-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. diff --git a/exts/fun.py b/exts/fun.py index 136643d..d84f513 100644 --- a/exts/fun.py +++ b/exts/fun.py @@ -1,7 +1,7 @@ """ Fun extension for qrm --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-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. diff --git a/exts/grid.py b/exts/grid.py index 1a9366f..e8774ec 100644 --- a/exts/grid.py +++ b/exts/grid.py @@ -1,7 +1,7 @@ """ Grid extension for qrm --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-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. diff --git a/exts/ham.py b/exts/ham.py index a040ceb..71766ba 100644 --- a/exts/ham.py +++ b/exts/ham.py @@ -1,7 +1,7 @@ """ Ham extension for qrm --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-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. diff --git a/exts/image.py b/exts/image.py index c4f92d9..bcc6cc7 100644 --- a/exts/image.py +++ b/exts/image.py @@ -1,7 +1,7 @@ """ Image extension for qrm --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-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. diff --git a/exts/lookup.py b/exts/lookup.py index b078bd9..ae22e7a 100644 --- a/exts/lookup.py +++ b/exts/lookup.py @@ -1,7 +1,7 @@ """ Lookup extension for qrm --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-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. diff --git a/exts/morse.py b/exts/morse.py index 434822b..0f61650 100644 --- a/exts/morse.py +++ b/exts/morse.py @@ -1,7 +1,7 @@ """ Morse Code extension for qrm --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-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. diff --git a/exts/qrz.py b/exts/qrz.py index 5cc94b8..5492d30 100644 --- a/exts/qrz.py +++ b/exts/qrz.py @@ -1,7 +1,7 @@ """ QRZ extension for qrm --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-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. diff --git a/exts/study.py b/exts/study.py index faf40a0..3470f80 100644 --- a/exts/study.py +++ b/exts/study.py @@ -1,7 +1,7 @@ """ Study extension for qrm --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-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. diff --git a/exts/weather.py b/exts/weather.py index 978d057..1063426 100644 --- a/exts/weather.py +++ b/exts/weather.py @@ -1,7 +1,7 @@ """ Weather extension for qrm --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-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. diff --git a/info.py b/info.py index c429a44..5a442b2 100644 --- a/info.py +++ b/info.py @@ -1,7 +1,7 @@ """ Static info about the bot. --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-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. diff --git a/main.py b/main.py index c3b4ae4..9bd398c 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ """ qrm, a bot for Discord --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-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. diff --git a/resources/callsign_info.py b/resources/callsign_info.py index c8f4945..ac671f4 100644 --- a/resources/callsign_info.py +++ b/resources/callsign_info.py @@ -1,7 +1,7 @@ """ Information about callsigns for the vanity prefixes command in hamcog. --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-2020 Abigail Gold, 0x5c This file is part of discord-qrmbot and is released under the terms of the GNU General Public License, version 2. diff --git a/resources/morse.py b/resources/morse.py index 97181d6..950306c 100644 --- a/resources/morse.py +++ b/resources/morse.py @@ -1,7 +1,7 @@ """ A listing of morse code symbols --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-2020 Abigail Gold, 0x5c This file is part of discord-qrmbot and is released under the terms of the GNU General Public License, version 2. diff --git a/resources/phonetics.py b/resources/phonetics.py index 7dca02b..45bf2c7 100644 --- a/resources/phonetics.py +++ b/resources/phonetics.py @@ -1,7 +1,7 @@ """ A listing of NATO Phonetics --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-2020 Abigail Gold, 0x5c This file is part of discord-qrmbot and is released under the terms of the GNU General Public License, version 2. diff --git a/resources/qcodes.py b/resources/qcodes.py index 562a6c1..394472c 100644 --- a/resources/qcodes.py +++ b/resources/qcodes.py @@ -1,7 +1,7 @@ """ A listing of Q Codes --- -Copyright (C) 2019 Abigail Gold, 0x5c +Copyright (C) 2019-2020 Abigail Gold, 0x5c This file is part of discord-qrmbot and is released under the terms of the GNU General Public License, version 2. From 8bfaaf4af6e76158f3278d3bf8621149584b4433 Mon Sep 17 00:00:00 2001 From: 0x5c Date: Tue, 7 Jan 2020 05:36:09 -0500 Subject: [PATCH 2/4] [FIX] aiohttp DeprecationWarning - Passing a connector to Bot() - Using that connector for sessions in extensions Fixes #141 --- exts/ae7q.py | 3 ++- exts/image.py | 4 +++- exts/qrz.py | 2 +- exts/study.py | 4 +++- exts/weather.py | 4 +++- main.py | 19 ++++++++++++++----- utils/__init__.py | 3 +++ utils/connector.py | 16 ++++++++++++++++ 8 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 utils/__init__.py create mode 100644 utils/connector.py 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) From 0608a74e6cfc0c55098ef4d6a06c2ace3c667ee0 Mon Sep 17 00:00:00 2001 From: 0x5c Date: Wed, 8 Jan 2020 02:19:35 -0500 Subject: [PATCH 3/4] echo: New converters for arguemnts - Created a GlobalChannelConverter - Accepts Union[GlobalChannelConverter, UserConverter] Fixes #145 --- common.py | 34 +++++++++++++++++++++++++++------- exts/base.py | 15 +++++++++++---- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/common.py b/common.py index c924498..20a7d77 100644 --- a/common.py +++ b/common.py @@ -5,19 +5,15 @@ Copyright (C) 2019-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. ---- - -`colours`: Colours used by embeds. - -`cat`: Category names for the HelpCommand. """ import collections import json +import re import traceback -from pathlib import Path from datetime import datetime +from pathlib import Path from types import SimpleNamespace import discord @@ -26,7 +22,8 @@ import discord.ext.commands as commands import data.options as opt -__all__ = ["colours", "cat", "emojis", "embed_factory", "error_embed_factory", "add_react", "check_if_owner"] +__all__ = ["colours", "cat", "emojis", "paths", "ImageMetadata", "ImagesGroup", + "embed_factory", "error_embed_factory", "add_react", "check_if_owner"] # --- Common values --- @@ -94,6 +91,29 @@ class ImagesGroup(collections.abc.Mapping): return str(self._images) +# --- Converters --- + +class GlobalChannelConverter(commands.IDConverter): + """Converter to get any bot-acessible channel by ID/mention (global), or name (in current guild only).""" + async def convert(self, ctx: commands.Context, argument: str): + bot = ctx.bot + guild = ctx.guild + match = self._get_id_match(argument) or re.match(r'<#([0-9]+)>$', argument) + result = None + if match is None: + # not a mention/ID + if guild: + result = discord.utils.get(guild.text_channels, name=argument) + else: + raise commands.BadArgument(f"""Channel named "{argument}" not found in this guild.""") + else: + channel_id = int(match.group(1)) + result = bot.get_channel(channel_id) + if not isinstance(result, (discord.TextChannel, discord.abc.PrivateChannel)): + raise commands.BadArgument(f"""Channel "{argument}" not found.""") + return result + + # --- Helper functions --- def embed_factory(ctx: commands.Context) -> discord.Embed: diff --git a/exts/base.py b/exts/base.py index 0970cf8..eee76a2 100644 --- a/exts/base.py +++ b/exts/base.py @@ -7,17 +7,19 @@ This file is part of discord-qrm2 and is released under the terms of the GNU General Public License, version 2. """ + +import random import re from collections import OrderedDict -import random +from typing import Union import discord import discord.ext.commands as commands import info +import common as cmn import data.options as opt -import common as cmn class QrmHelpCommand(commands.HelpCommand): @@ -171,8 +173,13 @@ class BaseCog(commands.Cog): @commands.command(name="echo", aliases=["e"], hidden=True) @commands.check(cmn.check_if_owner) - async def _echo(self, ctx: commands.Context, channel: commands.TextChannelConverter, *, msg: str): - """Send a message in a channel as qrm. Only works within a server or DM to server, not between servers.""" + async def _echo(self, ctx: commands.Context, + channel: Union[cmn.GlobalChannelConverter, commands.UserConverter], *, msg: str): + """Send a message in a channel as qrm. Accepts channel/user IDs/mentions. + Channel names are current-guild only. + Does not work with the ID of the bot user.""" + if isinstance(channel, discord.ClientUser): + raise commands.BadArgument("Can't send to the bot user!") await channel.send(msg) From 8db13755bc20523328fff8803693c102cac98301 Mon Sep 17 00:00:00 2001 From: 0x5c Date: Wed, 8 Jan 2020 03:47:00 -0500 Subject: [PATCH 4/4] Added aliases for extctl and subcommands Fixes #164 --- main.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 3c90cf7..dbce811 100644 --- a/main.py +++ b/main.py @@ -82,7 +82,7 @@ async def _shutdown_bot(ctx: commands.Context): await bot.logout() -@bot.group(name="extctl", hidden=True) +@bot.group(name="extctl", aliases=["ex"], hidden=True) @commands.check(cmn.check_if_owner) async def _extctl(ctx: commands.Context): """Extension control commands. @@ -92,7 +92,7 @@ async def _extctl(ctx: commands.Context): await ctx.invoke(cmd) -@_extctl.command(name="list") +@_extctl.command(name="list", aliases=["ls"]) async def _extctl_list(ctx: commands.Context): """Lists Extensions.""" embed = cmn.embed_factory(ctx) @@ -101,7 +101,7 @@ async def _extctl_list(ctx: commands.Context): await ctx.send(embed=embed) -@_extctl.command(name="load") +@_extctl.command(name="load", aliases=["ld"]) async def _extctl_load(ctx: commands.Context, extension: str): try: bot.load_extension(ext_dir + "." + extension) @@ -111,7 +111,7 @@ async def _extctl_load(ctx: commands.Context, extension: str): await ctx.send(embed=embed) -@_extctl.command(name="reload", aliases=["relaod"]) +@_extctl.command(name="reload", aliases=["rl", "r", "relaod"]) async def _extctl_reload(ctx: commands.Context, extension: str): if ctx.invoked_with == "relaod": pika = bot.get_emoji(opt.pika) @@ -125,7 +125,7 @@ async def _extctl_reload(ctx: commands.Context, extension: str): await ctx.send(embed=embed) -@_extctl.command(name="unload") +@_extctl.command(name="unload", aliases=["ul"]) async def _extctl_unload(ctx: commands.Context, extension: str): try: bot.unload_extension(ext_dir + "." + extension)