2019-12-06 01:19:42 -05:00
|
|
|
"""
|
|
|
|
Common tools for the bot.
|
|
|
|
---
|
2020-01-06 23:27:48 -05:00
|
|
|
Copyright (C) 2019-2020 Abigail Gold, 0x5c
|
2019-12-06 01:19:42 -05:00
|
|
|
|
2020-02-15 06:27:48 -05:00
|
|
|
This file is part of qrm2 and is released under the terms of
|
2020-01-31 06:50:50 -05:00
|
|
|
the GNU General Public License, version 2.
|
2019-12-06 01:19:42 -05:00
|
|
|
"""
|
|
|
|
|
2019-12-07 11:38:34 -05:00
|
|
|
|
2019-12-23 20:51:31 -05:00
|
|
|
import collections
|
|
|
|
import json
|
2020-01-08 02:19:35 -05:00
|
|
|
import re
|
2019-12-07 11:38:34 -05:00
|
|
|
import traceback
|
|
|
|
from datetime import datetime
|
2020-01-08 02:19:35 -05:00
|
|
|
from pathlib import Path
|
2019-12-06 01:19:42 -05:00
|
|
|
from types import SimpleNamespace
|
|
|
|
|
2020-01-27 00:37:52 -05:00
|
|
|
import aiohttp
|
|
|
|
|
2019-12-07 11:38:34 -05:00
|
|
|
import discord
|
|
|
|
import discord.ext.commands as commands
|
|
|
|
|
2019-12-08 04:10:19 -05:00
|
|
|
import data.options as opt
|
|
|
|
|
|
|
|
|
2020-01-08 02:19:35 -05:00
|
|
|
__all__ = ["colours", "cat", "emojis", "paths", "ImageMetadata", "ImagesGroup",
|
|
|
|
"embed_factory", "error_embed_factory", "add_react", "check_if_owner"]
|
2019-12-07 11:38:34 -05:00
|
|
|
|
|
|
|
|
|
|
|
# --- Common values ---
|
2019-12-06 01:19:42 -05:00
|
|
|
|
2020-01-30 06:55:11 -05:00
|
|
|
colours = SimpleNamespace(
|
|
|
|
good=0x43B581,
|
|
|
|
neutral=0x7289DA,
|
|
|
|
bad=0xF04747,
|
|
|
|
)
|
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
# meow
|
2020-01-30 06:55:11 -05:00
|
|
|
cat = SimpleNamespace(
|
|
|
|
lookup="Information Lookup",
|
|
|
|
fun="Fun",
|
|
|
|
maps="Mapping",
|
|
|
|
ref="Reference",
|
|
|
|
study="Exam Study",
|
|
|
|
weather="Land and Space Weather",
|
|
|
|
admin="Bot Control",
|
|
|
|
)
|
|
|
|
|
|
|
|
emojis = SimpleNamespace(
|
|
|
|
check_mark="✅",
|
|
|
|
x="❌",
|
|
|
|
warning="⚠️",
|
|
|
|
question="❓",
|
|
|
|
no_entry="⛔",
|
|
|
|
bangbang="‼️",
|
|
|
|
a="🇦",
|
|
|
|
b="🇧",
|
|
|
|
c="🇨",
|
|
|
|
d="🇩",
|
|
|
|
)
|
|
|
|
|
|
|
|
paths = SimpleNamespace(
|
|
|
|
data=Path("./data/"),
|
|
|
|
resources=Path("./resources/"),
|
2020-09-27 16:07:21 -04:00
|
|
|
img=Path("./resources/img/"),
|
2020-01-30 06:55:11 -05:00
|
|
|
bandcharts=Path("./resources/img/bandcharts/"),
|
|
|
|
maps=Path("./resources/img/maps/"),
|
|
|
|
)
|
2019-12-23 20:51:31 -05:00
|
|
|
|
|
|
|
|
|
|
|
# --- Classes ---
|
|
|
|
|
2020-09-24 19:02:12 -04:00
|
|
|
|
|
|
|
class CallsignInfoData:
|
|
|
|
"""Represents a country's callsign info"""
|
|
|
|
def __init__(self, data: list):
|
|
|
|
self.title: str = data[0]
|
|
|
|
self.desc: str = data[1]
|
|
|
|
self.calls: str = data[2]
|
|
|
|
self.emoji: str = data[3]
|
|
|
|
|
|
|
|
|
2019-12-23 20:51:31 -05:00
|
|
|
class ImageMetadata:
|
|
|
|
"""Represents the metadata of a single image."""
|
|
|
|
def __init__(self, metadata: list):
|
|
|
|
self.filename: str = metadata[0]
|
|
|
|
self.name: str = metadata[1]
|
|
|
|
self.long_name: str = metadata[2]
|
|
|
|
self.description: str = metadata[3]
|
|
|
|
self.source: str = metadata[4]
|
|
|
|
self.emoji: str = metadata[5]
|
|
|
|
|
|
|
|
|
|
|
|
class ImagesGroup(collections.abc.Mapping):
|
|
|
|
"""Represents a group of images, loaded from a meta.json file."""
|
|
|
|
def __init__(self, file_path):
|
|
|
|
self._images = {}
|
|
|
|
self.path = file_path
|
|
|
|
|
|
|
|
with open(file_path, "r") as file:
|
|
|
|
images: dict = json.load(file)
|
|
|
|
for key, imgdata in images.items():
|
|
|
|
self._images[key] = ImageMetadata(imgdata)
|
|
|
|
|
|
|
|
# Wrappers to implement dict-like functionality
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._images)
|
|
|
|
|
|
|
|
def __getitem__(self, key: str):
|
|
|
|
return self._images[key]
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self._images)
|
|
|
|
|
|
|
|
# str(): Simply return what it would be for the underlaying dict
|
|
|
|
def __str__(self):
|
|
|
|
return str(self._images)
|
|
|
|
|
2019-12-07 11:38:34 -05:00
|
|
|
|
2020-01-27 00:37:52 -05:00
|
|
|
# --- Exceptions ---
|
|
|
|
|
|
|
|
class BotHTTPError(Exception):
|
|
|
|
"""Raised whan a requests fails (status != 200) in a command."""
|
|
|
|
def __init__(self, response: aiohttp.ClientResponse):
|
|
|
|
msg = f"Request failed: {response.status} {response.reason}"
|
|
|
|
super().__init__(msg)
|
|
|
|
self.response = response
|
|
|
|
self.status = response.status
|
|
|
|
self.reason = response.reason
|
|
|
|
|
|
|
|
|
2020-01-08 02:19:35 -05:00
|
|
|
# --- 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
|
2020-01-30 06:15:42 -05:00
|
|
|
match = self._get_id_match(argument) or re.match(r"<#([0-9]+)>$", argument)
|
2020-01-08 02:19:35 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-12-07 11:38:34 -05:00
|
|
|
# --- Helper functions ---
|
|
|
|
|
2019-12-16 03:49:34 -05:00
|
|
|
def embed_factory(ctx: commands.Context) -> discord.Embed:
|
|
|
|
"""Creates an embed with neutral colour and standard footer."""
|
|
|
|
embed = discord.Embed(timestamp=datetime.utcnow(), colour=colours.neutral)
|
|
|
|
embed.set_footer(text=ctx.author, icon_url=str(ctx.author.avatar_url))
|
|
|
|
return embed
|
|
|
|
|
|
|
|
|
2019-12-07 11:38:34 -05:00
|
|
|
def error_embed_factory(ctx: commands.Context, exception: Exception, debug_mode: bool) -> discord.Embed:
|
|
|
|
"""Creates an Error embed."""
|
|
|
|
if debug_mode:
|
|
|
|
fmtd_ex = traceback.format_exception(exception.__class__, exception, exception.__traceback__)
|
|
|
|
else:
|
|
|
|
fmtd_ex = traceback.format_exception_only(exception.__class__, exception)
|
2019-12-16 03:49:34 -05:00
|
|
|
embed = embed_factory(ctx)
|
2020-01-03 23:03:17 -05:00
|
|
|
embed.title = "⚠️ Error"
|
2020-01-30 06:15:42 -05:00
|
|
|
embed.description = "```\n" + "\n".join(fmtd_ex) + "```"
|
2019-12-16 03:49:34 -05:00
|
|
|
embed.colour = colours.bad
|
2019-12-07 11:38:34 -05:00
|
|
|
return embed
|
2019-12-08 04:10:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
async def add_react(msg: discord.Message, react: str):
|
|
|
|
try:
|
|
|
|
await msg.add_reaction(react)
|
|
|
|
except discord.Forbidden:
|
|
|
|
print(f"[!!] Missing permissions to add reaction in '{msg.guild.id}/{msg.channel.id}'!")
|
|
|
|
|
|
|
|
|
|
|
|
# --- Checks ---
|
|
|
|
|
|
|
|
async def check_if_owner(ctx: commands.Context):
|
|
|
|
if ctx.author.id in opt.owners_uids:
|
|
|
|
return True
|
2020-01-03 23:03:17 -05:00
|
|
|
raise commands.NotOwner
|