Merge branch 'master' into ae7qcog

This commit is contained in:
Abigail Gold 2019-10-12 20:30:11 -04:00
commit c0bc07827e
No known key found for this signature in database
GPG Key ID: 80A676456AB6B045
7 changed files with 140 additions and 48 deletions

View File

@ -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):

View File

@ -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):

View File

@ -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)

View File

@ -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)

View File

@ -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)

31
main.py
View File

@ -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 ---
@ -96,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
@ -107,14 +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")
bot.load_extension("cogs.ae7qcog")
for cog in opt.cogs:
bot.load_extension(f"cogs.{cog}")
_ensure_activity.start()
@ -122,22 +119,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

View File

@ -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', 'ae7qcog']
# The text to put in the "playing" status.
game = 'with lids on 7.200'