From 9f834dfc43e28cf833833f69008e57b655ca3499 Mon Sep 17 00:00:00 2001 From: misarb Date: Sat, 27 Aug 2022 17:49:38 +0100 Subject: [PATCH 1/3] add md5 and morse code --- cogs/md5.py | 35 ++++++++++++++++++++++ cogs/mor.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 4 +++ 3 files changed, 125 insertions(+) create mode 100644 cogs/md5.py create mode 100644 cogs/mor.py diff --git a/cogs/md5.py b/cogs/md5.py new file mode 100644 index 0000000..a83d50f --- /dev/null +++ b/cogs/md5.py @@ -0,0 +1,35 @@ +#!/usr/bin/python +# created by : Boulbalah lahcen + +from discord.ext import commands +import hashlib + +class md5(commands.Cog): + def __init__(self, client): + self.client = client + + @commands.Cog.listener() + async def on_ready(self): + print(__file__, ' Online') + + @commands.command() + async def hex(self, ctx, action, *, text): + + # encode md5 + if action == "encode" or action == "e": + # encode to md5 + output = hashlib.md5(text.encode()).hexdigest() + await ctx.send(output) + return [output, True] + + # decode md5 + if action == "decode" or action == "d": + # decode to md5 + if hashlib.md5(text.encode()).hexdigest() == text: + return [text,True] + else: + return "Eroor" + +async def setup(client): + await client.add_cog(hex(client)) + diff --git a/cogs/mor.py b/cogs/mor.py new file mode 100644 index 0000000..c80b3f0 --- /dev/null +++ b/cogs/mor.py @@ -0,0 +1,86 @@ +#!/usr/bin/python +# created by : Boulbalah lahcen + +from discord.ext import commands + + + +class mor(commands.Cog): + def __init__(self, client): + self.client = client + # Dictionary representing the morse code + encode_table = { + "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": "--..", + "0": "-----", + "1": ".----", + "2": "..---", + "3": "...--", + "4": "....-", + "5": ".....", + "6": "-....", + "7": "--...", + "8": "---..", + "9": "----.", + ".": ".-.-.-", + ",": "--..--", + "?": "..--..", + " ": "SPACE", + } + #reverse encode tabale + decode_table = {v: k for k, v in encode_table.items()} + def encode(self,s): + enc = " ".join(self.encode_table[x] for x in s) + return enc.replace(" SPACE ", " ") + + def decode(self,encoded): + symbols = encoded.replace(" ", " SPACE ").split(" ") + return "".join(self.decode_table[x] for x in symbols) + + @commands.Cog.listener() + async def on_ready(self): + print(__file__, ' Online') + + @commands.command() + async def hex(self, ctx, action, *, text): + + # encode morse + if action == "encode" or action == "e": + # encode to morse + output = self.encode(text) + await ctx.send(output) + return [output, True] + + # decode mor + if action == "decode" or action == "d": + output = self.decode(text) + await ctx.send(output) + return [output,True] + +async def setup(client): + await client.add_cog(hex(client)) + diff --git a/main.py b/main.py index f0d50e2..adbe2bf 100644 --- a/main.py +++ b/main.py @@ -66,6 +66,10 @@ async def help(ctx): "Encoding and decoding with Rotation 13.", inline=False) embed.add_field(name="Binary", value="`bin {encode/e & decode/d} {\"string\"}`\n" "Encoding and decoding with binary.", inline=False) + embed.add_field(name="MD5", value="`md5 {encode/e & decode/d} {\"string\"}`\n" + "Encoding and decoding with md5.", inline=False) + embed.add_field(name="Morse", value="`mor {encode/e & decode/d} {\"string\"}`\n" + "Encoding and decoding Morse code.", inline=False) embed.set_footer(text='Created by Soulsender. See the wiki for documentation.') await ctx.send(embed=embed) From 14ac9a33ec9739243651b16c202f8ae2f3c1300f Mon Sep 17 00:00:00 2001 From: misarb Date: Sat, 27 Aug 2022 19:10:46 +0100 Subject: [PATCH 2/3] fix cog not found --- cogs/md5.py | 4 ++-- cogs/mor.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cogs/md5.py b/cogs/md5.py index a83d50f..e28cbb2 100644 --- a/cogs/md5.py +++ b/cogs/md5.py @@ -13,7 +13,7 @@ async def on_ready(self): print(__file__, ' Online') @commands.command() - async def hex(self, ctx, action, *, text): + async def md5(self, ctx, action, *, text): # encode md5 if action == "encode" or action == "e": @@ -31,5 +31,5 @@ async def hex(self, ctx, action, *, text): return "Eroor" async def setup(client): - await client.add_cog(hex(client)) + await client.add_cog(md5(client)) diff --git a/cogs/mor.py b/cogs/mor.py index c80b3f0..730e193 100644 --- a/cogs/mor.py +++ b/cogs/mor.py @@ -66,7 +66,7 @@ async def on_ready(self): print(__file__, ' Online') @commands.command() - async def hex(self, ctx, action, *, text): + async def mor(self, ctx, action, *, text): # encode morse if action == "encode" or action == "e": @@ -82,5 +82,5 @@ async def hex(self, ctx, action, *, text): return [output,True] async def setup(client): - await client.add_cog(hex(client)) + await client.add_cog(mor(client)) From 4aed1026abfb8dea5d476ff5c00e212514f48a12 Mon Sep 17 00:00:00 2001 From: misarb Date: Sat, 27 Aug 2022 22:47:58 +0100 Subject: [PATCH 3/3] mor dicts --- cogs/md5.py | 3 +- cogs/mor.py | 109 +++++++++++++++++++++--------------------- cogs/template_file.py | 3 ++ main.py | 1 + 4 files changed, 60 insertions(+), 56 deletions(-) diff --git a/cogs/md5.py b/cogs/md5.py index e28cbb2..9e2634e 100644 --- a/cogs/md5.py +++ b/cogs/md5.py @@ -28,7 +28,8 @@ async def md5(self, ctx, action, *, text): if hashlib.md5(text.encode()).hexdigest() == text: return [text,True] else: - return "Eroor" + output = "Eroor" + return {output,True} async def setup(client): await client.add_cog(md5(client)) diff --git a/cogs/mor.py b/cogs/mor.py index 730e193..39e1d11 100644 --- a/cogs/mor.py +++ b/cogs/mor.py @@ -9,75 +9,74 @@ class mor(commands.Cog): def __init__(self, client): self.client = client # Dictionary representing the morse code - encode_table = { - "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": "--..", - "0": "-----", - "1": ".----", - "2": "..---", - "3": "...--", - "4": "....-", - "5": ".....", - "6": "-....", - "7": "--...", - "8": "---..", - "9": "----.", - ".": ".-.-.-", - ",": "--..--", - "?": "..--..", - " ": "SPACE", - } - #reverse encode tabale - decode_table = {v: k for k, v in encode_table.items()} - def encode(self,s): - enc = " ".join(self.encode_table[x] for x in s) - return enc.replace(" SPACE ", " ") - - def decode(self,encoded): - symbols = encoded.replace(" ", " SPACE ").split(" ") - return "".join(self.decode_table[x] for x in symbols) - @commands.Cog.listener() async def on_ready(self): print(__file__, ' Online') @commands.command() async def mor(self, ctx, action, *, text): - + cipher = '' + # Dictionary representing the morse code + MORSE_CODE_DICT = { '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':'-----', ', ':'--..--', '.':'.-.-.-', + '?':'..--..', '/':'-..-.', '-':'-....-', + '(':'-.--.', ')':'-.--.-'} # encode morse if action == "encode" or action == "e": # encode to morse - output = self.encode(text) + + for letter in text: + if letter != ' ': + output += MORSE_CODE_DICT[letter] + ' ' + else: + output += ' ' + # output = cipher await ctx.send(output) return [output, True] # decode mor if action == "decode" or action == "d": - output = self.decode(text) + text += ' ' + output = '' + citext = '' + for letter in text: + + # checks for space + if (letter != ' '): + + # counter to keep track of space + i = 0 + + # storing morse code of a single character + citext += letter + + # in case of space + else: + # if i = 1 that indicates a new character + i += 1 + + # if i = 2 that indicates a new word + if i == 2 : + + # adding space to separate words + output += ' ' + else: + # accessing the keys using their values (reverse of encryption) + output += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT + .values()).index(citext)] + citext = '' + await ctx.send(output) return [output,True] diff --git a/cogs/template_file.py b/cogs/template_file.py index 425ed42..85fa3ab 100644 --- a/cogs/template_file.py +++ b/cogs/template_file.py @@ -1,6 +1,8 @@ # IMPORTS from discord.ext import commands +from cogs.mor import mor + # INIT CLASS # should be the same name as the file class x(commands.Cog): @@ -31,6 +33,7 @@ async def x(self, ctx, action, *, text): elif action == "decode" or "d": # do the cipher code here + output = "output of the cipher" # this sends the result diff --git a/main.py b/main.py index adbe2bf..50d07f6 100644 --- a/main.py +++ b/main.py @@ -16,6 +16,7 @@ intents=intents) client.remove_command('help') + async def main(): async with client: