2019-10-04 13:10:27 -04:00
|
|
|
"""
|
2019-12-07 17:26:55 -05:00
|
|
|
Base extension for qrm
|
2019-10-04 13:10:27 -04:00
|
|
|
---
|
2020-01-06 23:27:48 -05:00
|
|
|
Copyright (C) 2019-2020 Abigail Gold, 0x5c
|
2019-10-04 18:05:57 -04: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-10-05 02:23:11 -04:00
|
|
|
General Public License, version 2.
|
2019-10-04 13:10:27 -04:00
|
|
|
"""
|
|
|
|
|
2020-01-08 02:19:35 -05:00
|
|
|
|
|
|
|
import random
|
2019-10-29 13:54:48 -04:00
|
|
|
import re
|
|
|
|
from collections import OrderedDict
|
2020-01-08 02:19:35 -05:00
|
|
|
from typing import Union
|
2019-10-18 08:27:05 -04:00
|
|
|
|
2019-10-04 13:10:27 -04:00
|
|
|
import discord
|
|
|
|
import discord.ext.commands as commands
|
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
import info
|
2020-01-08 02:19:35 -05:00
|
|
|
import common as cmn
|
2019-12-06 01:19:42 -05:00
|
|
|
|
2019-12-07 17:13:06 -05:00
|
|
|
import data.options as opt
|
2019-12-06 01:19:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
class QrmHelpCommand(commands.HelpCommand):
|
|
|
|
def __init__(self):
|
2019-12-25 02:48:59 -05:00
|
|
|
super().__init__(command_attrs={'help': 'Shows help about qrm or a command', 'aliases': ['h']})
|
2020-01-21 22:21:30 -05:00
|
|
|
self.verify_checks = True
|
2019-12-06 01:19:42 -05:00
|
|
|
|
2020-01-21 22:21:30 -05:00
|
|
|
async def get_bot_mapping(self):
|
2019-12-06 01:19:42 -05:00
|
|
|
bot = self.context.bot
|
|
|
|
mapping = {}
|
2020-01-21 22:21:30 -05:00
|
|
|
|
|
|
|
for cmd in await self.filter_commands(bot.commands, sort=True):
|
2019-12-06 01:19:42 -05:00
|
|
|
cat = cmd.__original_kwargs__.get('category', None)
|
|
|
|
if cat in mapping:
|
|
|
|
mapping[cat].append(cmd)
|
|
|
|
else:
|
|
|
|
mapping[cat] = [cmd]
|
|
|
|
return mapping
|
|
|
|
|
2020-01-21 22:21:30 -05:00
|
|
|
async def get_command_signature(self, command):
|
2019-12-06 01:19:42 -05:00
|
|
|
parent = command.full_parent_name
|
|
|
|
if command.aliases != []:
|
|
|
|
aliases = ', '.join(command.aliases)
|
|
|
|
fmt = command.name
|
|
|
|
if parent:
|
|
|
|
fmt = f'{parent} {fmt}'
|
|
|
|
alias = fmt
|
|
|
|
return f'{opt.prefix}{alias} {command.signature}\n *Aliases:* {aliases}'
|
|
|
|
alias = command.name if not parent else f'{parent} {command.name}'
|
|
|
|
return f'{opt.prefix}{alias} {command.signature}'
|
|
|
|
|
|
|
|
async def send_error_message(self, error):
|
2019-12-16 03:49:34 -05:00
|
|
|
embed = cmn.embed_factory(self.context)
|
|
|
|
embed.title = 'qrm Help Error'
|
|
|
|
embed.description = error
|
|
|
|
embed.colour = cmn.colours.bad
|
2019-12-06 01:19:42 -05:00
|
|
|
await self.context.send(embed=embed)
|
|
|
|
|
|
|
|
async def send_bot_help(self, mapping):
|
2019-12-16 03:49:34 -05:00
|
|
|
embed = cmn.embed_factory(self.context)
|
|
|
|
embed.title = 'qrm Help'
|
|
|
|
embed.description = (f'For command-specific help and usage, use `{opt.prefix}help [command name]`'
|
|
|
|
'. Many commands have shorter aliases.')
|
2020-01-21 22:21:30 -05:00
|
|
|
mapping = await mapping
|
2019-12-06 01:19:42 -05:00
|
|
|
|
|
|
|
for cat, cmds in mapping.items():
|
|
|
|
if cmds == []:
|
|
|
|
continue
|
|
|
|
names = sorted([cmd.name for cmd in cmds])
|
|
|
|
if cat is not None:
|
|
|
|
embed.add_field(name=cat.title(), value=', '.join(names), inline=False)
|
|
|
|
else:
|
|
|
|
embed.add_field(name='Other', value=', '.join(names), inline=False)
|
|
|
|
await self.context.send(embed=embed)
|
|
|
|
|
|
|
|
async def send_command_help(self, command):
|
2020-01-21 22:21:30 -05:00
|
|
|
if self.verify_checks and not await command.can_run(self.context):
|
|
|
|
raise commands.CheckFailure
|
|
|
|
return
|
|
|
|
embed = cmn.embed_factory(self.context)
|
|
|
|
embed.title = await self.get_command_signature(command)
|
|
|
|
embed.description = command.help
|
|
|
|
await self.context.send(embed=embed)
|
2019-12-06 01:19:42 -05:00
|
|
|
|
|
|
|
async def send_group_help(self, group):
|
2020-01-21 22:21:30 -05:00
|
|
|
if self.verify_checks and not await group.can_run(self.context):
|
|
|
|
raise commands.CheckFailure
|
|
|
|
return
|
|
|
|
embed = cmn.embed_factory(self.context)
|
|
|
|
embed.title = await self.get_command_signature(group)
|
|
|
|
embed.description = group.help
|
|
|
|
for cmd in await self.filter_commands(group.commands, sort=True):
|
|
|
|
embed.add_field(name=await self.get_command_signature(cmd), value=cmd.help, inline=False)
|
|
|
|
await self.context.send(embed=embed)
|
2019-12-06 01:19:42 -05:00
|
|
|
|
2019-10-04 13:10:27 -04:00
|
|
|
|
2019-10-04 18:16:47 -04:00
|
|
|
class BaseCog(commands.Cog):
|
2019-10-18 08:27:05 -04:00
|
|
|
def __init__(self, bot: commands.Bot):
|
2019-10-04 13:10:27 -04:00
|
|
|
self.bot = bot
|
2019-11-06 00:44:39 -05:00
|
|
|
self.changelog = parse_changelog()
|
2019-10-04 13:10:27 -04:00
|
|
|
|
|
|
|
@commands.command(name="info", aliases=["about"])
|
2019-12-06 01:19:42 -05:00
|
|
|
async def _info(self, ctx: commands.Context):
|
2019-10-04 13:10:27 -04:00
|
|
|
"""Shows info about qrm."""
|
2019-12-16 03:49:34 -05:00
|
|
|
embed = cmn.embed_factory(ctx)
|
|
|
|
embed.title = "About qrm"
|
|
|
|
embed.description = info.description
|
2019-12-06 01:19:42 -05:00
|
|
|
|
2019-12-23 09:53:49 -05:00
|
|
|
embed.add_field(name="Authors", value=", ".join(info.authors))
|
|
|
|
embed.add_field(name="License", value=info.license)
|
|
|
|
embed.add_field(name="Version", value=f'v{info.release}')
|
|
|
|
embed.add_field(name="Contributing", value=info.contributing, inline=False)
|
2019-12-24 21:04:59 -05:00
|
|
|
embed.add_field(name="Official Server", value=info.bot_server, inline=False)
|
2019-11-11 21:13:43 -05:00
|
|
|
embed.set_thumbnail(url=str(self.bot.user.avatar_url))
|
2019-10-04 13:10:27 -04:00
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
2020-01-04 01:40:17 -05:00
|
|
|
@commands.command(name="ping", aliases=['beep'])
|
2019-10-18 08:27:05 -04:00
|
|
|
async def _ping(self, ctx: commands.Context):
|
|
|
|
"""Show the current latency to the discord endpoint."""
|
2019-12-16 03:49:34 -05:00
|
|
|
embed = cmn.embed_factory(ctx)
|
2020-01-04 01:40:17 -05:00
|
|
|
content = ''
|
|
|
|
if ctx.invoked_with == "beep":
|
|
|
|
embed.title = "**Boop!**"
|
|
|
|
else:
|
|
|
|
content = ctx.message.author.mention if random.random() < 0.05 else ''
|
|
|
|
embed.title = "🏓 **Pong!**"
|
2019-12-16 03:49:34 -05:00
|
|
|
embed.description = f'Current ping is {self.bot.latency*1000:.1f} ms'
|
2020-01-04 01:40:17 -05:00
|
|
|
await ctx.send(content, embed=embed)
|
2019-10-04 19:45:59 -04:00
|
|
|
|
2019-10-29 13:54:48 -04:00
|
|
|
@commands.command(name="changelog", aliases=["clog"])
|
2020-01-04 15:24:33 -05:00
|
|
|
async def _changelog(self, ctx: commands.Context, version: str = 'latest'):
|
2020-01-04 16:36:47 -05:00
|
|
|
"""Show what has changed in a bot version."""
|
2019-12-16 03:49:34 -05:00
|
|
|
embed = cmn.embed_factory(ctx)
|
|
|
|
embed.title = "qrm Changelog"
|
|
|
|
embed.description = ("For a full listing, visit [Github](https://"
|
|
|
|
"github.com/classabbyamp/discord-qrm2/blob/master/CHANGELOG.md).")
|
2019-11-06 00:44:39 -05:00
|
|
|
changelog = self.changelog
|
2020-01-04 15:24:33 -05:00
|
|
|
vers = list(changelog.keys())
|
|
|
|
vers.remove("Unreleased")
|
|
|
|
|
|
|
|
version = version.lower()
|
|
|
|
|
|
|
|
if version == 'latest':
|
|
|
|
version = info.release
|
|
|
|
if version == 'unreleased':
|
|
|
|
version = 'Unreleased'
|
|
|
|
|
|
|
|
try:
|
|
|
|
log = changelog[version]
|
|
|
|
except KeyError:
|
|
|
|
embed.title += ": Version Not Found"
|
|
|
|
embed.description += '\n\n**Valid versions:** latest, '
|
|
|
|
embed.description += ', '.join(vers)
|
|
|
|
embed.colour = cmn.colours.bad
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
return
|
|
|
|
|
|
|
|
if 'date' in log:
|
|
|
|
embed.description += f'\n\n**v{version}** ({log["date"]})'
|
|
|
|
else:
|
|
|
|
embed.description += f'\n\n**v{version}**'
|
|
|
|
embed = await format_changelog(log, embed)
|
2019-10-29 13:54:48 -04:00
|
|
|
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
2020-01-04 01:49:37 -05:00
|
|
|
@commands.command(name="issue")
|
|
|
|
async def _issue(self, ctx: commands.Context):
|
|
|
|
"""Shows how to create an issue for the bot."""
|
|
|
|
embed = cmn.embed_factory(ctx)
|
|
|
|
embed.title = "Found a bug? Have a feature request?"
|
|
|
|
embed.description = ("Submit an issue on the [issue tracker]"
|
|
|
|
"(https://github.com/classabbyamp/discord-qrm2/issues)!")
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
2020-01-21 22:21:30 -05:00
|
|
|
@commands.command(name="echo", aliases=["e"], category=cmn.cat.admin)
|
2019-12-10 00:47:40 -05:00
|
|
|
@commands.check(cmn.check_if_owner)
|
2020-01-08 02:19:35 -05:00
|
|
|
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!")
|
2019-12-10 00:47:40 -05:00
|
|
|
await channel.send(msg)
|
|
|
|
|
2019-10-29 13:54:48 -04:00
|
|
|
|
2019-11-06 00:44:39 -05:00
|
|
|
def parse_changelog():
|
2019-10-29 13:54:48 -04:00
|
|
|
changelog = OrderedDict()
|
|
|
|
ver = ''
|
|
|
|
heading = ''
|
|
|
|
|
|
|
|
with open('CHANGELOG.md') as changelog_file:
|
|
|
|
for line in changelog_file.readlines():
|
|
|
|
if line.strip() == '':
|
|
|
|
continue
|
|
|
|
if re.match(r'##[^#]', line):
|
|
|
|
ver_match = re.match(r'\[(.+)\](?: - )?(\d{4}-\d{2}-\d{2})?', line.lstrip('#').strip())
|
2019-12-16 00:15:37 -05:00
|
|
|
if ver_match is not None:
|
|
|
|
ver = ver_match.group(1)
|
|
|
|
changelog[ver] = dict()
|
|
|
|
if ver_match.group(2):
|
|
|
|
changelog[ver]['date'] = ver_match.group(2)
|
2019-10-29 13:54:48 -04:00
|
|
|
elif re.match(r'###[^#]', line):
|
|
|
|
heading = line.lstrip('#').strip()
|
|
|
|
changelog[ver][heading] = []
|
|
|
|
elif ver != '' and heading != '':
|
2019-12-16 00:02:21 -05:00
|
|
|
if line.startswith('-'):
|
|
|
|
changelog[ver][heading].append(line.lstrip('-').strip())
|
2019-10-29 13:54:48 -04:00
|
|
|
return changelog
|
|
|
|
|
|
|
|
|
2019-12-15 19:10:03 -05:00
|
|
|
async def format_changelog(log: dict, embed: discord.Embed):
|
2019-10-29 13:54:48 -04:00
|
|
|
for header, lines in log.items():
|
2019-12-15 19:10:03 -05:00
|
|
|
formatted = ''
|
2019-10-29 13:54:48 -04:00
|
|
|
if header != 'date':
|
|
|
|
for line in lines:
|
|
|
|
formatted += f'- {line}\n'
|
2019-12-15 19:10:03 -05:00
|
|
|
embed.add_field(name=f'**{header}**', value=formatted, inline=False)
|
|
|
|
return embed
|
2019-10-29 13:54:48 -04:00
|
|
|
|
2019-10-04 19:45:59 -04:00
|
|
|
|
2019-10-18 08:27:05 -04:00
|
|
|
def setup(bot: commands.Bot):
|
2019-10-04 18:16:47 -04:00
|
|
|
bot.add_cog(BaseCog(bot))
|
2019-12-06 01:19:42 -05:00
|
|
|
bot._original_help_command = bot.help_command
|
|
|
|
bot.help_command = QrmHelpCommand()
|
|
|
|
|
|
|
|
|
|
|
|
def teardown(bot: commands.Bot):
|
|
|
|
bot.help_command = bot._original_help_command
|