async def handle(self, message, command):
     if await author_is_mod(message.author, self.storage):
         if len(command) == 2:
             if is_number(command[1]):
                 guild_id = str(message.guild.id)
                 user_id = int(command[1])
                 muted_role_id = int(self.storage.settings["guilds"][guild_id]["muted_role_id"])
                 user = message.guild.get_member(user_id)
                 muted_role = message.guild.get_role(muted_role_id)
                 if user is not None:
                     # Remove the muted role from the user and remove them from the guilds muted users list
                     await user.remove_roles(muted_role, reason=f"Unmuted by {message.author.name}")
                     self.storage.settings["guilds"][guild_id]["muted_users"].pop(str(user_id))
                     await self.storage.write_settings_file_to_disk()
                     # Message the channel
                     await message.channel.send(f"**Unmuted user:** `{user.name}`**.**")
                     
                     # Build the embed and message it to the log channel
                     embed_builder = EmbedBuilder(event="unmute")
                     await embed_builder.add_field(name="**Executor**", value=f"`{message.author.name}`")
                     await embed_builder.add_field(name="**Unmuted user**", value=f"`{user.name}`")
                     embed = await embed_builder.get_embed()
                     log_channel_id = int(self.storage.settings["guilds"][guild_id]["log_channel_id"])
                     log_channel = message.guild.get_channel(log_channel_id)
                     if log_channel is not None:
                         await log_channel.send(embed=embed)
                 else:
                     await message.channel.send(self.invalid_user.format(user_id=user_id, usage=self.usage))
             else:
                 await message.channel.send(self.not_a_user_id.format(user_id=command[1], usage=self.usage))
         else:
             await message.channel.send(self.not_enough_arguments.format(usage=self.usage))
     else:
         await message.channel.send("**You must be a moderator to use this command.**")
 async def execute(self, message, **kwargs):
     command = kwargs.get("args")
     if await author_is_mod(message.author, self.storage):
         if len(command) >= 2:
             if is_number(command[0]):
                 user_id = int(command[0])
                 duration = parse_duration(command[1])
                 if is_valid_duration(duration):
                     guild_id = str(message.guild.id)
                     mute_duration = int(time.time()) + duration
                     muted_role_id = int(self.storage.settings["guilds"][guild_id]["muted_role_id"])
                     user = await message.guild.fetch_member(user_id)
                     muted_role = message.guild.get_role(muted_role_id)
                     if len(command) >= 3:
                         # Collects everything after the first two items in the command and uses it as a reason.
                         temp = [item for item in command if command.index(item) > 1]
                         reason = " ".join(temp)
                     else:
                         reason = f"Temp muted by {message.author.name}"
                     if user is not None:
                         # Add the muted role and store them in guilds muted users list. We use -1 as the duration to state that it lasts forever.
                         await user.add_roles(muted_role, reason=f"Muted by {message.author.name}")
                         self.storage.settings["guilds"][guild_id]["muted_users"][str(user_id)] = {}
                         self.storage.settings["guilds"][guild_id]["muted_users"][str(user_id)]["duration"] = mute_duration
                         self.storage.settings["guilds"][guild_id]["muted_users"][str(user_id)]["reason"] = reason
                         self.storage.settings["guilds"][guild_id]["muted_users"][str(user_id)]["normal_duration"] = command[1]
                         await self.storage.write_settings_file_to_disk()
                         # Message the channel
                         await message.channel.send(f"**Temporarily muted user:** `{user.name}` **for:** `{command[1]}`**. Reason:** `{reason}`")
                         
                         # Build the embed and message it to the log channel
                         embed_builder = EmbedBuilder(event="tempmute")
                         await embed_builder.add_field(name="**Executor**", value=f"`{message.author.name}`")
                         await embed_builder.add_field(name="**Muted user**", value=f"`{user.name}`")
                         await embed_builder.add_field(name="**Reason**", value=f"`{reason}`")
                         await embed_builder.add_field(name="**Duration**", value=f"`{command[1]}`")
                         embed = await embed_builder.get_embed()
                         log_channel_id = int(self.storage.settings["guilds"][guild_id]["log_channel_id"])
                         log_channel = message.guild.get_channel(log_channel_id)
                         if log_channel is not None:
                             await log_channel.send(embed=embed)
                     else:
                         await message.channel.send(self.invalid_user.format(user_id=user_id, usage=self.usage))
                 else:
                     await message.channel.send(self.invalid_duration.format(user_id=user_id, usage=self.usage))
             else:
                 await message.channel.send(self.not_a_user_id.format(user_id=command[0], usage=self.usage))
         else:
             await message.channel.send(self.not_enough_arguments.format(usage=self.usage))
     else:
         await message.channel.send("**You must be a moderator to use this command.**")
Exemple #3
0
    async def execute(self, message, **kwargs):
        command = kwargs.get("args")
        if await author_is_mod(message.author, self.storage):
            if len(command) == 1:
                if is_number(command[0]):
                    user_id = int(command[0])
                    guild_id = str(message.guild.id)
                    user = await message.guild.fetch_member(user_id)
                    if user is not None:
                        # Unban the user and remove them from the guilds banned users list
                        await message.guild.unban(
                            user, reason=f"Unbanned by {message.author.name}")
                        self.storage.settings["guilds"][guild_id][
                            "banned_users"].pop(str(user_id))
                        await self.storage.write_settings_file_to_disk()
                        # Message the channel
                        await message.channel.send(
                            f"**Unbanned user:** `{user.name}`**.**")

                        # Build the embed and message it to the log channel
                        embed_builder = EmbedBuilder(event="unban")
                        await embed_builder.add_field(
                            name="**Executor**",
                            value=f"`{message.author.name}`")
                        await embed_builder.add_field(name="**Unbanned user**",
                                                      value=f"`{user.name}`")
                        embed = await embed_builder.get_embed()
                        log_channel_id = int(self.storage.settings["guilds"]
                                             [guild_id]["log_channel_id"])
                        log_channel = message.guild.get_channel(log_channel_id)
                        if log_channel is not None:
                            await log_channel.send(embed=embed)
                    else:
                        await message.channel.send(
                            self.invalid_user.format(user_id=user_id,
                                                     usage=self.usage))
                else:
                    await message.channel.send(
                        self.not_a_user_id.format(user_id=command[0],
                                                  usage=self.usage))
            else:
                await message.channel.send(
                    self.not_enough_arguments.format(usage=self.usage))
        else:
            await message.channel.send(
                "**You must be a moderator to use this command.**")
    async def handle(self, message, command):
        if author_is_admin(message.author):
            if len(command) == 3:
                guild_id = str(message.guild.id)
                if command[1] == "add":
                    if is_number(command[2]):
                        role_id = int(command[2])
                        mod_role = message.guild.get_role(role_id)
                        # If the mod role exists
                        if mod_role is not None:
                            # Load the list of mod roles from disk
                            mod_roles = self.storage.settings["guilds"][
                                guild_id].get("mod_roles")
                            # Create the section of mod roles if one doesn't exist
                            if mod_roles is None:
                                self.storage.settings["guilds"][guild_id][
                                    "mod_roles"] = []
                                await self.storage.write_settings_file_to_disk(
                                )
                                mod_roles = self.storage.settings["guilds"][
                                    guild_id].get("mod_roles")
                            if role_id not in mod_roles:
                                # Add the mod role to the settings storage
                                mod_roles.append(role_id)
                                await self.storage.write_settings_file_to_disk(
                                )
                                await message.channel.send(
                                    f"**Added** `{mod_role.name}` **as a moderator role.** "
                                )
                            else:
                                # That role is already a mod
                                await message.channel.send(
                                    self.role_already_mod)
                        else:
                            await message.channel.send(self.not_a_valid_role)
                    else:
                        await message.channel.send(self.not_a_valid_role)
                elif command[1] == "remove":
                    if is_number(command[2]):
                        role_id = int(command[2])
                        # Get the mod role from the guild and assign the name as the role ID if it doesn't exist.
                        mod_role = message.guild.get_role(role_id)
                        if mod_role is None:
                            role_name = str(role_id)
                        else:
                            role_name = mod_role.name
                        # Get the mod roles from disk
                        mod_roles = self.storage.settings["guilds"][
                            guild_id].get("mod_roles")
                        # Create the section if it doesn't exist
                        if mod_roles is None:
                            self.storage.settings["guilds"][guild_id][
                                "mod_roles"] = []
                            await self.storage.write_settings_file_to_disk()
                            mod_roles = self.storage.settings["guilds"][
                                guild_id].get("mod_roles")
                        # If the role is in the mods list, remove it.
                        if role_id in mod_roles:
                            mod_roles.remove(role_id)
                            await self.storage.write_settings_file_to_disk()
                            await message.channel.send(
                                f"**Removed** `{role_name}` **from the moderator roles.**"
                            )
                        else:
                            # The role was not listed as a mod.
                            await message.channel.send(
                                f"**The role ID** `{str(role_id)}` **is not a moderator role.**"
                            )
                    else:
                        await message.channel.send(self.not_a_valid_role)
                elif command[1] == "list":
                    await self.list_mods(message)
                else:
                    await message.channel.send(
                        self.invalid_option.format(option=command[1]))

            elif len(command) == 2:
                if command[1] == "list":
                    await self.list_mods(message)
                else:
                    await message.channel.send(
                        self.invalid_option.format(option=command[1]))
            else:
                await message.channel.send(self.not_enough_arguments)
        else:
            await message.channel.send(
                "**You must be an Administrator to use this command.**")