2019-12-06 01:19:42 -05:00
|
|
|
"""
|
|
|
|
Common tools for the bot.
|
|
|
|
---
|
|
|
|
Copyright (C) 2019 Abigail Gold, 0x5c
|
|
|
|
|
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.
|
|
|
|
---
|
|
|
|
|
|
|
|
`colours`: Colours used by embeds.
|
|
|
|
|
|
|
|
`cat`: Category names for the HelpCommand.
|
|
|
|
"""
|
|
|
|
|
2019-12-07 11:38:34 -05:00
|
|
|
|
|
|
|
import traceback
|
|
|
|
from datetime import datetime
|
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
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ["colours", "cat", "emojis", "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
|
|
|
|
|
|
|
emojis = SimpleNamespace(good='✅',
|
|
|
|
bad='❌')
|
|
|
|
|
|
|
|
|
|
|
|
# --- 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)
|
|
|
|
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
|
|
|
|
await add_react(ctx.message, emojis.bad)
|
|
|
|
return False
|