Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Application Commands #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
276 changes: 276 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,282 @@ async def on_message_edit(before, after):
ctx = await bot.get_context(after)
await bot.invoke(ctx)

@bot.user_command(name="Join Position")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Command is extraneous, already exists and in better implementation.

async def _joinpos(ctx, member: discord.Member):
all_members = list(ctx.guild.members)
all_members.sort(key=lambda m: m.joined_at)

def ord(n):
return str(n) + ("th" if 4 <= n % 100 <= 20 else {
1: "st",
2: "nd",
3: "rd"
}.get(n % 10, "th"))

embed = discord.Embed(
title="Member info",
description=
f"{member.mention} was the {ord(all_members.index(member) + 1)} person to join",
)
await ctx.send(embed=embed)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

send() is incorrect here.



MORSE_CODE_DICT = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, this is stolen from https://www.geeksforgeeks.org/morse-code-translator-python/ (which was arguably stolen from https://www.tutorialspoint.com/morse-code-translator-in-python but that's another story). Anyways, I'll likely write a better implementation of this that allows for a universal encode/decode/compile command with subcommands for each language, closer supporting my brainfuck commands with this. Same goes for all the other encode/decode commands here.

"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": "-----",
", ": "--..--",
".": ".-.-.-",
"?": "..--..",
"/": "-..-.",
"-": "-....-",
"(": "-.--.",
")": "-.--.-",
"!": "-.-.--",
",": "--..--",
}

# we make a list of what to replace with what


# Function to encrypt the string
# according to the morse code chart
def encrypt(message):
cipher = ""
for letter in message:
if letter != " ":

# Looks up the dictionary and adds the
# correspponding morse code
# along with a space to separate
# morse codes for different characters
cipher += MORSE_CODE_DICT[letter] + " "
else:
# 1 space indicates different characters
# and 2 indicates different words
cipher += " "

return cipher


# Function to decrypt the string
# from morse to english
def decrypt(message):

# extra space added at the end to access the
# last morse code
message += " "

decipher = ""
citext = ""
for letter in message:

# 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
decipher += " "
else:

# accessing the keys using their values (reverse of encryption)
decipher += list(MORSE_CODE_DICT.keys())[list(
MORSE_CODE_DICT.values()).index(citext)]
citext = ""

return decipher


@bot.message_command(name="Encrypt to Morse")
async def _tomorse(ctx, message: discord.message):
result = encrypt(message.content.upper())
await ctx.send(result)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

send() is incorrect here.



@bot.message_command(name="Decrypt Morse")
async def _frommorse(ctx, message: discord.message):
result = decrypt(message.content)
await ctx.send(result)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

send() is incorrect here.



@bot.message_command(name="Decrypt binary")
async def _frombinary(ctx, message: discord.message):

if message.content.lower() == "01000000 01100101 01110110 01100101 01110010 01111001 01101111 01101110 01100101":
await ctx.respond("SMH. Allowed mentions are turned off. go do something better.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is childish, and doesn't even work as intended as someone could easily add another character to bypass this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, I forgot to remove that Easter Egg in the PR, besides, allowed mentions are turned off so it wouldn't actually be Bypassed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's part of my point. There isn't any need for this so why have it.

else:
a_binary_string = message.content
binary_values = a_binary_string.split()

ascii_string = ""
for binary_value in binary_values:
an_integer = int(binary_value, 2)

ascii_character = chr(an_integer)

ascii_string += ascii_character

await ctx.send(ascii_string,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

send() is incorrect here.

allowed_mentions=discord.AllowedMentions.none())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The allowed_mentions kwarg is redundant, it's set by default in the custom bot class.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see



@bot.message_command(name="Encrypt to binary")
async def _tobinary(ctx, message: discord.message):
if message.content.lower() == 'bruce':
await ctx.respond("01010000 01101111 01100111 01100111 01100101 01110010 01110011 00101110")
elif message.content.lower() == 'easter egg':
await ctx.respond("01010000 01110010 01101111 01100010 01100001 01100010 01101100 01111001 00100000 01101110 01101111 01110100")

elif message.content.lower() == '@everyone':
await ctx.respond("Wow, you though allowed mentions were on? Smh.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is childish, and doesn't even work as intended as someone could easily add another character to bypass this. Same goes for all of the other checks here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They aren't checks, just little easter eggs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An if statement is a check.
Screen Shot 2021-10-01 at 10 27 58 AM

else:
a_string = message.content
a_byte_array = bytearray(a_string, "utf8")
byte_list = []

for byte in a_byte_array:
binary_representation = bin(byte)
byte_list.append(binary_representation)

await ctx.send(" ".join(byte_list))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

send() is incorrect here.



# ------
# Commented because max commands reached
# ------

# @bot.slash_command(name="Decrypt from hex", guild_ids=[869782707226439720, 881207955029110855])
# async def _fromhex(ctx, message:discord.message):
# hex_string = message.content[2:]

# bytes_object = bytes.fromhex(hex_string)

# ascii_string = bytes_object.decode("ASCII")

# await ctx.send(ascii_string)

# @bot.message_command(name="Encrypt to hex")
# async def _tohex(ctx, message:discord.message):
# hex_string = message.content
# an_integer = int(hex_string, 16)
# hex_value = hex(an_integer)
# await ctx.send(hex_value)


@bot.user_command(name="Avatar")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just respond with the link, no need to take up more space with an embed. This bot isn't intended to be fancy, it's supposed to be useful, fun, and simple.

async def _avatar(ctx, member: discord.Member):
embed = discord.Embed(
title=f"{member}'s avatar!",
description=f"[Link]({member.avatar.url})",
color=member.color,
)
try:
embed.set_image(url=member.avatar.url)
except AttributeError:
embed.set_image(url=member.display_avatar.url)
await ctx.send(embed=embed)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

send() is incorrect here.



binary = bot.command_group("binary", "Set of tools for converting binary")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment on the morse code commands.



@binary.command(name="encrypt")
async def binary_encrypt(ctx,
text: Option(
str, "The string you want to convert to binary")):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This formatting hurts to look at.

a_string = text
a_byte_array = bytearray(a_string, "utf8")
byte_list = []

for byte in a_byte_array:
binary_representation = bin(byte)
byte_list.append(binary_representation[2:])

await ctx.send(" ".join(byte_list))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

send() is incorrect here.



@binary.command(name="decrypt")
async def binary_decrypt(
ctx, text: Option(str, "The binary string you want to decrypt")):
a_binary_string = text
binary_values = a_binary_string.split()

ascii_string = ""
for binary_value in binary_values:
an_integer = int(binary_value, 2)

ascii_character = chr(an_integer)

ascii_string += ascii_character

await ctx.send(ascii_string,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

send() is incorrect here.

allowed_mentions=discord.AllowedMentions.none())

slowmode = bot.command_group(name='slowmode', description="Slowmode related commands for moderators")

@slowmode.command(name='set', description='Set the slowmode of the current channel')
@commands.has_role(881407111211384902)
async def set(ctx, time:Option(int, 'Enter the time in seconds')):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use time_str here. It's a module I wrote for exactly this kind of thing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright

if time > 21600:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using datetime timedelta objects is more readable here. time_str uses timedelta.

await ctx.respond(content=f"Slowmode of a channel must be {humanize.precisedelta(21600)} (21600 seconds) or less.", ephemeral=True)
else:
await ctx.channel.edit(slowmode_delay=time)
await ctx.respond(f"The slowmode of this channel has been changed to {humanize.precisedelta(time)} ({time}s)")

@slowmode.command(name='off', description='Remove the slowmode from the current channel')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There shouldn't be 2 commands for this. Use an optional parameter in one command that defaults to 0.

@commands.has_role(881407111211384902)
async def off(ctx):
if ctx.channel.slowmode_delay == 0:
await ctx.respond(content="This channel doesn't have a slowmode. Use `/slowmode set` to set a slowmode.", ephemeral=True)
await ctx.channel.edit(slowmode_delay=0)
await ctx.respond("Removed the slowmode from this channel!")


if __name__ == "__main__":
bot.run()
Expand Down