From 5ebcebcac2a81177ed6dba7beabc408aedc62866 Mon Sep 17 00:00:00 2001 From: Abigail Gold <5366828+classabbyamp@users.noreply.github.com> Date: Tue, 8 Oct 2019 18:39:05 -0400 Subject: [PATCH 1/3] updated embed colors, and linted some of main.py (#17) * updated embed colors, and linted some of main.py * fixed spacing, comments * fixed line continuations ;3 * Fix spacing --- main.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index d4ac98c..a715063 100644 --- a/main.py +++ b/main.py @@ -34,14 +34,17 @@ class GlobalSettings(commands.Cog): self.keys = keys self.info = info - self.colours = SimpleNamespace(good=0x2dc614, neutral=0x2044f7, bad=0xc91628) + self.colours = SimpleNamespace(good=0x43B581, + neutral=0x7289DA, + bad=0xF04747) self.debug = debug_mode # --- Bot setup --- -bot = commands.Bot(command_prefix=opt.prefix, description=info.description, help_command=commands.MinimalHelpCommand()) - +bot = commands.Bot(command_prefix=opt.prefix, + description=info.description, + help_command=commands.MinimalHelpCommand()) # --- Helper functions --- @@ -121,22 +124,24 @@ _ensure_activity.start() try: bot.run(keys.discord_token) -except discord.LoginFailure as ex: # Miscellaneous authentications errors: borked token and co +except discord.LoginFailure as ex: + # Miscellaneous authentications errors: borked token and co if debug_mode: raise raise SystemExit("Error: Failed to authenticate: {}".format(ex)) -except discord.ConnectionClosed as ex: # When the connection the the gateway (websocket) is closed +except discord.ConnectionClosed as ex: + # When the connection to the gateway (websocket) is closed if debug_mode: raise raise SystemExit("Error: Discord gateway connection closed: [Code {}] {}".format(ex.code, ex.reason)) -except ConnectionResetError as ex: # More generic connection reset error +except ConnectionResetError as ex: + # More generic connection reset error if debug_mode: raise raise SystemExit("ConnectionResetError: {}".format(ex)) - # --- Exit --- # Codes for the wrapper shell script: # 0 - Clean exit, don't restart From a0cb64789b2571e44c223b2a0257c1102d643f72 Mon Sep 17 00:00:00 2001 From: Abigail Gold <5366828+classabbyamp@users.noreply.github.com> Date: Tue, 8 Oct 2019 23:02:57 -0400 Subject: [PATCH 2/3] Move cogs to load to options.py (#33) * list of cogs in options.py instead of hardcoded. * moved the playing status to options.py * add all cogs to the template --- main.py | 11 +++-------- templates/template_options.py | 7 +++++++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index a715063..e9177b7 100644 --- a/main.py +++ b/main.py @@ -99,7 +99,7 @@ async def on_ready(): @tasks.loop(minutes=5) async def _ensure_activity(): - await bot.change_presence(activity=discord.Game(name="with lids on 7.200")) + await bot.change_presence(activity=discord.Game(name=opt.game)) @_ensure_activity.before_loop @@ -110,13 +110,8 @@ async def _before_ensure_activity(): # --- Run --- bot.add_cog(GlobalSettings(bot)) -bot.load_extension("cogs.basecog") -bot.load_extension("cogs.morsecog") -bot.load_extension("cogs.funcog") -bot.load_extension("cogs.gridcog") -bot.load_extension("cogs.hamcog") -bot.load_extension("cogs.imagecog") -bot.load_extension("cogs.studycog") +for cog in opt.cogs: + bot.load_extension(f"cogs.{cog}") _ensure_activity.start() diff --git a/templates/template_options.py b/templates/template_options.py index acf65bc..549f4b4 100644 --- a/templates/template_options.py +++ b/templates/template_options.py @@ -24,3 +24,10 @@ debug = False # * Those users will have full control over the bot. # ! This MUST be a tuple of integers. Single element tuple: `(123,)` owners_uids = (200102491231092736,) + +# The cogs to load when running the bot. +cogs = ['basecog', 'morsecog', 'funcog', 'gridcog', 'hamcog', 'imagecog', + 'studycog'] + +# The text to put in the "playing" status. +game = 'with lids on 7.200' From 021a1312cbe9fc98dea0a04bf57cdfee26207aea Mon Sep 17 00:00:00 2001 From: Abigail Gold <5366828+classabbyamp@users.noreply.github.com> Date: Wed, 9 Oct 2019 01:25:33 -0400 Subject: [PATCH 3/3] add embed footers and timestamps to all current embeds (#34) --- cogs/basecog.py | 17 +++++++++++++++-- cogs/gridcog.py | 44 +++++++++++++++++++++++++++++++++++--------- cogs/hamcog.py | 31 +++++++++++++++++++++++-------- cogs/morsecog.py | 32 +++++++++++++++++++++++--------- cogs/studycog.py | 26 ++++++++++++++++++++++---- 5 files changed, 118 insertions(+), 32 deletions(-) diff --git a/cogs/basecog.py b/cogs/basecog.py index 585664a..79dc20a 100644 --- a/cogs/basecog.py +++ b/cogs/basecog.py @@ -10,6 +10,8 @@ General Public License, version 2. import discord import discord.ext.commands as commands +from datetime import datetime + class BaseCog(commands.Cog): def __init__(self, bot): @@ -19,7 +21,12 @@ class BaseCog(commands.Cog): @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) + 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) @@ -27,7 +34,13 @@ class BaseCog(commands.Cog): @commands.command(name="ping") async def _ping(self, ctx): - await ctx.send(f'**Pong!** Current ping is {self.bot.latency*1000:.1f} ms') + 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)) + await ctx.send(embed=embed) def setup(bot): diff --git a/cogs/gridcog.py b/cogs/gridcog.py index 8dd1715..03f1d9a 100644 --- a/cogs/gridcog.py +++ b/cogs/gridcog.py @@ -11,6 +11,8 @@ import discord import discord.ext.commands as commands import math +from datetime import datetime + class GridCog(commands.Cog): def __init__(self, bot): @@ -36,12 +38,20 @@ class GridCog(commands.Cog): grid += chr(ord('a') + int((latf - (int(latf/1)*1)) / (2.5/60))) grid += "**" embed = discord.Embed(title=f'Maidenhead Grid Locator for {float(lat):.6f}, {float(lon):.6f}', - description=grid, colour=self.gs.colours.good) + description=grid, + colour=self.gs.colours.good, + timestamp=datetime.utcnow()) + embed.set_footer(text=ctx.author.name, + icon_url=str(ctx.author.avatar_url)) else: raise ValueError('Out of range.') except Exception as e: msg = f'Error generating grid square for {lat}, {lon}.' - embed = discord.Embed(title=msg, description=str(e), colour=self.gs.colours.bad) + embed = discord.Embed(title=msg, description=str(e), + colour=self.gs.colours.bad, + timestamp=datetime.utcnow()) + embed.set_footer(text=ctx.author.name, + icon_url=str(ctx.author.avatar_url)) await ctx.send(embed=embed) @commands.command(name="ungrid", aliases=['loc']) @@ -56,16 +66,24 @@ class GridCog(commands.Cog): if len(grid) >= 6: embed = discord.Embed(title=f'Latitude and Longitude for {grid}', - description=f'**{loc[0]:.5f}, {loc[1]:.5f}**', colour=self.gs.colours.good, - url=f'https://www.openstreetmap.org/#map=13/{loc[0]:.5f}/{loc[1]:.5f}') + description=f'**{loc[0]:.5f}, {loc[1]:.5f}**', colour=self.gs.colours.good, + url=f'https://www.openstreetmap.org/#map=13/{loc[0]:.5f}/{loc[1]:.5f}', + timestamp=datetime.utcnow()) else: embed = discord.Embed(title=f'Latitude and Longitude for {grid}', - description=f'**{loc[0]:.1f}, {loc[1]:.1f}**', colour=self.gs.colours.good, - url=f'https://www.openstreetmap.org/#map=10/{loc[0]:.1f}/{loc[1]:.1f}') + description=f'**{loc[0]:.1f}, {loc[1]:.1f}**', colour=self.gs.colours.good, + url=f'https://www.openstreetmap.org/#map=10/{loc[0]:.1f}/{loc[1]:.1f}', + timestamp=datetime.utcnow()) + embed.set_footer(text=ctx.author.name, + icon_url=str(ctx.author.avatar_url)) except Exception as e: msg = f'Error generating latitude and longitude for grid {grid}.' - embed = discord.Embed(title=msg, description=str(e), colour=self.gs.colours.bad) + embed = discord.Embed(title=msg, description=str(e), + colour=self.gs.colours.bad, + timestamp=datetime.utcnow()) + embed.set_footer(text=ctx.author.name, + icon_url=str(ctx.author.avatar_url)) else: R = 6371 try: @@ -92,10 +110,18 @@ class GridCog(commands.Cog): des = f'**Distance:** {d:.1f} km ({d_mi:.1f} mi)\n**Bearing:** {bearing:.1f}°' embed = discord.Embed(title=f'Great Circle Distance and Bearing from {grid} to {grid2}', - description=des, colour=self.gs.colours.good) + description=des, + colour=self.gs.colours.good, + timestamp=datetime.utcnow()) + embed.set_footer(text=ctx.author.name, + icon_url=str(ctx.author.avatar_url)) except Exception as e: msg = f'Error generating great circle distance and bearing from {grid} and {grid2}.' - embed = discord.Embed(title=msg, description=str(e), colour=self.gs.colours.bad) + embed = discord.Embed(title=msg, description=str(e), + colour=self.gs.colours.bad, + timestamp=datetime.utcnow()) + embed.set_footer(text=ctx.author.name, + icon_url=str(ctx.author.avatar_url)) await ctx.send(embed=embed) def getCoords(self, grid: str): diff --git a/cogs/hamcog.py b/cogs/hamcog.py index 614dbd6..f8d79b4 100644 --- a/cogs/hamcog.py +++ b/cogs/hamcog.py @@ -28,15 +28,20 @@ class HamCog(commands.Cog): '''Look up a Q Code.''' with ctx.typing(): q = q.upper() - try: - code = self.qcodes[q] - embed = discord.Embed(title=q, description=self.qcodes[q], colour=self.gs.colours.good) - except: - embed = discord.Embed(title=f'Q Code {q} not found', colour=self.gs.colours.bad) + if q in self.qcodes: + embed = discord.Embed(title=q, description=self.qcodes[q], + colour=self.gs.colours.good, + timestamp=datetime.utcnow()) + else: + embed = discord.Embed(title=f'Q Code {q} not found', + colour=self.gs.colours.bad, + timestamp=datetime.utcnow()) + embed.set_footer(text=ctx.author.name, + icon_url=str(ctx.author.avatar_url)) await ctx.send(embed=embed) @commands.command(name="phonetics", aliases=['ph', 'phoneticize', 'phoneticise', 'phone']) - async def _phonetics_lookup(self, ctx, *, msg : str): + async def _phonetics_lookup(self, ctx, *, msg: str): '''Get phonetics for a word or phrase.''' with ctx.typing(): result = '' @@ -47,7 +52,12 @@ class HamCog(commands.Cog): else: result += char result += ' ' - embed = discord.Embed(title=f'Phonetics for {msg}', description=result.title(), colour=self.gs.colours.good) + embed = discord.Embed(title=f'Phonetics for {msg}', + description=result.title(), + colour=self.gs.colours.good, + timestamp=datetime.utcnow()) + embed.set_footer(text=ctx.author.name, + icon_url=str(ctx.author.avatar_url)) await ctx.send(embed=embed) @commands.command(name="utc", aliases=['z']) @@ -56,7 +66,12 @@ class HamCog(commands.Cog): with ctx.typing(): d = datetime.utcnow() result = '**' + d.strftime('%Y-%m-%d %H:%M') + 'Z**' - embed = discord.Embed(title='The current time is:', description=result, colour=self.gs.colours.good) + embed = discord.Embed(title='The current time is:', + description=result, + colour=self.gs.colours.good, + timestamp=datetime.utcnow()) + embed.set_footer(text=ctx.author.name, + icon_url=str(ctx.author.avatar_url)) await ctx.send(embed=embed) diff --git a/cogs/morsecog.py b/cogs/morsecog.py index c14af81..f32e37a 100644 --- a/cogs/morsecog.py +++ b/cogs/morsecog.py @@ -11,6 +11,8 @@ import discord import discord.ext.commands as commands import json +from datetime import datetime + class MorseCog(commands.Cog): def __init__(self, bot): @@ -21,7 +23,7 @@ class MorseCog(commands.Cog): self.morse2ascii = {v: k for k, v in self.ascii2morse.items()} @commands.command(name="morse", aliases=['cw']) - async def _morse(self, ctx, *, msg : str): + async def _morse(self, ctx, *, msg: str): """Converts ASCII to international morse code.""" with ctx.typing(): result = '' @@ -31,12 +33,16 @@ class MorseCog(commands.Cog): except: result += '' result += ' ' - embed = discord.Embed(title=f'Morse Code for {msg}', description=result, - colour=self.gs.colours.good) + embed = discord.Embed(title=f'Morse Code for {msg}', + description=result, + colour=self.gs.colours.good, + timestamp=datetime.utcnow()) + embed.set_footer(text=ctx.author.name, + icon_url=str(ctx.author.avatar_url)) await ctx.send(embed=embed) @commands.command(name="unmorse", aliases=['demorse', 'uncw', 'decw']) - async def _unmorse(self, ctx, *, msg : str): + async def _unmorse(self, ctx, *, msg: str): '''Converts international morse code to ASCII.''' with ctx.typing(): result = '' @@ -50,12 +56,16 @@ class MorseCog(commands.Cog): except: result += '' result += ' ' - embed = discord.Embed(title=f'ASCII for {msg0}', description=result, - colour=self.gs.colours.good) + embed = discord.Embed(title=f'ASCII for {msg0}', + description=result, + colour=self.gs.colours.good, + timestamp=datetime.utcnow()) + embed.set_footer(text=ctx.author.name, + icon_url=str(ctx.author.avatar_url)) await ctx.send(embed=embed) @commands.command(name="weight", aliases=["cwweight", 'cww']) - async def _weight(self, ctx, msg : str): + async def _weight(self, ctx, msg: str): '''Calculates the CW Weight of a callsign.''' with ctx.typing(): msg = msg.upper() @@ -69,8 +79,12 @@ class MorseCog(commands.Cog): await ctx.send(res) return res = f'The CW weight is **{weight}**' - embed = discord.Embed(title=f'CW Weight of {msg}', description=res, - colour=self.gs.colours.good) + embed = discord.Embed(title=f'CW Weight of {msg}', + description=res, + colour=self.gs.colours.good, + timestamp=datetime.utcnow()) + embed.set_footer(text=ctx.author.name, + icon_url=str(ctx.author.avatar_url)) await ctx.send(embed=embed) diff --git a/cogs/studycog.py b/cogs/studycog.py index 530b3c9..a76deb7 100644 --- a/cogs/studycog.py +++ b/cogs/studycog.py @@ -13,6 +13,8 @@ import discord.ext.commands as commands import random import json import aiohttp +from datetime import datetime + class StudyCog(commands.Cog): def __init__(self, bot): @@ -62,7 +64,11 @@ class StudyCog(commands.Cog): pool_questions = random.choice(pool_section)['questions'] question = random.choice(pool_questions) - embed = discord.Embed(title=question['id'], colour=self.gs.colours.good) + embed = discord.Embed(title=question['id'], + colour=self.gs.colours.good, + timestamp=datetime.utcnow()) + embed.set_footer(text=ctx.author.name, + icon_url=str(ctx.author.avatar_url)) embed = embed.add_field(name='Question:', value=question['text'], inline=False) embed = embed.add_field(name='Answers:', value= '**A:** ' + question['answers']['A'] + @@ -86,13 +92,25 @@ class StudyCog(commands.Cog): ans = ans.upper() if ans == correct_ans: result = f'Correct! The answer to {q_num} was **{correct_ans}**.' - embed = discord.Embed(title=f'{q_num} Answer', description=result, colour=self.gs.colours.good) + embed = discord.Embed(title=f'{q_num} Answer', + description=result, + colour=self.gs.colours.good, + timestamp=datetime.utcnow()) else: result = f'Incorrect. The answer to {q_num} was **{correct_ans}**, not **{ans}**.' - embed = discord.Embed(title=f'{q_num} Answer', description=result, colour=self.gs.colours.bad) + embed = discord.Embed(title=f'{q_num} Answer', + description=result, + colour=self.gs.colours.bad, + timestamp=datetime.utcnow()) else: result = f'The correct answer to {q_num} was **{correct_ans}**.' - embed = discord.Embed(title=f'{q_num} Answer', description=result, colour=self.gs.colours.neutral) + embed = discord.Embed(title=f'{q_num} Answer', + description=result, + colour=self.gs.colours.neutral, + timestamp=datetime.utcnow()) + + embed.set_footer(text=ctx.author.name, + icon_url=str(ctx.author.avatar_url)) await ctx.send(embed=embed)