Skip to content

Commit

Permalink
Merge pull request #8 from Warhawk947/patch-4
Browse files Browse the repository at this point in the history
Add Polybius Cipher
  • Loading branch information
notnotrachit authored Mar 27, 2021
2 parents 9899b46 + e7c37b6 commit 9754973
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 20 deletions.
239 changes: 238 additions & 1 deletion cogs/cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import discord
import time
import string
import re
import typing
import asyncio
import sys; sys.path.append("..") # Recognize /utils as a package
from utils import *


alphabet = string.ascii_lowercase


class cipher(commands.Cog):
Expand Down Expand Up @@ -52,9 +53,245 @@ async def a1z26(self, ctx,*arg):
async def null(self, ctx,*,arg):
await ctx.send(f'```{null(arg)}```')

@commands.command()
async def polybius_encrypt(self, ctx,*, arg):
square = ''
embed = discord.Embed(title="Polybius Cipher: ", description="Set Alphabet", color=discord.Color(0x39ff14))
embed.add_field(name=":one:", value="Alphabet Presets")
embed.add_field(name=":two:", value="Keyword Alphabet")
embed.add_field(name=":three:", value="Enter your own alphabet")

msg = await ctx.send(embed=embed)

await msg.add_reaction('1️⃣')
await msg.add_reaction('2️⃣')
await msg.add_reaction('3️⃣')

def checkg(reaction, user):
return user == ctx.author

def check(m):
return m.author == ctx.author and m.channel == msg.channel


reaction, user = await self.bot.wait_for('reaction_add', timeout=20.0, check=checkg)


if str(reaction.emoji) == '1️⃣':
embed = discord.Embed(title="Polybius Cipher: ", description="Select Alphabet Presets", color=discord.Color(0x39ff14))
embed.add_field(name=":one:", value="Default Alphabet(Without the \"J\")", inline=False)
embed.add_field(name=":two:", value="Default Alphabet (Without the \"V\")", inline=False)
embed.add_field(name=":three:", value="Default Alphabet (Without the \"W\")", inline=False)
embed.add_field(name=":four:", value="Default Alphabet (Without the \"Q\")", inline=False)
embed.add_field(name=":five:", value="Default Alphabet (Without the \"Z\")", inline=False)
msg = await ctx.send(embed=embed)

await msg.add_reaction('1️⃣')
await msg.add_reaction('2️⃣')
await msg.add_reaction('3️⃣')
await msg.add_reaction('4️⃣')
await msg.add_reaction('5️⃣')


reaction, user = await self.bot.wait_for('reaction_add', timeout=20.0, check=checkg)
if str(reaction.emoji) == '1️⃣': square = alphabet.replace('j', '')
elif str(reaction.emoji) == '2️⃣': square = alphabet.replace('v', '')
elif str(reaction.emoji) == '3️⃣': square = alphabet.replace('w', '')
elif str(reaction.emoji) == '4️⃣': square = alphabet.replace('q', '')
elif str(reaction.emoji) == '5️⃣': square = alphabet.replace('z', '')



await ctx.send(f'✅ Your alphabet is: **{square}**')

elif str(reaction.emoji) == '2️⃣':
embed = discord.Embed(title="Polybius Cipher:", description="Create an alphabet from a keyword!", color=discord.Color(0x39ff14))
embed.add_field(name="Example: ", value="If you input \"cipher\", the alphabet will be cipherabdfgklmnoqstuvwxyz.", inline=False)
embed.add_field(name="Input: ", value="Please type in a keyword.")
await ctx.send(embed=embed)
msgi = await self.bot.wait_for('message', timeout=30.0, check=check)
msgi = msgi.content
await ctx.send(f'Using key: **{msgi}**')

# clean up the key

# Check for non alpha characters
msgi = msgi.lower()
msgii = re.sub(r'[^A-Za-z]', '', msgi)
if msgi != msgii:
await ctx.send(f'⚠ Warning: Your key contains non-alphabet characters. We have removed the non-alphabet characters from the key and the key is now: ```{msgii}```')
msgi = msgii


msgii = "".join(dict.fromkeys(msgi)) # See https://stackoverflow.com/a/9841328/14437456

if msgi != msgii:
await ctx.send(f'⚠ Warning: Your key contains duplicate letters. We have removed the duplicate letters from the key and the key is now: ```{msgii}```')
msgi = msgii



square = msgi
square = square.replace('j', '')

for char in alphabet:
if char not in square and char != 'j':
square += char

await ctx.send(f'✅ Your alphabet is: **{square}**')

elif str(reaction.emoji) == '3️⃣':
embed = discord.Embed(title="Polybius Cipher: ", description="Create your own alphabet!", color=discord.Color(0x39ff14))
embed.add_field(name="Feel like the alphabet presets are too dull? Don't want to create an alphabet from a keyword? Well this section is for you!", value="Enter your own custom made brand new shiny alphabet here!", inline=False)
embed.add_field(name="Requirements: ", value="Your alphabet must be excatly 25 characters long, not contain any duplicate characters, and contain only alphabet characters.", inline=False)
await ctx.send(embed=embed)

msgi = await self.bot.wait_for('message', timeout=60.0, check=check)
msgi = msgi.content
await ctx.send(f'Using Alphabet: **{msgi}**')

msgi = msgi.lower()


msgii = re.sub(r'[^A-Za-z]', '', msgi)
if msgi != msgii:
await ctx.send(f'⚠ Warning: Your alphabet contains non-alphabet characters. We have removed the non-alphabet characters from the alphabet and the alphabet is now: ```{msgii}```')
msgi = msgii


msgii = "".join(dict.fromkeys(msgi)) # See https://stackoverflow.com/a/9841328/14437456

if msgi != msgii:
await ctx.send(f'⚠ Warning: Your alphabet contains duplicate letters. We have removed the duplicate letters from the alphabet and the alphabet is now: ```{msgii}```')
msgi = msgii

if len(msgi) != 25:
return await ctx.send(f'❌ Error: Your alphabet is not 25 characters long! Aborting...')

square = msgi



await ctx.send(f'```{polybius_encrypt(arg, square)}```')


@commands.command()
async def polybius_decrypt(self, ctx,*, arg):
square = ''
embed = discord.Embed(title="Polybius Cipher: ", description="Set Alphabet", color=discord.Color(0x39ff14))
embed.add_field(name=":one:", value="Alphabet Presets")
embed.add_field(name=":two:", value="Keyword Alphabet")
embed.add_field(name=":three:", value="Enter your own alphabet")

msg = await ctx.send(embed=embed)

await msg.add_reaction('1️⃣')
await msg.add_reaction('2️⃣')
await msg.add_reaction('3️⃣')

def checkg(reaction, user):
return user == ctx.author

def check(m):
return m.author == ctx.author and m.channel == msg.channel


reaction, user = await self.bot.wait_for('reaction_add', timeout=20.0, check=checkg)


if str(reaction.emoji) == '1️⃣':
embed = discord.Embed(title="Polybius Cipher: ", description="Select Alphabet Presets", color=discord.Color(0x39ff14))
embed.add_field(name=":one:", value="Default Alphabet(Without the \"J\")", inline=False)
embed.add_field(name=":two:", value="Default Alphabet (Without the \"V\")", inline=False)
embed.add_field(name=":three:", value="Default Alphabet (Without the \"W\")", inline=False)
embed.add_field(name=":four:", value="Default Alphabet (Without the \"Q\")", inline=False)
embed.add_field(name=":five:", value="Default Alphabet (Without the \"Z\")", inline=False)
msg = await ctx.send(embed=embed)

await msg.add_reaction('1️⃣')
await msg.add_reaction('2️⃣')
await msg.add_reaction('3️⃣')
await msg.add_reaction('4️⃣')
await msg.add_reaction('5️⃣')


reaction, user = await self.bot.wait_for('reaction_add', timeout=20.0, check=checkg)
if str(reaction.emoji) == '1️⃣': square = alphabet.replace('j', '')
elif str(reaction.emoji) == '2️⃣': square = alphabet.replace('v', '')
elif str(reaction.emoji) == '3️⃣': square = alphabet.replace('w', '')
elif str(reaction.emoji) == '4️⃣': square = alphabet.replace('q', '')
elif str(reaction.emoji) == '5️⃣': square = alphabet.replace('z', '')



await ctx.send(f'✅ Your alphabet is: **{square}**')

elif str(reaction.emoji) == '2️⃣':
embed = discord.Embed(title="Polybius Cipher:", description="Create an alphabet from a keyword!", color=discord.Color(0x39ff14))
embed.add_field(name="Example: ", value="If you input \"cipher\", the alphabet will be cipherabdfgklmnoqstuvwxyz.", inline=False)
embed.add_field(name="Input: ", value="Please type in a keyword.")
await ctx.send(embed=embed)
msgi = await self.bot.wait_for('message', timeout=30.0, check=check)
msgi = msgi.content
await ctx.send(f'Using key: **{msgi}**')

# clean up the key

# Check for non alpha characters
msgi = msgi.lower()
msgii = re.sub(r'[^A-Za-z]', '', msgi)
if msgi != msgii:
await ctx.send(f'⚠ Warning: Your key contains non-alphabet characters. We have removed the non-alphabet characters from the key and the key is now: ```{msgii}```')
msgi = msgii


msgii = "".join(dict.fromkeys(msgi)) # See https://stackoverflow.com/a/9841328/14437456

if msgi != msgii:
await ctx.send(f'⚠ Warning: Your key contains duplicate letters. We have removed the duplicate letters from the key and the key is now: ```{msgii}```')
msgi = msgii



square = msgi
square = square.replace('j', '')

for char in alphabet:
if char not in square and char != 'j':
square += char

await ctx.send(f'✅ Your alphabet is: **{square}**')

elif str(reaction.emoji) == '3️⃣':
embed = discord.Embed(title="Polybius Cipher: ", description="Create your own alphabet!", color=discord.Color(0x39ff14))
embed.add_field(name="Feel like the alphabet presets are too dull? Don't want to create an alphabet from a keyword? Well this section is for you!", value="Enter your own custom made brand new shiny alphabet here!", inline=False)
embed.add_field(name="Requirements: ", value="Your alphabet must be excatly 25 characters long, not contain any duplicate characters, and contain only alphabet characters.", inline=False)
await ctx.send(embed=embed)

msgi = await self.bot.wait_for('message', timeout=60.0, check=check)
msgi = msgi.content
await ctx.send(f'Using Alphabet: **{msgi}**')

msgi = msgi.lower()


msgii = re.sub(r'[^A-Za-z]', '', msgi)
if msgi != msgii:
await ctx.send(f'⚠ Warning: Your alphabet contains non-alphabet characters. We have removed the non-alphabet characters from the alphabet and the alphabet is now: ```{msgii}```')
msgi = msgii


msgii = "".join(dict.fromkeys(msgi)) # See https://stackoverflow.com/a/9841328/14437456

if msgi != msgii:
await ctx.send(f'⚠ Warning: Your alphabet contains duplicate letters. We have removed the duplicate letters from the alphabet and the alphabet is now: ```{msgii}```')
msgi = msgii

if len(msgi) != 25:
return await ctx.send(f'❌ Error: Your alphabet is not 25 characters long! Aborting...')

square = msgi
await ctx.send(f'```{polybius_decrypt(arg, square)}```')

def setup(bot):
bot.add_cog(cipher(bot))
Expand Down
30 changes: 14 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "Re-dcrypt Bot v0.9.8 Beta"
version = "Re-dcrypt Bot v0.9.9 Beta"


print(f'Starting {version}...')
Expand Down Expand Up @@ -31,6 +31,7 @@
'tap':['```&?tap [code/text] \n(This will automatically decode the tap code & encode the text)```',"For eg. `&?tap ... .... ... ..... . ..... ... ... / .. ... . . ... ..... ... ..... .. .... ... ... . ..... .... ... .... ... `"],
'bacon':["```&?bacon_encode [text]\n&?bacon_decode [code]\n&?bacon_encode [complete] [text]\n&?bacon_decode [complete] [code]\n\nNote: By default Bacon cipher will decrypt/encrypt in the standard (I=J, U=V) form. If you need to encrypt/decrypt in complete form of bacon cipher then you need to mention it.```","For eg. `&?bacon_decode AABBA ABBAB ABBAB AABBA ABABA AABAA ` or `&?bacon_encode Google`"],
'reverse':['```&?reverse [text]```', "For eg. `&?reverse olleh`"],
'polybius':["```&?polybius_encrypt [text]\n&?polybius_decrypt [code]\n\nNote: Since Polybius Cipher uses a custom alphabet, there will be a choice to use a default alphabet, create an alphabet from a keyword (If you input \"cipher\", the alphabet will be cipherabdfgklmnoqstuvwxyz), and to input your own alphabet.```", "For eg. `&polybius_encrypt hello world`, `&>polybius_decrypt 34 20 52 20 43 22 40 34 34 11 22 24 52 20 60 40 51 51 41`"],
'slash':"```You can use slash commands as well. They are much more simpler to use & don't require you to remember the commands. The format is predifened & you just have to select the things.```"

}
Expand All @@ -52,56 +53,53 @@ def helpcmd(arg):
embed.add_field(name='Tap Code',value=f"{help_dict['tap'][0]}")
embed.add_field(name='Bacon/Baconian Cipher',value=f"{help_dict['bacon'][0]}", inline=False)
embed.add_field(name='Text Reverse',value=f"{help_dict['reverse'][0]}", inline=True)
embed.add_field(name='Polybius Cipher',value=f'{help_dict["polybius"][0]}', inline=False)
embed.add_field(name='Feedback/Suggestion',value='''```&?feedback [your feedback/suggestion]```''',inline=True)
embed.add_field(name='Note',value='The Prefix `&?` is only required in servers. All the commands will work without the prefix in the bots DM.',inline=False)
embed.add_field(name='Slash Commands',value="""```diff\n+ Most of the above commands are available as slash commands as well. For Slash commands, you just have to type `/` and then you will see multiple options for the commands. The format is also predefined in the slash commands so you don't have to remember anything.\n```""")
embed.add_field(name='About:',value='''***Re-Dcrypt Bot v0.9.8 Beta***\n<a:BlobDiscord:779402415916580864> [Invite Me](https://discord.com/api/oauth2/authorize?client_id=775629409494630410&permissions=346176&redirect_uri=https%3A%2F%2Fbot.redcrypt.ml&scope=bot%20applications.commands)\n<a:redcryptexcited:781090077748494336> [Our Community Server](https://discord.gg/c68aWWMruT)''',inline=False)

elif arg.lower().startswith('caesar'):
embed.add_field(name='Caesar Cipher Decode', value=f"{help_dict['Caesar'][0]}\n {help_dict['Caesar'][1]}", inline=False)
#embed.add_field(name='Note',value='The Prefix `&?` is only required in servers. All the commands will work without the prefix in the bots DM.',inline=False)
embed.add_field(name='Slash Commands', value=f"{help_dict['slash']}", inline=False)


elif arg.lower().startswith('vigenere'):
embed.add_field(name='Vigenere Cipher', value=f"{help_dict['Vigenere'][0]}\n {help_dict['Vigenere'][1]}", inline=False)
#embed.add_field(name='Note',value='The Prefix `&?` is only required in servers. All the commands will work without the prefix in the bots DM.',inline=False)
embed.add_field(name='Slash Commands', value=f"{help_dict['slash']}", inline=False)

elif arg.lower().startswith('a1z26'):
embed.add_field(name='a1z26', value=f"{help_dict['a1z26'][0]}\n {help_dict['a1z26'][1]}", inline=False)
#embed.add_field(name='Note',value='The Prefix `&?` is only required in servers. All the commands will work without the prefix in the bots DM.',inline=False)
embed.add_field(name='Slash Commands', value=f"{help_dict['slash']}", inline=False)

elif arg.lower().startswith('base64'):
embed.add_field(name='Base64', value=f"{help_dict['base64'][0]}\n {help_dict['base64'][1]}", inline=False)
#embed.add_field(name='Note',value='The Prefix `&?` is only required in servers. All the commands will work without the prefix in the bots DM.',inline=False)
embed.add_field(name='Slash Commands', value=f"{help_dict['slash']}", inline=False)

elif arg.lower().startswith('null'):
embed.add_field(name='null', value=f"{help_dict['null'][0]}\n {help_dict['null'][1]}", inline=False)
#embed.add_field(name='Note',value='The Prefix `&?` is only required in servers. All the commands will work without the prefix in the bots DM.',inline=False)
embed.add_field(name='Slash Commands', value=f"{help_dict['slash']}", inline=False)

elif arg.lower().startswith('atbash'):
embed.add_field(name='Atbash Cipher', value=f"{help_dict['atbash'][0]}\n {help_dict['atbash'][1]}", inline=False)
#embed.add_field(name='Note',value='The Prefix `&?` is only required in servers. All the commands will work without the prefix in the bots DM.',inline=False)
embed.add_field(name='Slash Commands', value=f"{help_dict['slash']}", inline=False)

elif arg.lower().startswith('morse'):
embed.add_field(name='Morse Code', value=f"{help_dict['morse'][0]}\n {help_dict['morse'][1]}", inline=False)
#embed.add_field(name='Note',value='The Prefix `&?` is only required in servers. All the commands will work without the prefix in the bots DM.',inline=False)
embed.add_field(name='Slash Commands', value=f"{help_dict['slash']}", inline=False)

elif arg.lower().startswith('tap'):
embed.add_field(name='Tap Code', value=f"{help_dict['tap'][0]}\n {help_dict['tap'][1]}", inline=False)
#embed.add_field(name='Note',value='The Prefix `&?` is only required in servers. All the commands will work without the prefix in the bots DM.',inline=False)
embed.add_field(name='Slash Commands', value=f"{help_dict['slash']}", inline=False)

elif arg.lower().startswith('bacon'):
embed.add_field(name='Bacon Cipher', value=f"{help_dict['bacon'][0]}\n {help_dict['bacon'][1]}", inline=False)
#embed.add_field(name='Note',value='The Prefix `&?` is only required in servers. All the commands will work without the prefix in the bots DM.',inline=False)
embed.add_field(name='Slash Commands', value=f"{help_dict['slash']}", inline=False)

elif arg.lower().startswith('polybius'):
embed.add_field(name='Polybus Cipher: ', value=f'{help_dict["polybius"][0]}\n {help_dict["polybius"][1]}', inline=False)
embed.add_field(name='Slash Commands', value=f"{help_dict['slash']}", inline=False)


else:
embed.add_field(name="Error the command that you entered could not be found", value="[You can type `&?help` to see a list of all commands](https://bit.ly/2PbNIbf)")
Expand Down Expand Up @@ -160,11 +158,11 @@ def command_prefix(bot, message):
if message.guild is None:
return ''
else:
return '&?'
return '&>'


async def get_prefix(ctx):
return '' if ctx.guild is None else '&?'
return '' if ctx.guild is None else '&>'

bot = commands.Bot(command_prefix)
slash = SlashCommand(bot, sync_commands=True)
Expand All @@ -179,17 +177,17 @@ async def get_prefix(ctx):
@bot.event
async def on_ready():
print(f'Logged in as {bot.user}!')
activity = discord.Game(name=f"&?help | on {len(bot.guilds)} servers")
activity = discord.Game(name=f"&>help | on {len(bot.guilds)} servers")
await bot.change_presence(activity=activity)

@bot.event
async def on_guild_join(self):
activity = discord.Game(name=f"&?help | on {len(bot.guilds)} servers")
activity = discord.Game(name=f"&>help | on {len(bot.guilds)} servers")
await bot.change_presence(activity=activity)

@bot.event
async def on_guild_remove(self):
activity = discord.Game(name=f"&?help | on {len(bot.guilds)} servers")
activity = discord.Game(name=f"&>help | on {len(bot.guilds)} servers")
await bot.change_presence(activity=activity)


Expand Down Expand Up @@ -248,4 +246,4 @@ async def feedback(ctx,*, cont):
bot.load_extension("cogs.slash")

print('Firing up the Flux Capacitors...')
bot.run(os.getenv('TOKEN'))
bot.run(os.getenv('BETA'))
Loading

0 comments on commit 9754973

Please sign in to comment.