async def _roles_remove(self, ctx, role_name=None, role=None): """/roles remove""" if role_name is not None: if role_name[0] == "!": role_name = role[1::] actual_role = await (await Roles_DB(ctx.guild.id )).grab_role(command=role_name) actual_role = actual_role["role_id"] if actual_role is None: embed = create_embed_template( "You did not provide a proper role command.", "Check all of the role commands with !roles.", "error", ) await ctx.send(embed=embed, hidden=True) else: result = await remove_role(ctx, actual_role) await ctx.send(embed=result[1], hidden=True) elif role is not None: result = await remove_role(ctx, role.id) await ctx.send(embed=result[1], hidden=True) else: await ctx.send( embed=create_embed_template("You did not provide any values.", "", "error"), hidden=True, )
async def programs(ctx, bot, user: str) -> Tuple[bool, discord.Embed]: """Display's a user's programs Args: ctx (discord.ext.commands.Context): Context bot (discord.ClientUser.bot): Bot user (str): User's programs to display Returns: Tuple[bool, discord.Embed]: [Status: bool, Embed: discord.Embed] """ # Check if user is None: return ( False, create_embed_template("Invalid Arguments", "User was Null.", "error"), ) # Gets the user's id user = parse_id(user) db = await Programs_DB(ctx.guild.id) # Grabs all of the programs programs_list = await db.grab_programs(user) # Empty list if programs_list is None: return ( False, create_embed_template( "Invalid User", "That user doesn't have any programs, have them use /programs add.", "error", ), ) programs_list = programs_list.split("\n") # Creates the message message = "" space_amount = (len(programs_list) // 10) + 1 for i in range(len(programs_list)): if programs_list[i] != "" and programs_list[i] is not None: # Ensure the numbers line up number = f"{i + 1}".rjust(space_amount, " ") # Only add a newline if it's required message += f"{number}. {programs_list[i]}" + ( "\n" if i != (len(programs_list) - 1) else "") user = bot.get_user(user) return ( True, create_embed(f"{user.name}#{user.discriminator}'s Programs", message, "orange"), )
async def add_role(ctx: discord.ext.commands.Context, name: str, role_id: int): """Adds a role to the database Args: ctx (discord.ext.commands.Context): Context name (str): Name of the role (denominator) role_id (int): The role's ID """ if ctx.author.guild_permissions.administrator is False: return ( False, create_embed_template( "No Permission", "You do not have permission to add a role. Contact an administrator.", "error", ), ) db = await Roles_DB(ctx.guild.id) if await db.check_role(role_id, name): for role in ctx.guild.roles: if role.name == "Borg" or role.name == "Borg Test": borg_role = {"id": role.id, "position": role.position} actual_role = ctx.guild.get_role(role_id) if borg_role["position"] > actual_role.position: await db.add_role(role_id, name) embed = create_embed("Role Added", "", "light_green") add_field(embed, "Role", actual_role.mention, True) add_field(embed, "Command", f"!role {name}", True) return True, embed else: return ( False, create_embed_template( "Bot Doesn't have Permission.", "The bot does not have permission to give that role to users. Ensure the bot's role (@Borg) is **ABOVE** the role in the 'Roles' part of your server's dashboard.", "error", ), ) else: return ( False, create_embed_template( "Already Created", "There is already a command associated with **either** that role or command name. Check with !roles.", "error", ), )
async def roles( ctx: discord.ext.commands.Context, bot: discord.ClientUser.bot) -> Tuple[bool, discord.Embed] or bool: """Lists out this server's roles Args: ctx (discord.ext.commands.Context): Context bot (discord.ClientUser.bot): Bot Returns: Union(bool, discord.Embed): [Status: bool, Embed: discord.Embed] """ db = await Roles_DB(ctx.guild.id) try: all_roles = [ f"!role {i[1]} - {ctx.guild.get_role(i[0]).mention}" for i in await db.grab_roles() ] except: return ( False, create_embed_template( "No Roles", "This server has no roles. Have an administrator run /roles create.", "error", ), ) if len(all_roles) == 0: return ( False, create_embed_template( "No Roles", "This server has no roles. Have an administrator run /roles create.", "error", ), ) await page_command(ctx, bot, all_roles, "Roles") return True
async def remove_role(ctx, role_id: int): """Removes a role from the database Args: ctx (discord.ext.commands.Context): Context role_id (int): The ID of the role """ if ctx.author.guild_permissions.administrator is False: return ( False, create_embed_template( "No Permission.", "You do not have permission to remove a role. Ask an administrator.", "error", ), ) db = await Roles_DB(ctx.guild.id) removal_role = await db.grab_role(role_id=role_id) if removal_role is None: return ( False, create_embed_template( "No Command.", "There is no command associated with that role in this server.", "error", ), ) await db.remove_role(role_id) embed = create_embed("Role Removed", "", "dark_blue") add_field(embed, "Role", f"{ctx.guild.get_role(removal_role['role_id']).mention}", True) add_field(embed, "Command", f"!role {removal_role['command']}", True) return [True, embed]
async def custom_command_list(bot: discord.ClientUser.bot, ctx: discord.ext.commands.Context): """Displays the custom commands. Args: bot (discord.ClientUser.bot): Bot instance ctx (discord.ext.commands.Context): Context for the call Returns: - bool: Whether the command list was displayed - Tuple[bool, discord.Embed]: bool is the status code, embed is the response message """ # Database db = await Commands_DB(ctx.guild.id) # List of commands grab_commands = await db.grab_commands() if grab_commands is None: return ( False, create_embed_template( "No Commands", "There are currently no commands, ask an admin to use /command create or !command add.", "error", ), ) command_list = [i[0] for i in grab_commands] # Length of command list command_list_length = len(command_list) message = "" # Display the commands with the denominator for i in range(command_list_length): message += ("!" + command_list[i] + ("\n" if i + 1 != command_list_length else "")) await page_command(ctx, bot, message.split("\n"), "Commands") return True
async def role_toggle(ctx: discord.ext.commands.Context, role: str) -> Tuple[bool, discord.Embed]: """Handling for the !role command Args: ctx (discord.ext.commands.Context): Context role (str): The role's denominator Returns: Tuple[bool, discord.Embed]: [Status: bool, Embed: discord.Embed] """ # Grab the user's roles user_roles = [i.id for i in ctx.author.roles] db = await Roles_DB(ctx.guild.id) # Grab this server's role commands role_id = await db.grab_role(command=role) if role_id is None: return ( False, create_embed_template("Invalid Role", "Use !roles to see all of the roles.", "error"), ) role_id = role_id[1] actual_role = ctx.guild.get_role(role_id) if role_id in user_roles: # Add the role try: await ctx.author.remove_roles(actual_role) except: return ( False, create_embed_template( "No Permission.", "Borg doesn't have permission to add/remove that role. Ensure Borg's role is **above** the role you're trying to toggle in the server settings (Contact an administrator)", "error", ), ) embed = create_embed("Removed Role", f"You removed the {actual_role.mention} role.", "dark_blue") return True, embed else: # Remove the role try: await ctx.author.add_roles(actual_role) except: return ( False, create_embed_template( "No Permission.", "Borg doesn't have permission to add/remove that role. Ensure Borg's role is **above** the role you're trying to toggle in the server settings (Contact an administrator).", "error", ), ) embed = create_embed("Added Role", f"You added the {actual_role.mention} role.", "light_green") return True, embed
async def custom_command_add(ctx: discord.ext.commands.Context, name: str, description: str, image: str or None) -> Tuple[bool, discord.Embed]: """Adds a command to the database Args: ctx (discord.ext.commands.Context): Context name (str): Name of the command to add description (str): Description of the command image (str or None): Image link for the command Returns: Tuple[bool, discord.Embed]: [Status: bool, Embed: discord.Embed] """ name = name.lower() name = name[1::] if name[0] == "!" else name # Permissions check if ctx.author.guild_permissions.administrator is False: return ( False, create_embed_template( "No Permission", "You do not have permission to create a command.", "error", ), ) urls = [] if image: # Checks for an actual photo in "image" photo_indicators = ( "tenor", "jpeg", "jpg", "png", "gif", "webp", "giphy", "tiff", "nef", "cr2", "arw", ) # Extracts the urls from the "image" variable urls = URLExtract().find_urls(image) if len(urls) == 1: # Check to ensure the single URL is actually an image image = urls[0] if not (image.startswith("http") and set(image.split(".")).intersection(set(photo_indicators))): return ( False, create_embed_template("Invalid Image", "Make sure your link is to an image.", "error"), ) elif len(urls) == 2: # Too many URLs return ( False, create_embed_template( "Too many Links", "Borg can only support 1 image per command.", "error"), ) else: urls = None db = await Commands_DB(ctx.guild.id) # Find all of the current commands grab_commands = await db.grab_commands() if grab_commands is not None: command_list = [i[0] for i in grab_commands] # Ensure the current command is unique if name in command_list: return ( False, create_embed_template( "Invalid Command Name", "There is already a command with that denominator.", "error", ), ) # Add it to the database await db.add_command(name, description, image) return ( True, create_embed( "Success", f"Command successfully created, run it with !{name}", "light_green", ), )
async def custom_command_remove(ctx: discord.ext.commands.Context, command: str) -> Tuple[bool, discord.Embed]: """Removes a custom command from the server's database Args: ctx (discord.ext.commands.Context): Context command (str): The name of the command to remove Returns: Tuple[bool, discord.Embed]: [Status: bool, Embed: discord.Embed] """ # Permissions check if ctx.author.guild_permissions.administrator is False: return ( False, create_embed_template( "No Permission", "You do not have permission to create commands.", "error", ), ) db = await Commands_DB(ctx.guild.id) # Removes the ! from the command -> !hello turns into hello if command[0] == "!": command = command[1::] # Grabs all of the commands grab_commands = await db.grab_commands() if grab_commands is None: return ( False, create_embed_template( "Invalid Command", "This server has no commands therefore you cannot remove one.", "error", ), ) command_list = [i[0] for i in grab_commands] if command in command_list: # Grabs the info for the command to be deleted command_delete = await db.fetch_command(command) # Deletes await db.remove_command(command) return ( True, create_embed( "Command Successfully Deleted", f"Command Deleted: !{command_delete[0]}", "light_green", ), ) else: return ( False, create_embed_template("Invalid Command", "Check the commands with !commands.", "error"), )
async def _programs(self, ctx): """!programs command""" content = ctx.message.content.split(" ") if len(content) == 1: embed = create_embed( "Command: !programs", "**Description**: Allows you to interact with programs commands.\n**Sub Commands**:\n!programs {user} - Allows you to see a user's programs. (ex: !programs <@749359897405161522>)\n!programs add - Allows you to add programs to your list.\n!programs remove - Allows you to remove programs from your list.\n!programs edit - Allows you to edit one of your programs.\n!commands setup - Allows an admin to setup a programs confirmation channel.", "orange", ) await ctx.send(embed=embed) return subcommand = content[1] def grab_user(content): user = re.search(r"<@(?:&|!|)[0-9]{18}>", " ".join(content)) if not user: return None, content else: content = " ".join(content).replace(user, "").split(" ") user = parse_id(user.group()) return user, content if subcommand == "add" or subcommand == "a": #: !programs add Queens CS if len(content) == 2: embed = create_embed( "Command: !programs add", "**Description**: Allows you to add programs to your list.\n**Usage**:\n!programs add Queens CS, Mcgill CS, UW CS\n!programs add Queens CS, UW CS <@749359897405161522>", "orange", ) await ctx.send(embed=embed) else: content = content[2::] user, content = grab_user(content) if user is None: user = ctx.author.id potential_programs = " ".join(content).split("\n") if len(potential_programs) == 1: potential_programs = [ i.strip() for i in potential_programs[0].split(",") ] else: p = [] for i in potential_programs: additions = [g.strip() for g in i.split(",")] p.extend(additions) potential_programs = p result = await programs_add(ctx, self.bot, potential_programs, user) await ctx.send(embed=result[1]) elif subcommand == "remove" or subcommand == "r": if len(content) == 2: embed = create_embed( "Command: !programs remove", "**Description**: Allows you to remove programs from your list. Provide a comma seperated list of numbers, or * to remove all of your programs.\n**Usage**:\n!programs remove 1, 2, 3\n!programs remove * <@749359897405161522>", "orange", ) await ctx.send(embed=embed) return content = content[2::] user, content = grab_user(content) if user is None: user = ctx.author.id result = await programs_remove(ctx, " ".join(content), user) await ctx.send(embed=result[1]) elif subcommand == "edit" or subcommand == "e": #: !command edit 1 New Program if len(content) < 4: embed = create_embed( "Command: !programs edit", "**Description**: Allows you to edit a program in your list. Provide the number for the program, and the new text.\n**Usage**:\n!programs edit 1 Queens CS\n!programs edit 1 Queens CS <@749359897405161522>", "orange", ) await ctx.send(embed=embed) return content = content[2::] user, content = grab_user(content) if user is None: user = ctx.author.id before = content[0] after = " ".join(content[1::]) result = await programs_edit(ctx, self.bot, user, before, after) await ctx.send(embed=result[1]) elif subcommand == "setup" or subcommand == "s": #: !command setup <#846962522065993768> if len(content) == 2 or len(content) > 3: embed = create_embed( "Command: !programs setup", "**Description**: Allows an administrator to setup a programs verification channel.\n**Usage**:\n!programs setup <@799848795050606607>", "orange", ) await ctx.send(embed=embed) return channel_id = parse_id(content[2]) if not channel_id: embed = create_embed_template("Invalid Setup Channel", "", "error") await ctx.send(embed=embed) return result = await programs_setup(ctx, channel_id) await ctx.send(embed=result[1]) else: attempt_user = parse_id(subcommand) if attempt_user: user_id = attempt_user else: user_id = ctx.guild.get_member_named(subcommand).id p = await programs(ctx, self.bot, user_id) await ctx.send(embed=p[1])
async def programs_remove(ctx: discord.ext.commands.Context, programs: str, user=None) -> Tuple[bool, discord.Embed]: """Allows a user to remove programs Args: ctx (discord.ext.commands.Context): Context programs (str): The programs to remove user (int, optional): The user ID to remove programs from. Defaults to None. Returns: Tuple[bool, discord.Embed]: [Status: bool, Embed: discord.Embed] """ # Determines the user id if user == ctx.author.id: user_id = user elif user is not None: if ctx.author.guild_permissions.administrator is False: return ( False, create_embed_template( "No Permission", "You do not have permission to remove programs for other users.", "error", ), ) else: user_id = user else: user_id = ctx.author.id db = await Programs_DB(ctx.guild.id) # Deletes ALL the programs if programs.lower() in ["*", "all"]: await db.delete_all_programs(user_id) return ( True, create_embed("Removed **all** program successfully.", "", "light_green"), ) remove_programs = [] for i in programs.split("\n"): [remove_programs.append(g.strip()) for g in i.split(",")] # Removes duplicates remove_programs = list(dict.fromkeys(remove_programs)) try: remove_programs = [int(i) for i in remove_programs] except: return ( False, create_embed_template( "Invalid Values", "Invalid removal values were provided. Provide a number corrosponding to the program(s) you would like to remove.", "error", ), ) # All programs all_programs = {} i = 1 for program in (await db.grab_programs(user_id)).split("\n"): all_programs[i] = program i += 1 # New list w/o removals new_programs = [ val for key, val in all_programs.items() if key not in remove_programs ] if len(new_programs) == 0: await db.delete_all_programs(user_id) return ( True, create_embed( "Programs removed Successfully.", "You now have no programs!", "light_green", ), ) # Message with the current programs list message = "" len_programs = len(new_programs) for program in range(len_programs): message += new_programs[program] + ("\n" if program != len_programs - 1 else "") await db.update_programs(user_id, message) return ( True, create_embed( "Programs Removed Successfully.", f"**Current Programs**:\n {message}" if message else f"All of your programs have been removed.", "light_green", ), )
async def programs_add( ctx: discord.ext.commands.Context, client: discord.ClientUser.bot, programs: list, user: int, ) -> Tuple[bool, discord.Embed]: """Creates a request to add programs for a user Args: ctx (discord.ext.commands.Context): Context client (discord.ClientUser.bot): Discord Bot programs (list): List of programs to add user (int): User's ID to add Returns: Tuple(bool, discord.Embed): [Status: bool, Embed: discord.Embed] """ db_guild = await Guild_DB(ctx.guild.id) # Channel for verification settings = await db_guild.grab_settings() if settings is None or settings["programs_channel"] is None: return ( False, create_embed_template( "No Programs Channel", "The admins haven't created a programs channel. Have an admin run /programs setup.", "error", ), ) programs_channel = settings["programs_channel"] # Creates an embed embed = create_embed("Programs Verification Required", "", "magenta") add_field(embed, "User", client.get_user(user).mention, False) # Message with all of the programs programs_msg = "" len_programs = len(programs) for program in range(len_programs): programs_msg += (programs[program] + "\n" if program != len_programs - 1 else programs[program]) add_field(embed, "Program Additions", programs_msg, True) # Sends the message to the verification channel verify_channel = client.get_channel(programs_channel) verification_msg = await verify_channel.send(embed=embed) # Adds the verification emoji for emoji in ["✅", "❌"]: await verification_msg.add_reaction(emoji) return ( True, create_embed("Programs successfully sent to Moderators.", programs_msg, "light_green"), )
async def programs_edit( ctx: discord.ext.commands.Context, client: discord.ClientUser.bot, user: int, before: str, after: str, ) -> Tuple[bool, discord.Embed]: """Edits one of a user's programs Args: ctx (discord.ext.commands.Context): Context client (discord.ClientUser.bot): Bot instance user (int): User's ID before (str): The program to edit after (str): The new text to replace the program Returns: Tuple[bool, discord.Embed]: [Status: bool, Embed: discord.Embed] """ # Check user if user is None: return ( False, create_embed_template("Invalid Arguments", "User was null.", "error"), ) # Check before and after if before is None or after is None: return ( False, create_embed_template("Invalid Arguments", "*Before* or *after* value is invalid.", "error"), ) try: before = int(before) except: return ( False, create_embed_template( "Invalid Arguments", "*Before* value is invalid (not a number).", "error", ), ) user = parse_id(user) db = await Programs_DB(ctx.guild.id) db_guild = await Guild_DB(ctx.guild.id) # Channel for verification settings = await db_guild.grab_settings() if settings is None or settings["programs_channel"] is None: return ( False, create_embed_template( "Invalid Channel", "The admins haven't created a programs channel. Have an admin run /programs setup.", "error", ), ) programs_channel = settings["programs_channel"] programs = (await db.grab_programs(user)).split("\n") # Entire programs list p = {} i = 1 for program in programs: p[i] = program i += 1 if before not in p.keys(): return ( False, create_embed_template( "Invalid Program", "You do not have a program with that *before* value.", "error", ), ) # Create a verification Embed embed = create_embed("Programs (Edit) Verification Required", "", "magenta") add_field(embed, "User", f"{(await client.fetch_user(user)).mention}", True) add_field(embed, "Before", p[before], True) add_field(embed, "After", after, True) verify_channel = client.get_channel(int(programs_channel)) verify_msg = await verify_channel.send(embed=embed) # Add emojis for emoji in ["✅", "❌"]: await verify_msg.add_reaction(emoji) embed = create_embed("Program Edit successfully sent to the moderators.", "", "light_green") add_field(embed, "Before", p[before], True) add_field(embed, "After", after, True) return True, embed