From 12a7f9bbacec70931c8dbe20318934a35c2520c6 Mon Sep 17 00:00:00 2001 From: Abigail Gold Date: Tue, 29 Oct 2019 13:54:48 -0400 Subject: [PATCH 1/4] first try at implementing a changelog command. This is unfinished but functional. --- cogs/basecog.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/cogs/basecog.py b/cogs/basecog.py index 5556ff9..60d95e1 100644 --- a/cogs/basecog.py +++ b/cogs/basecog.py @@ -8,6 +8,8 @@ General Public License, version 2. """ from datetime import datetime +import re +from collections import OrderedDict import discord import discord.ext.commands as commands @@ -43,6 +45,54 @@ class BaseCog(commands.Cog): icon_url=str(ctx.author.avatar_url)) await ctx.send(embed=embed) + @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", + colour=self.gs.colours.neutral, + timestamp=datetime.utcnow()) + embed.set_footer(text=ctx.author.name, + icon_url=str(ctx.author.avatar_url)) + changelog = await parse_changelog() + + for ver, log in changelog.items(): + embed.add_field(name=ver, value=await format_changelog(log), inline=False) + + await ctx.send(embed=embed) + + +async 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 + def setup(bot: commands.Bot): bot.add_cog(BaseCog(bot)) From 94109b7a947df85be13305e9e5514d5599a9ae8c Mon Sep 17 00:00:00 2001 From: Abigail Gold Date: Wed, 30 Oct 2019 15:26:29 -0400 Subject: [PATCH 2/4] changelog command finished --- CHANGELOG.md | 1 + cogs/basecog.py | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a15db8..f44c0cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Rich lookup for QRZ, if a QRZ subscription is present - Timestamp and requester username and avatar are now shown on embeds - Current and 3-Day Forecast terrestrial weather conditions lookup commands +- Changelog command ### Changed - Rewrote code to take advantage of discord.py's cogs - Moved most bot responses into embeds diff --git a/cogs/basecog.py b/cogs/basecog.py index 60d95e1..1a3de11 100644 --- a/cogs/basecog.py +++ b/cogs/basecog.py @@ -49,14 +49,24 @@ class BaseCog(commands.Cog): async def _changelog(self, ctx: commands.Context): """Show what has changed in recent bot versions.""" embed = discord.Embed(title="qrm Changelog", + 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)) changelog = await parse_changelog() + vers = 0 for ver, log in changelog.items(): - embed.add_field(name=ver, value=await format_changelog(log), inline=False) + 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) From a7578d5b8cebbe0426cc965dd03bd8519a3ec04c Mon Sep 17 00:00:00 2001 From: Abigail Gold Date: Wed, 30 Oct 2019 15:31:17 -0400 Subject: [PATCH 3/4] PEP8 fixes --- cogs/basecog.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cogs/basecog.py b/cogs/basecog.py index 1a3de11..4c4fb2b 100644 --- a/cogs/basecog.py +++ b/cogs/basecog.py @@ -49,7 +49,8 @@ class BaseCog(commands.Cog): async def _changelog(self, ctx: commands.Context): """Show what has changed in recent bot versions.""" embed = discord.Embed(title="qrm Changelog", - description="For a full listing, visit [Github](https://github.com/classabbyamp/discord-qrm-bot/blob/master/CHANGELOG.md).", + 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, From 7cd231b0434f3c007af9ffa767741c6a948d2f7b Mon Sep 17 00:00:00 2001 From: Abigail Gold Date: Wed, 6 Nov 2019 00:44:39 -0500 Subject: [PATCH 4/4] load changelog at cog load --- cogs/basecog.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cogs/basecog.py b/cogs/basecog.py index 4c4fb2b..a133348 100644 --- a/cogs/basecog.py +++ b/cogs/basecog.py @@ -19,6 +19,7 @@ class BaseCog(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot self.gs = bot.get_cog("GlobalSettings") + self.changelog = parse_changelog() @commands.command(name="info", aliases=["about"]) async def _info(self, ctx): @@ -55,7 +56,7 @@ class BaseCog(commands.Cog): timestamp=datetime.utcnow()) embed.set_footer(text=ctx.author.name, icon_url=str(ctx.author.avatar_url)) - changelog = await parse_changelog() + changelog = self.changelog vers = 0 for ver, log in changelog.items(): @@ -72,7 +73,7 @@ class BaseCog(commands.Cog): await ctx.send(embed=embed) -async def parse_changelog(): +def parse_changelog(): changelog = OrderedDict() ver = '' heading = ''