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
|
|
|
|
2019-12-08 15:35:58 -05:00
|
|
|
This file is part of discord-qrm2 and is released under the terms of the GNU
|
2019-12-06 01:19:42 -05:00
|
|
|
General Public License, version 2.
|
|
|
|
"""
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
colours = SimpleNamespace(good=0x43B581,
|
|
|
|
neutral=0x7289DA,
|
|
|
|
bad=0xF04747)
|
|
|
|
# meow
|
|
|
|
cat = SimpleNamespace(lookup='Information Lookup',
|
|
|
|
fun='Fun',
|
|
|
|
maps='Mapping',
|
|
|
|
ref='Reference',
|
|
|
|
study='Exam Study',
|
|
|
|
weather='Land and Space Weather')
|
2019-12-07 11:38:34 -05:00
|
|
|
|
2020-01-03 23:03:17 -05:00
|
|
|
emojis = SimpleNamespace(check_mark='✅',
|
|
|
|
x='❌',
|
|
|
|
warning='⚠️',
|
|
|
|
question='❓',
|
|
|
|
no_entry='⛔',
|
2020-01-20 20:50:55 -05:00
|
|
|
bangbang='‼️',
|
|
|
|
a='🇦',
|
|
|
|
b='🇧',
|
|
|
|
c='🇨',
|
|
|
|
d='🇩')
|
2019-12-07 11:38:34 -05:00
|
|
|
|
2019-12-23 20:51:31 -05:00
|
|
|
paths = SimpleNamespace(data=Path("./data/"),
|
|
|
|
resources=Path("./resources/"),
|
|
|
|
bandcharts=Path("./resources/img/bandcharts/"),
|
|
|
|
maps=Path("./resources/img/maps/"))
|
|
|
|
|
|
|
|
|
|
|
|
# --- Classes ---
|
|
|
|
|
|
|
|
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-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
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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"
|
2019-12-07 11:38:34 -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
|