From 3316c4058abc347169a090be21d813b0a9e24666 Mon Sep 17 00:00:00 2001 From: Ryth-cs <49680490+Ryth-cs@users.noreply.github.com> Date: Sun, 29 Aug 2021 20:20:11 +0100 Subject: [PATCH] Moved changes from old develop format to the new one (#151) * Moved changes from old develop format to the new one * Resolved conflicts --- README.md | 139 ++++++++++++-------------- src/esportsbot/cogs/DefaultRoleCog.py | 134 ++++++++++++++----------- src/esportsbot/models.py | 8 +- src/esportsbot/user_strings.toml | 15 +-- 4 files changed, 157 insertions(+), 139 deletions(-) diff --git a/README.md b/README.md index 40169d95..73036cfe 100644 --- a/README.md +++ b/README.md @@ -67,81 +67,70 @@ $ source secrets.env ```bash $ cd src ``` - -5. Install all the requirements for python: -```bash -pip install -r requirements.txt -``` - -6. Run the bot: -```bash -python3 main.py -``` - -## Current Functions - -The list below describes the different "Cogs" of the bot, their associated commands, and any additional information required to set them up. - -
-Voicemaster - -### Voicemaster - - #### !setvmmaster - -* Make the given ID a Voicemaster master. - -#### !getvmmasters * Get all the Voicemaster masters in the server. - -#### !removevmmaster - -* Remove the given ID as a Voicemaster master. - -#### !removeallmasters * Remove all Voicemaster masters from the server. - -#### !killallslaves * Kill all the Voicemaster slave channels in the server. - -#### !lockvm * Locks the Voicemaster slave you're currently in to the number of current members. - -#### !unlockvm * Unlocks the Voicemaster slave you're currently in. -
- -
-Default Role - -### Default role - - #### !setdefaultrole * Set the default role to the @'ed role or given role ID. - -#### !getdefaultrole * Gets the current default role value. - -#### !removedefaultrole * Removes the current default role. -
- -
-Log Channel - -### Log Channel - - #### !setlogchannel * Set the log channel to the #'ed channel or given role ID. - -#### !getlogchannel * Gets the current log channel value. - -#### !removelogchannel * Removes the current log channel value. - -
- -
-Administrator Tools - -### Administrator Tools - - Adds a few commands useful for admin operations. - -#### !clear_message * Aliases: `cls, purge, delete` -* Clear the specified number of messages from the current text channel. - -#### !members +5. Install all the requirements for python: +```bash +pip install -r requirements.txt +``` +6. Run the bot: +```bash +python3 main.py +``` + +## Current Functions +The list below describes the different "Cogs" of the bot, their associated commands, and any additional information required to set them up. + +
+Voicemaster + +### Voicemaster + #### !setvmmaster +* Make the given ID a Voicemaster master. + +#### !getvmmasters * Get all the Voicemaster masters in the server. + +#### !removevmmaster +* Remove the given ID as a Voicemaster master. + +#### !removeallmasters * Remove all Voicemaster masters from the server. + +#### !killallslaves * Kill all the Voicemaster slave channels in the server. + +#### !lockvm * Locks the Voicemaster slave you're currently in to the number of current members. + +#### !unlockvm * Unlocks the Voicemaster slave you're currently in. +
+ +
+Default Role + +### Default role + #### !setdefaultroles * Sets the roles that the server gives to members when they join the server. + +#### !getdefaultroles * Gets the current default roles set for the server. + +#### !removedefaultroles * Removes the current default roles for the server. +
+ +
+Log Channel + +### Log Channel + #### !setlogchannel * Set the log channel to the #'ed channel or given role ID. + +#### !getlogchannel * Gets the current log channel value. + +#### !removelogchannel * Removes the current log channel value. +
+ +
+Administrator Tools + +### Administrator Tools + Adds a few commands useful for admin operations. +#### !clear_message * Aliases: `cls, purge, delete` +* Clear the specified number of messages from the current text channel. + +#### !members * List the current number of members in the server. #### !remove-cog \ diff --git a/src/esportsbot/cogs/DefaultRoleCog.py b/src/esportsbot/cogs/DefaultRoleCog.py index 6688f795..58cba6f8 100644 --- a/src/esportsbot/cogs/DefaultRoleCog.py +++ b/src/esportsbot/cogs/DefaultRoleCog.py @@ -1,7 +1,8 @@ from discord.ext import commands from esportsbot.base_functions import role_id_from_mention from esportsbot.db_gateway import DBGatewayActions -from esportsbot.models import Guild_info +from esportsbot.models import Guild_info, Default_roles +from esportsbot.base_functions import role_id_from_mention, send_to_log_channel class DefaultRoleCog(commands.Cog): @@ -14,15 +15,24 @@ async def on_member_join(self, member): guild = DBGatewayActions().get(Guild_info, guild_id=member.guild.id) if not guild: return - - if guild.default_role_id: - default_role = member.guild.get_role(guild.default_role_id) - await member.add_roles(default_role) + # Get all the default role for the server from database + guild_default_roles = DBGatewayActions().list(Default_roles, guild_id=member.guild.id) + # Check to see if any roles exist + if guild_default_roles: + # Create list of roles from database response + apply_roles = [member.guild.get_role(role.role_id) for role in guild_default_roles] + # Add all the roles to the user, we don't check if they're valid as we do this on input + await member.add_roles(*apply_roles) await self.bot.adminLog( None, { - "Cog": str(type(self)), - "Message": f"{member.mention} has joined the server and received the {default_role.mention} role" + "Cog": + str(type(self)), + "Message": + self.STRINGS['default_role_join'].format( + member_name=member.mention, + role_ids=(' '.join(f'<@&{x.id}>' for x in apply_roles)) + ) }, guildID=member.guild.id ) @@ -31,76 +41,86 @@ async def on_member_join(self, member): None, { "Cog": str(type(self)), - "Message": f"{member.mention} has joined the server" + "Message": self.STRINGS['default_role_join_no_role'].format(member_name=member.mention) }, guildID=member.guild.id ) @commands.command( - name="setdefaultrole", - usage=" or <@role>", - help="Sets the role that the server gives to members when they join the server" + name="setdefaultroles", + usage="<@role> <@role> <@role> ...", + help="Sets the roles that the server gives to members when they join the server" ) @commands.has_permissions(administrator=True) - async def setdefaultrole(self, ctx, given_role_id): - cleaned_role_id = role_id_from_mention(given_role_id) if given_role_id else False - if cleaned_role_id: - guild = DBGatewayActions().get(Guild_info, guild_id=ctx.author.guild.id) - - if not guild: - db_item = Guild_info(guild_id=ctx.guild.id, default_role_id=cleaned_role_id) - DBGatewayActions().create(db_item) - else: - guild.default_role_id = cleaned_role_id - DBGatewayActions().update(guild) - - await ctx.channel.send(self.STRINGS['default_role_set'].format(role_id=cleaned_role_id)) - default_role = ctx.author.guild.get_role(cleaned_role_id) - await self.bot.adminLog( - ctx.message, - { - "Cog": - str(type(self)), - "Message": - self.STRINGS['default_role_set_log'].format(author=ctx.author.mention, - role_mention=default_role.mention) - } - ) + async def setdefaultroles(self, ctx, *, args: str): + role_list = args.split(" ") + if len(role_list) == 0: + await ctx.channel.send(self.STRINGS['default_roles_set_empty']) else: - await ctx.channel.send(self.STRINGS['default_role_set_missing_params']) + checked_roles = [] + checking_error = False + # Loop through the roles to check the input formatting is correct and that roles exist + for role in role_list: + try: + # Clean the inputted role to just the id + cleaned_role_id = role_id_from_mention(role) + # Obtain role object from the guild to check it exists + role_obj = ctx.author.guild.get_role(cleaned_role_id) + # Add role to array to add post checks + checked_roles.append(cleaned_role_id) + except Exception as err: + print(err) + checking_error = True + if not checking_error: + for role in checked_roles: + DBGatewayActions().create(Default_roles(guild_id=ctx.author.guild.id, role_id=role)) + await ctx.channel.send(self.STRINGS['default_roles_set'].format(roles=args)) + await self.bot.adminLog( + ctx.message, + { + "Cog": str(type(self)), + "Message": self.STRINGS['default_roles_set_log'].format(author_mention=ctx.author.mention, + roles=args) + } + ) + else: + await ctx.channel.send(self.STRINGS['default_roles_set_error']) @commands.command( - name="getdefaultrole", + name="getdefaultroles", usage="", - help="Gets the role that the server gives to members when they join the server" + help="Gets the roles that the server gives to members when they join the server" ) @commands.has_permissions(administrator=True) - async def getdefaultrole(self, ctx): - guild = DBGatewayActions().get(Guild_info, guild_id=ctx.author.guild.id) - if not guild: - await ctx.channel.send(self.STRINGS['default_role_missing']) - return - - if guild.default_role_id: - await ctx.channel.send(self.STRINGS['default_role_get'].format(role_id=guild.default_role_id)) + async def getdefaultroles(self, ctx): + # Get all the default role for the server from database + guild_default_roles = DBGatewayActions().list(Default_roles, guild_id=ctx.author.guild.id) + # Check to see if any roles exist + if guild_default_roles: + # Create list of roles from database response + apply_roles = [ctx.author.guild.get_role(role.role_id) for role in guild_default_roles] + # Return all the default roles to the user + await ctx.channel.send( + self.STRINGS['default_role_get'].format(role_ids=(' '.join(f'<@&{x.id}>' for x in apply_roles))) + ) else: await ctx.channel.send(self.STRINGS['default_role_missing']) @commands.command( - name="removedefaultrole", + name="removedefaultroles", usage="", - help="Removes the role that the server gives to members when they join the server" + help="Removes the roles that the server gives to members when they join the server" ) @commands.has_permissions(administrator=True) - async def removedefaultrole(self, ctx): - guild = DBGatewayActions().get(Guild_info, guild_id=ctx.author.guild.id) - if not guild: - await ctx.channel.send(self.STRINGS['default_role_missing']) - return - - if guild.default_role_id: - guild.default_role_id = None - DBGatewayActions().update(guild) + async def removedefaultroles(self, ctx): + # Get all the default role for the server from database + guild_default_roles = DBGatewayActions().list(Default_roles, guild_id=ctx.author.guild.id) + # Check to see if any roles exist + if guild_default_roles: + for default_role in guild_default_roles: + # Remove the current role + DBGatewayActions().delete(default_role) + # Return a response to the user await ctx.channel.send(self.STRINGS['default_role_removed']) await self.bot.adminLog( ctx.message, diff --git a/src/esportsbot/models.py b/src/esportsbot/models.py index 5ea8b12b..e54a58ed 100644 --- a/src/esportsbot/models.py +++ b/src/esportsbot/models.py @@ -9,7 +9,13 @@ class Guild_info(base): __tablename__ = 'guild_info' guild_id = Column(BigInteger, primary_key=True, nullable=False) log_channel_id = Column(BigInteger, nullable=True) - default_role_id = Column(BigInteger, nullable=True) + + +class Default_roles(base): + __tablename__ = 'default_roles' + default_roles_id = Column(BigInteger, primary_key=True, autoincrement=True, nullable=False) + guild_id = Column(BigInteger, nullable=False) + role_id = Column(BigInteger, nullable=False) class Pingable_polls(base): diff --git a/src/esportsbot/user_strings.toml b/src/esportsbot/user_strings.toml index 9c4cb59a..67e8271f 100644 --- a/src/esportsbot/user_strings.toml +++ b/src/esportsbot/user_strings.toml @@ -18,15 +18,18 @@ members = "This server has {member_count} members including me" no_version = "No version recorded" [default_role] -default_role_missing = "Default role has not been set" +default_role_join = "{member_name} has joined the server and received: {role_ids}" +default_role_join_no_role = "{member_name} has joined the server" +default_role_missing = "Default role(s) have not been set" -default_role_set = "Default role has been set to {role_id}" -default_role_set_log = "{author} has set the default role to {role_mention}" -default_role_set_missing_param = "You need to either @ a role or paste the ID" +default_roles_set = "Default role(s) are now set to {roles}" +default_roles_set_empty = "No roles were passed, please review your usage" +default_roles_set_error = "Error occurred during this operation, please check that you have formatted these inputs correctly" +default_role_set_log = "{author_mention} has set the default role(s) to: {roles}" -default_role_get = "Default role is set to {role_id}" +default_role_get = "Default role(s) are set to {role_ids}" -default_role_removed = "Default role has been removed" +default_role_removed = "Default role(s) are removed" default_role_removed_log = "{author_mention} has removed the default role" [music]