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
|
|
|
---
|
2020-01-06 23:27:48 -05:00
|
|
|
Copyright (C) 2019-2020 Abigail Gold, 0x5c
|
2019-10-04 20:07:58 -04:00
|
|
|
|
2019-12-08 15:35:58 -05:00
|
|
|
This file is part of discord-qrm2 and is released under the terms of the GNU
|
2019-10-05 02:23:11 -04:00
|
|
|
General Public License, version 2.
|
2019-10-04 20:07:58 -04:00
|
|
|
"""
|
|
|
|
|
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-12-25 00:42:54 -05:00
|
|
|
from resources import morse
|
2019-12-06 01:19:42 -05:00
|
|
|
|
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
|
|
|
|
|
2019-12-06 01:19: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."""
|
|
|
|
with ctx.typing():
|
|
|
|
result = ''
|
|
|
|
for char in msg.upper():
|
2019-10-12 19:50:40 -04:00
|
|
|
try:
|
2019-12-25 00:42:54 -05:00
|
|
|
result += morse.morse[char]
|
2019-10-12 19:50:40 -04:00
|
|
|
except KeyError:
|
2019-10-04 20:07:58 -04:00
|
|
|
result += '<?>'
|
|
|
|
result += ' '
|
2019-12-16 03:49:34 -05:00
|
|
|
embed = cmn.embed_factory(ctx)
|
|
|
|
embed.title = f'Morse Code for {msg}'
|
2019-12-25 00:42:54 -05:00
|
|
|
embed.description = '**' + result + '**'
|
2019-12-16 03:49:34 -05:00
|
|
|
embed.colour = cmn.colours.good
|
2019-10-04 20:07:58 -04:00
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
2019-12-06 01:19: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):
|
2019-10-04 20:07:58 -04:00
|
|
|
'''Converts international morse code to ASCII.'''
|
|
|
|
with ctx.typing():
|
|
|
|
result = ''
|
|
|
|
msg0 = msg
|
|
|
|
msg = msg.split('/')
|
|
|
|
msg = [m.split() for m in msg]
|
|
|
|
for word in msg:
|
|
|
|
for char in word:
|
2019-10-12 19:50:40 -04:00
|
|
|
try:
|
2019-12-25 00:42:54 -05:00
|
|
|
result += morse.ascii[char]
|
2019-10-12 19:50:40 -04:00
|
|
|
except KeyError:
|
2019-10-04 20:07:58 -04:00
|
|
|
result += '<?>'
|
|
|
|
result += ' '
|
2019-12-16 03:49:34 -05:00
|
|
|
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)
|
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
@commands.command(name="cwweight", aliases=["weight", 'cww'], category=cmn.cat.ref)
|
|
|
|
async def _weight(self, ctx: commands.Context, *, msg: str):
|
|
|
|
'''Calculates the CW Weight of a callsign or message.'''
|
2019-12-23 10:32:24 -05:00
|
|
|
embed = cmn.embed_factory(ctx)
|
2019-10-04 20:07:58 -04:00
|
|
|
with ctx.typing():
|
|
|
|
msg = msg.upper()
|
|
|
|
weight = 0
|
|
|
|
for char in msg:
|
2019-10-12 19:50:40 -04:00
|
|
|
try:
|
2019-12-25 00:42:54 -05:00
|
|
|
cw_char = morse.morse[char].replace('-', '==')
|
2019-10-18 08:27:05 -04:00
|
|
|
weight += len(cw_char) * 2 + 2
|
2019-10-12 19:50:40 -04:00
|
|
|
except KeyError:
|
2019-12-23 10:32:24 -05:00
|
|
|
embed.title = 'Error in calculation of CW weight'
|
2020-01-27 00:37:52 -05:00
|
|
|
embed.description = f'Unknown character `{char}` in message'
|
2019-12-23 10:32:24 -05:00
|
|
|
embed.colour = cmn.colours.bad
|
|
|
|
await ctx.send(embed=embed)
|
2019-10-04 20:07:58 -04:00
|
|
|
return
|
2019-12-16 03:49:34 -05:00
|
|
|
embed.title = f'CW Weight of {msg}'
|
2019-12-23 10:32:24 -05:00
|
|
|
embed.description = f'The CW weight is **{weight}**'
|
2019-12-16 03:49:34 -05:00
|
|
|
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))
|