qrm2/exts/ham.py

120 lines
5.2 KiB
Python
Raw Normal View History

2019-10-05 19:36:45 -04:00
"""
Ham extension for qrm
2019-10-05 19:36:45 -04:00
---
Copyright (C) 2019 Abigail Gold, 0x5c
This file is part of discord-qrm2 and is released under the terms of the GNU
2019-10-05 19:36:45 -04:00
General Public License, version 2.
"""
import json
import random
from datetime import datetime
2019-10-05 19:36:45 -04:00
import discord
import discord.ext.commands as commands
import common as cmn
from resources import callsign_info
2019-10-05 19:36:45 -04:00
class HamCog(commands.Cog):
def __init__(self, bot: commands.Bot):
2019-10-05 19:36:45 -04:00
self.bot = bot
with open('resources/qcodes.json') as qcode_file:
self.qcodes = json.load(qcode_file)
2019-10-05 20:30:46 -04:00
with open('resources/words') as words_file:
self.words = words_file.read().lower().splitlines()
2019-10-05 19:36:45 -04:00
@commands.command(name="qcode", aliases=['q'], category=cmn.cat.ref)
async def _qcode_lookup(self, ctx: commands.Context, qcode: str):
2019-10-05 19:36:45 -04:00
'''Look up a Q Code.'''
with ctx.typing():
qcode = qcode.upper()
if qcode in self.qcodes:
embed = discord.Embed(title=qcode, description=self.qcodes[qcode],
colour=cmn.colours.good,
timestamp=datetime.utcnow())
else:
embed = discord.Embed(title=f'Q Code {qcode} not found',
colour=cmn.colours.bad,
timestamp=datetime.utcnow())
embed.set_footer(text=ctx.author.name,
icon_url=str(ctx.author.avatar_url))
2019-10-05 19:36:45 -04:00
await ctx.send(embed=embed)
@commands.command(name="phonetics", aliases=['ph', 'phoneticize', 'phoneticise', 'phone'], category=cmn.cat.fun)
async def _phonetics_lookup(self, ctx: commands.Context, *, msg: str):
2019-10-05 19:36:45 -04:00
'''Get phonetics for a word or phrase.'''
with ctx.typing():
result = ''
for char in msg.lower():
if char.isalpha():
result += random.choice([word for word in self.words if word[0] == char])
2019-10-05 19:36:45 -04:00
else:
result += char
result += ' '
embed = discord.Embed(title=f'Phonetics for {msg}',
description=result.title(),
colour=cmn.colours.good,
timestamp=datetime.utcnow())
embed.set_footer(text=ctx.author.name,
icon_url=str(ctx.author.avatar_url))
2019-10-05 19:36:45 -04:00
await ctx.send(embed=embed)
@commands.command(name="utc", aliases=['z'], category=cmn.cat.ref)
async def _utc_lookup(self, ctx: commands.Context):
2019-10-05 19:36:45 -04:00
'''Gets the current time in UTC.'''
with ctx.typing():
now = datetime.utcnow()
result = '**' + now.strftime('%Y-%m-%d %H:%M') + 'Z**'
embed = discord.Embed(title='The current time is:',
description=result,
colour=cmn.colours.good,
timestamp=datetime.utcnow())
embed.set_footer(text=ctx.author.name,
icon_url=str(ctx.author.avatar_url))
2019-10-05 19:36:45 -04:00
await ctx.send(embed=embed)
@commands.command(name="prefixes", aliases=["vanity", "pfx", "vanities", "prefix"])
async def _vanity_prefixes(self, ctx: commands.Context, country: str = None):
'''Lists valid prefixes for countries.'''
if country is None:
await ctx.send_help(ctx.command)
return
if country.lower() not in callsign_info.options:
embed = discord.Embed(title=f'{country} not found!',
description=f'Valid countries: {", ".join(callsign_info.options.keys())}',
2019-12-07 17:13:06 -05:00
colour=cmn.colours.bad,
timestamp=datetime.utcnow())
embed.set_footer(text=ctx.author.name,
icon_url=str(ctx.author.avatar_url))
await ctx.send(embed=embed)
return
embed = discord.Embed(title=callsign_info.options[country.lower()][0],
description=callsign_info.options[country.lower()][1],
2019-12-07 17:13:06 -05:00
colour=cmn.colours.good,
timestamp=datetime.utcnow())
embed.set_footer(text=ctx.author.name,
icon_url=str(ctx.author.avatar_url))
for name, val in callsign_info.options[country.lower()][2].items():
embed.add_field(name=name, value=val, inline=False)
await ctx.send(embed=embed)
2019-12-15 12:51:43 -05:00
@commands.command(name="contests", aliases=["cc", "tests"], category=cmn.cat.ref)
async def _contests(self, ctx: commands.Context):
embed = discord.Embed(title="Contest Calendar",
timestamp=datetime.utcnow(),
colour=cmn.colours.good)
embed.set_footer(text=ctx.author.name,
icon_url=str(ctx.author.avatar_url))
embed.description = ("*We are currently rewriting the old, Chrome-based `contests` command. In the meantime, "
"use [the website](https://www.contestcalendar.com/weeklycont.php).*")
await ctx.send(embed=embed)
2019-10-05 19:36:45 -04:00
def setup(bot: commands.Bot):
2019-10-05 19:36:45 -04:00
bot.add_cog(HamCog(bot))