qrm2/exts/codes.py

82 lines
2.8 KiB
Python
Raw Normal View History

2019-10-05 19:36:45 -04:00
"""
Codes extension for qrm
2019-10-05 19:36:45 -04:00
---
2021-03-28 13:57:03 -04:00
Copyright (C) 2019-2021 classabbyamp, 0x5c (as ham.py)
Copyright (C) 2021 classabbyamp, 0x5c
2019-10-05 19:36:45 -04:00
This file is part of qrm2 and is released under the terms of
the GNU General Public License, version 2.
2019-10-05 19:36:45 -04:00
"""
import json
2019-10-05 19:36:45 -04:00
import discord.ext.commands as commands
import common as cmn
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(cmn.paths.resources / "phonetics.1.json") as file:
d = json.load(file)
self.phonetics: dict[str, str] = d["phonetics"]
self.pweights: dict[str, int] = d["pweights"]
with open(cmn.paths.resources / "qcodes.1.json") as file:
self.qcodes: dict = json.load(file)
2019-10-05 19:36:45 -04:00
@commands.command(name="qcode", aliases=["q"], category=cmn.Cats.CODES)
async def _qcode_lookup(self, ctx: commands.Context, qcode: str):
"""Looks up the meaning of a Q Code."""
qcode = qcode.upper()
embed = cmn.embed_factory(ctx)
if qcode in self.qcodes:
embed.title = qcode
embed.description = self.qcodes[qcode]
embed.colour = cmn.colours.good
else:
embed.title = f"Q Code {qcode} not found"
embed.colour = cmn.colours.bad
2019-10-05 19:36:45 -04:00
await ctx.send(embed=embed)
@commands.command(name="phonetics", aliases=["ph", "phoneticize", "phoneticise", "phone"], category=cmn.Cats.CODES)
async def _phonetics_lookup(self, ctx: commands.Context, *, msg: str):
"""Returns NATO phonetics for a word or phrase."""
result = ""
for char in msg.lower():
if char.isalpha():
result += self.phonetics[char]
else:
result += char
result += " "
embed = cmn.embed_factory(ctx)
embed.title = f"Phonetics for {msg}"
embed.description = result.title()
embed.colour = cmn.colours.good
2019-10-05 19:36:45 -04:00
await ctx.send(embed=embed)
@commands.command(name="phoneticweight", aliases=["pw"], category=cmn.Cats.CODES)
async def _weight(self, ctx: commands.Context, *, msg: str):
"""Calculates the phonetic weight of a callsign or message."""
embed = cmn.embed_factory(ctx)
msg = msg.upper()
weight = 0
for char in msg:
try:
weight += self.pweights[char]
except KeyError:
embed.title = "Error in calculation of phonetic weight"
embed.description = f"Unknown character `{char}` in message"
embed.colour = cmn.colours.bad
await ctx.send(embed=embed)
return
embed.title = f"Phonetic Weight of {msg}"
embed.description = f"The phonetic weight is **{weight}**"
embed.colour = cmn.colours.good
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))