-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMorse.py
160 lines (141 loc) · 4.76 KB
/
Morse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import asyncio
import discord
from discord.ext import commands
from operator import itemgetter
import base64
import binascii
import re
def setup(bot):
# Add the bot and deps
bot.add_cog(Morse(bot))
class Morse(commands.Cog):
# Init with the bot reference
def __init__(self, bot):
self.bot = bot
self.to_morse = {
"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": "-----",
}
@commands.command(pass_context=True)
async def morsetable(self, ctx, num_per_row=None):
"""Prints out the morse code lookup table."""
try:
num_per_row = int(num_per_row)
except Exception:
num_per_row = 5
msg = "__**Morse Code Lookup Table:**__\n```\n"
max_length = 0
current_row = 0
row_list = [[]]
cur_list = []
sorted_list = sorted(self.to_morse)
print(sorted_list)
for key in sorted_list:
print(key)
entry = "{} : {}".format(key.upper(), self.to_morse[key])
if len(entry) > max_length:
max_length = len(entry)
row_list[len(row_list) - 1].append(entry)
if len(row_list[len(row_list) - 1]) >= num_per_row:
row_list.append([])
current_row += 1
for row in row_list:
for entry in row:
entry = entry.ljust(max_length)
msg += entry + " "
msg += "\n"
msg += "```"
await ctx.send(msg)
@commands.command(pass_context=True)
async def morse(self, ctx, *, content=None):
"""Converts to morse code."""
if content == None:
await ctx.send("Usage `{}morse [content]`".format(ctx.prefix))
return
# Only accept alpha numeric stuff and spaces
word_list = content.split()
morse_list = []
for word in word_list:
# Iterate through words
letter_list = []
for letter in word:
# Iterate through each letter of each word
if letter.lower() in self.to_morse:
# It's in our list - add the morse equivalent
letter_list.append(self.to_morse[letter.lower()])
if len(letter_list):
# We have letters - join them into morse words
# each separated by a space
morse_list.append(" ".join(letter_list))
if not len(morse_list):
# We didn't get any valid words
await ctx.send("There were no valid a-z/0-9 chars in the passed content.")
return
# We got *something*
msg = " ".join(morse_list)
msg = "```\n" + msg + "```"
await ctx.send(msg)
@commands.command(pass_context=True)
async def unmorse(self, ctx, *, content=None):
"""Converts morse code to ascii."""
if content == None:
await ctx.send("Usage `{}unmorse [content]`".format(ctx.prefix))
return
# Only accept alpha numeric stuff and spaces
word_list = content.split(" ")
ascii_list = []
for word in word_list:
# Split by space for letters
letter_list = word.split()
letter_ascii = []
# Iterate through letters
for letter in letter_list:
for key in self.to_morse:
if self.to_morse[key] == letter:
# Found one
letter_ascii.append(key.upper())
if len(letter_ascii):
# We have letters - join them into ascii words
ascii_list.append("".join(letter_ascii))
if not len(ascii_list):
# We didn't get any valid words
await ctx.send("There were no valid morse chars in the passed content.")
return
# We got *something* - join separated by a space
msg = " ".join(ascii_list)
msg = "```\n" + msg + "```"
await ctx.send(msg)