コード例 #1
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_integer(command[0]):
                    guild_id = str(message.guild.id)
                    user_id = int(command[0])
                    muted_role_id = int(self.storage.settings["guilds"]
                                        [guild_id]["muted_role_id"])
                    try:
                        user = await message.guild.fetch_member(user_id)
                    except discord.errors.NotFound or discord.errors.HTTPException:
                        user = None
                    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_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[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.**")
コード例 #2
0
    async def execute(self, message, **kwargs):
        command = kwargs.get("args")
        if await author_is_mod(message.author, self.storage):
            if len(command) >= 3:
                if is_integer(command[0]):
                    user_id = int(command[0])
                    duration = parse_duration(command[1])
                    if is_valid_duration(duration):
                        guild_id = str(message.guild.id)
                        ban_duration = int(time.time()) + duration
                        try:
                            user = await message.guild.fetch_member(user_id)
                        except discord.errors.NotFound or discord.errors.HTTPException:
                            user = None
                        # 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)
                        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 message.guild.ban(user, reason=reason)
                            self.storage.settings["guilds"][guild_id][
                                "banned_users"][str(user_id)] = {}
                            self.storage.settings["guilds"][guild_id][
                                "banned_users"][str(
                                    user_id)]["duration"] = ban_duration
                            self.storage.settings["guilds"][guild_id][
                                "banned_users"][str(
                                    user_id)]["reason"] = reason
                            self.storage.settings["guilds"][guild_id][
                                "banned_users"][str(
                                    user_id)]["normal_duration"] = command[1]
                            await self.storage.write_file_to_disk()
                            # Message the channel
                            await message.channel.send(
                                f"**Temporarily banned user:** `{user.name}` **for:** `{command[1]}`**. Reason:** `{reason}`"
                            )

                            # Build the embed and message it to the log channel
                            embed_builder = EmbedBuilder(event="tempban")
                            await embed_builder.add_field(
                                name="**Executor**",
                                value=f"`{message.author.name}`")
                            await embed_builder.add_field(
                                name="**Temp Banned 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_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.**")
コード例 #3
0
    async def execute(self, message, **kwargs):
        command = kwargs.get("args")
        if author_is_admin(message.author):
            if len(command) == 2:
                guild_id = str(message.guild.id)
                if command[0] == "add":
                    if is_integer(command[1]):
                        role_id = int(command[1])
                        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_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_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[0] == "remove":
                    if is_integer(command[1]):
                        role_id = int(command[1])
                        # 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_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_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[0] == "list":
                    await self.list_mods(message)
                else:
                    await message.channel.send(
                        self.invalid_option.format(option=command[0]))

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