qrm2/cogs/basecog.py

115 lines
4.2 KiB
Python
Raw Normal View History

"""
2019-10-04 19:45:59 -04:00
Base cog for qrm
---
2019-10-05 02:23:11 -04:00
Copyright (C) 2019 Abigail Gold, 0x5c
2019-10-04 18:05:57 -04:00
2019-10-05 02:23:11 -04:00
This file is part of discord-qrmbot and is released under the terms of the GNU
General Public License, version 2.
"""
from datetime import datetime
import re
from collections import OrderedDict
2019-11-09 02:17:48 -05:00
import random
import discord
import discord.ext.commands as commands
2019-10-04 18:16:47 -04:00
class BaseCog(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.gs = bot.get_cog("GlobalSettings")
2019-11-06 00:44:39 -05:00
self.changelog = parse_changelog()
@commands.command(name="info", aliases=["about"])
async def _info(self, ctx):
"""Shows info about qrm."""
embed = discord.Embed(title="About qrm",
description=self.gs.info.description,
colour=self.gs.colours.neutral,
timestamp=datetime.utcnow())
embed.set_footer(text=ctx.author.name,
icon_url=str(ctx.author.avatar_url))
embed = embed.add_field(name="Authors", value=", ".join(self.gs.info.authors))
embed = embed.add_field(name="Contributing", value=self.gs.info.contributing)
embed = embed.add_field(name="License", value=self.gs.info.license)
await ctx.send(embed=embed)
2019-10-04 19:48:58 -04:00
@commands.command(name="ping")
async def _ping(self, ctx: commands.Context):
"""Show the current latency to the discord endpoint."""
2019-11-09 02:17:48 -05:00
content = ''
if random.random() < 0.05:
content = f'<@{ctx.message.author.id}>'
embed = discord.Embed(title="**Pong!**",
description=f'Current ping is {self.bot.latency*1000:.1f} ms',
colour=self.gs.colours.neutral,
timestamp=datetime.utcnow())
embed.set_footer(text=ctx.author.name,
icon_url=str(ctx.author.avatar_url))
2019-11-09 02:17:48 -05:00
await ctx.send(content=content, embed=embed)
2019-10-04 19:45:59 -04:00
@commands.command(name="changelog", aliases=["clog"])
async def _changelog(self, ctx: commands.Context):
"""Show what has changed in recent bot versions."""
embed = discord.Embed(title="qrm Changelog",
2019-10-30 15:31:17 -04:00
description=("For a full listing, visit [Github](https://"
"github.com/classabbyamp/discord-qrm-bot/blob/master/CHANGELOG.md)."),
colour=self.gs.colours.neutral,
timestamp=datetime.utcnow())
embed.set_footer(text=ctx.author.name,
icon_url=str(ctx.author.avatar_url))
2019-11-06 00:44:39 -05:00
changelog = self.changelog
2019-10-30 15:26:29 -04:00
vers = 0
for ver, log in changelog.items():
2019-10-30 15:26:29 -04:00
if ver.lower() != 'unreleased':
if 'date' in log:
header = f'**{ver}** ({log["date"]})'
else:
header = f'**{ver}**'
embed.add_field(name=header, value=await format_changelog(log), inline=False)
vers += 1
if vers >= 2:
break
await ctx.send(embed=embed)
2019-11-06 00:44:39 -05:00
def parse_changelog():
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())
ver = ver_match.group(1)
changelog[ver] = dict()
if ver_match.group(2):
changelog[ver]['date'] = ver_match.group(2)
elif re.match(r'###[^#]', line):
heading = line.lstrip('#').strip()
changelog[ver][heading] = []
elif ver != '' and heading != '':
changelog[ver][heading].append(line.lstrip('-').strip())
return changelog
async def format_changelog(log: dict):
formatted = ''
for header, lines in log.items():
if header != 'date':
formatted += f'**{header}**\n'
for line in lines:
formatted += f'- {line}\n'
return formatted
2019-10-04 19:45:59 -04:00
def setup(bot: commands.Bot):
2019-10-04 18:16:47 -04:00
bot.add_cog(BaseCog(bot))