2019-10-05 19:36:45 -04:00
|
|
|
"""
|
2021-03-28 09:50:51 -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
|
|
|
|
2021-06-26 20:23:55 -04:00
|
|
|
SPDX-License-Identifier: LiLiQ-Rplus-1.1
|
2019-10-05 19:36:45 -04:00
|
|
|
"""
|
2019-12-25 00:42:54 -05:00
|
|
|
|
2020-01-31 06:50:50 -05:00
|
|
|
|
2021-03-18 09:17:30 -04:00
|
|
|
import json
|
2019-10-05 19:36:45 -04:00
|
|
|
|
|
|
|
import discord.ext.commands as commands
|
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
import common as cmn
|
2019-11-28 23:48:11 -05:00
|
|
|
|
2019-10-05 19:36:45 -04:00
|
|
|
|
|
|
|
class HamCog(commands.Cog):
|
2019-10-18 08:27:05 -04:00
|
|
|
def __init__(self, bot: commands.Bot):
|
2019-10-05 19:36:45 -04:00
|
|
|
self.bot = bot
|
2021-03-18 09:17:30 -04:00
|
|
|
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
|
|
|
|
2021-03-28 09:50:51 -04:00
|
|
|
@commands.command(name="qcode", aliases=["q"], category=cmn.Cats.CODES)
|
2019-10-18 08:27:05 -04:00
|
|
|
async def _qcode_lookup(self, ctx: commands.Context, qcode: str):
|
2020-02-15 04:59:25 -05:00
|
|
|
"""Looks up the meaning of a Q Code."""
|
2020-02-25 20:36:21 -05:00
|
|
|
qcode = qcode.upper()
|
|
|
|
embed = cmn.embed_factory(ctx)
|
2021-03-18 09:17:30 -04:00
|
|
|
if qcode in self.qcodes:
|
2020-02-25 20:36:21 -05:00
|
|
|
embed.title = qcode
|
2021-03-18 09:17:30 -04:00
|
|
|
embed.description = self.qcodes[qcode]
|
2020-02-25 20:36:21 -05:00
|
|
|
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)
|
|
|
|
|
2021-03-28 09:50:51 -04:00
|
|
|
@commands.command(name="phonetics", aliases=["ph", "phoneticize", "phoneticise", "phone"], category=cmn.Cats.CODES)
|
2019-10-18 08:27:05 -04:00
|
|
|
async def _phonetics_lookup(self, ctx: commands.Context, *, msg: str):
|
2020-02-15 04:59:25 -05:00
|
|
|
"""Returns NATO phonetics for a word or phrase."""
|
2020-02-25 20:36:21 -05:00
|
|
|
result = ""
|
|
|
|
for char in msg.lower():
|
|
|
|
if char.isalpha():
|
2021-03-18 09:17:30 -04:00
|
|
|
result += self.phonetics[char]
|
2020-02-25 20:36:21 -05:00
|
|
|
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)
|
|
|
|
|
2021-03-28 09:50:51 -04:00
|
|
|
@commands.command(name="phoneticweight", aliases=["pw"], category=cmn.Cats.CODES)
|
2020-03-30 18:56:29 -04:00
|
|
|
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:
|
2021-03-18 09:17:30 -04:00
|
|
|
weight += self.pweights[char]
|
2020-03-30 18:56:29 -04:00
|
|
|
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
|
|
|
|
2019-10-18 08:27:05 -04:00
|
|
|
def setup(bot: commands.Bot):
|
2019-10-05 19:36:45 -04:00
|
|
|
bot.add_cog(HamCog(bot))
|