2019-10-04 20:07:58 -04:00
|
|
|
"""
|
2019-12-07 17:26:55 -05:00
|
|
|
Morse Code extension for qrm
|
2019-10-04 20:07:58 -04:00
|
|
|
---
|
2021-03-18 09:17:30 -04:00
|
|
|
Copyright (C) 2019-2021 Abigail Gold, 0x5c
|
2019-10-04 20:07:58 -04:00
|
|
|
|
2020-02-15 06:27:48 -05:00
|
|
|
This file is part of qrm2 and is released under the terms of
|
2020-01-31 06:50:50 -05:00
|
|
|
the GNU General Public License, version 2.
|
2019-10-04 20:07:58 -04:00
|
|
|
"""
|
|
|
|
|
2020-01-31 06:50:50 -05:00
|
|
|
|
2021-03-18 09:17:30 -04:00
|
|
|
import json
|
|
|
|
|
2019-10-18 08:27:05 -04:00
|
|
|
import discord.ext.commands as commands
|
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
import common as cmn
|
|
|
|
|
2019-10-04 20:07:58 -04:00
|
|
|
|
|
|
|
class MorseCog(commands.Cog):
|
2019-10-18 08:27:05 -04:00
|
|
|
def __init__(self, bot: commands.Bot):
|
2019-10-04 20:07:58 -04:00
|
|
|
self.bot = bot
|
2021-03-18 09:17:30 -04:00
|
|
|
with open(cmn.paths.resources / "morse.1.json") as file:
|
|
|
|
d = json.load(file)
|
|
|
|
self.morse: dict[str, str] = d["morse"]
|
|
|
|
self.ascii: dict[str, int] = d["ascii"]
|
2019-10-04 20:07:58 -04:00
|
|
|
|
2020-01-30 06:15:42 -05:00
|
|
|
@commands.command(name="morse", aliases=["cw"], category=cmn.cat.ref)
|
2019-10-18 08:27:05 -04:00
|
|
|
async def _morse(self, ctx: commands.Context, *, msg: str):
|
2019-10-04 20:07:58 -04:00
|
|
|
"""Converts ASCII to international morse code."""
|
2020-02-25 20:36:21 -05:00
|
|
|
result = ""
|
|
|
|
for char in msg.upper():
|
|
|
|
try:
|
2021-03-18 09:17:30 -04:00
|
|
|
result += self.morse[char]
|
2020-02-25 20:36:21 -05:00
|
|
|
except KeyError:
|
|
|
|
result += "<?>"
|
|
|
|
result += " "
|
|
|
|
embed = cmn.embed_factory(ctx)
|
|
|
|
embed.title = f"Morse Code for {msg}"
|
|
|
|
embed.description = "**" + result + "**"
|
|
|
|
embed.colour = cmn.colours.good
|
2019-10-04 20:07:58 -04:00
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
2020-01-30 06:15:42 -05:00
|
|
|
@commands.command(name="unmorse", aliases=["demorse", "uncw", "decw"], category=cmn.cat.ref)
|
2019-10-18 08:27:05 -04:00
|
|
|
async def _unmorse(self, ctx: commands.Context, *, msg: str):
|
2020-01-30 06:15:42 -05:00
|
|
|
"""Converts international morse code to ASCII."""
|
2020-02-25 20:36:21 -05:00
|
|
|
result = ""
|
|
|
|
msg0 = msg
|
|
|
|
msg = msg.split("/")
|
|
|
|
msg = [m.split() for m in msg]
|
|
|
|
for word in msg:
|
|
|
|
for char in word:
|
|
|
|
try:
|
2021-03-18 09:17:30 -04:00
|
|
|
result += self.ascii[char]
|
2020-02-25 20:36:21 -05:00
|
|
|
except KeyError:
|
|
|
|
result += "<?>"
|
|
|
|
result += " "
|
|
|
|
embed = cmn.embed_factory(ctx)
|
|
|
|
embed.title = f"ASCII for {msg0}"
|
|
|
|
embed.description = result
|
|
|
|
embed.colour = cmn.colours.good
|
2019-10-04 20:07:58 -04:00
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
2020-01-30 06:15:42 -05:00
|
|
|
@commands.command(name="cwweight", aliases=["weight", "cww"], category=cmn.cat.ref)
|
2019-12-06 01:19:42 -05:00
|
|
|
async def _weight(self, ctx: commands.Context, *, msg: str):
|
2020-02-15 04:59:25 -05:00
|
|
|
"""Calculates the CW weight of a callsign or message."""
|
2019-12-23 10:32:24 -05:00
|
|
|
embed = cmn.embed_factory(ctx)
|
2020-02-25 20:36:21 -05:00
|
|
|
msg = msg.upper()
|
|
|
|
weight = 0
|
|
|
|
for char in msg:
|
|
|
|
try:
|
2021-03-18 09:17:30 -04:00
|
|
|
cw_char = self.morse[char].replace("-", "==")
|
2020-02-25 20:36:21 -05:00
|
|
|
weight += len(cw_char) * 2 + 2
|
|
|
|
except KeyError:
|
|
|
|
embed.title = "Error in calculation of CW weight"
|
|
|
|
embed.description = f"Unknown character `{char}` in message"
|
|
|
|
embed.colour = cmn.colours.bad
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
return
|
|
|
|
embed.title = f"CW Weight of {msg}"
|
|
|
|
embed.description = f"The CW weight is **{weight}**"
|
|
|
|
embed.colour = cmn.colours.good
|
2019-10-04 20:07:58 -04:00
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
|
|
|
|
2019-10-18 08:27:05 -04:00
|
|
|
def setup(bot: commands.Bot):
|
2019-10-04 20:07:58 -04:00
|
|
|
bot.add_cog(MorseCog(bot))
|