-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds .env file with bot token * Adds basic functionality of converting letters to morse code * adds vscode settings.json to gitignore * Touch ups to provide proper spacing. * putting .env.example back * Adds formatting changes * Fixes Andrew's review. gitignore ignores the whole vscode folder, removes merge artifacts, deletes from unimplemented, changes name to UpperCamel, removes all back ticks instead of replacing 3 with 3 apostrophes * Morse Removed unnecessary imports. * Adds types for check adn encrypt. Changes class name to Morse and changes relevant references too. * Remove redundant SB_RATELIMIT in env ex. fixes order of morse in cogs. * Add return type to check and encrypt. Changes fallback return to "" to reflect this. Check on check return is also hence changed from != 0 to != "" * makes the regex a regex --------- Co-authored-by: Andrew Brown <[email protected]>
- Loading branch information
1 parent
689af81
commit 77904eb
Showing
3 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,3 +131,4 @@ dmypy.json | |
.vim | ||
*.db | ||
**/.DS_STORE | ||
.vscode/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
from typing import List | ||
|
||
import re | ||
|
||
import discord | ||
from discord import app_commands | ||
from discord.ext import commands | ||
|
||
from uqcsbot.bot import UQCSBot | ||
|
||
# value of all valid ascii values in morse code | ||
MorseCodeDict = { | ||
"A": ". - ", | ||
"B": "- . . . ", | ||
"C": "- . - . ", | ||
"D": "- . . ", | ||
"E": ". ", | ||
"F": ". . - . ", | ||
"G": "- - . ", | ||
"H": ". . . . ", | ||
"I": ". . ", | ||
"J": ". - - - ", | ||
"K": "- . - ", | ||
"L": ". - . . ", | ||
"M": "- - ", | ||
"N": "- . ", | ||
"O": "- - - ", | ||
"P": ". - - . ", | ||
"Q": "- - . - ", | ||
"R": ". - . ", | ||
"S": ". . . ", | ||
"T": "- ", | ||
"U": ". . - ", | ||
"V": ". . . - ", | ||
"W": ". - - ", | ||
"X": "- . . - ", | ||
"Y": "- . - - ", | ||
"Z": "- - . . ", | ||
"1": ". - - - - ", | ||
"2": ". . - - - ", | ||
"3": ". . . - - ", | ||
"4": ". . . . - ", | ||
"5": ". . . . . ", | ||
"6": "- . . . . ", | ||
"7": "- - . . . ", | ||
"8": "- - - . . ", | ||
"9": "- - - - . ", | ||
"0": "- - - - - ", | ||
",": "- - . . - - ", | ||
".": ". - . - . - ", | ||
"?": ". . - - . . ", | ||
"/": "- . . - . ", | ||
"-": "- . . . . - ", | ||
"(": "- . - - . ", | ||
")": "- . - - . - ", | ||
" ": " ", | ||
";": "_ . _ . _ . ", | ||
":": "_ _ _ . . .", | ||
"'": ". _ _ _ _ . ", | ||
'"': ". _ . . _ . ", | ||
"_": ". . _ _ . _ ", | ||
"=": "_ . . . _ ", | ||
"+": ". _ . _ . ", | ||
} | ||
|
||
|
||
class Morse(commands.Cog): | ||
def __init__(self, bot: UQCSBot): | ||
self.bot = bot | ||
|
||
@app_commands.command(name="morse") | ||
@app_commands.describe(message="The message to be converted to morse code") | ||
async def morse_command( | ||
self, interaction: discord.Interaction, message: str | ||
) -> None: | ||
""" | ||
Returns a string containing the argument converted to dots and dashes. | ||
""" | ||
|
||
# Sanitise invalid characters from the message | ||
message = Morse.sanitise_illegals(message) | ||
|
||
# Sanitise message for discord emotes | ||
message = Morse.sanitise_emotes(message) | ||
|
||
# Convert all chars to lower case | ||
message = message.upper() | ||
|
||
# Then check they are valid morse code ascii | ||
invalid = Morse.check(message) | ||
if invalid != "": | ||
await interaction.response.send_message( | ||
f"Invalid morse code character/s in string: {invalid}" | ||
) | ||
return | ||
|
||
# encrypt message | ||
response = Morse.encrypt_to_morse(message) | ||
|
||
# Check if response is too long | ||
if len(response) > 2000: | ||
await interaction.response.send_message("Resulting message too long!") | ||
return | ||
|
||
await interaction.response.send_message(response) | ||
|
||
@staticmethod | ||
def sanitise_illegals(message: str) -> str: | ||
""" | ||
Replaces all illegal characters in the message with their sanitised | ||
equivalent. | ||
""" | ||
|
||
# Strip whitespace from either side of the message | ||
message = message.strip() | ||
|
||
# replace code blocks with their sanitised equivalent | ||
message = message.replace("`", "") | ||
|
||
return message | ||
|
||
@staticmethod | ||
def sanitise_emotes(message: str) -> str: | ||
""" | ||
Replaces all emotes in the message with their emote name instead of | ||
their id. | ||
""" | ||
|
||
# Regex to match emotes. | ||
emotes: List[str] = re.findall(r"<a?:\w+:\d+>", message) | ||
|
||
# Replace each emote with its name. | ||
for emote in emotes: | ||
emote_name: str = emote.split(":")[1].strip() | ||
message = message.replace(emote, f":{emote_name}:") | ||
|
||
return message | ||
|
||
@staticmethod | ||
def check(message: str) -> str: | ||
ret = "" | ||
for letter in message: | ||
if letter not in MorseCodeDict: | ||
ret += letter | ||
if len(ret) != 0: | ||
return ret | ||
return "" | ||
|
||
@staticmethod | ||
def encrypt_to_morse(message: str) -> str: | ||
cipher = "" | ||
for letter in message: | ||
cipher += MorseCodeDict[letter] + " " | ||
return cipher | ||
|
||
|
||
async def setup(bot: UQCSBot): | ||
cog = Morse(bot) | ||
await bot.add_cog(cog) |