Beispiel #1
0
    async def parse_time_arguments(options, default_days_number=30):
        # Check arguments
        time_option = utils.get_option_value(options, 'time')
        if time_option is not None:  # Value assigned
            try:
                days_number = int(time_option)
            except ValueError:
                raise exceptions.MisformattedArgument(time_option,
                                                      "valeur entière")
            if days_number < 1:
                raise exceptions.UndersizedArgument(days_number, 1)
            elif days_number > 1000:
                raise exceptions.OversizedArgument(days_number, 1000)
        elif utils.is_option_enabled(options, 'time',
                                     has_value=True):  # No value assigned
            raise exceptions.MisformattedArgument(time_option,
                                                  "valeur entière")
        else:  # Option not used
            days_number = default_days_number

        # Determine granularity
        granularity = None
        for granularity_option in ('hour', 'day', 'month', 'year'):
            if utils.is_option_enabled(options, granularity_option):
                granularity = granularity_option
                break
        if not granularity:
            granularity = 'hour' if days_number < Server.HOURS_GRANULARITY_LIMIT \
                else 'day' if days_number < Server.DAYS_GRANULARITY_LIMIT \
                else 'month' if days_number < Server.MONTHS_GRANULARITY_LIMIT \
                else 'year'
        return days_number, granularity
Beispiel #2
0
    async def switch(self,
                     context,
                     dest_channel: discord.TextChannel,
                     *,
                     options=""):
        if context.channel == dest_channel \
                or not context.author.permissions_in(dest_channel).send_messages:
            raise exceptions.ForbiddenChannel(dest_channel)
        number_option = utils.get_option_value(options, 'number')
        if number_option is not None:  # Value assigned
            try:
                messages_number = int(number_option)
            except ValueError:
                raise exceptions.MisformattedArgument(number_option,
                                                      "valeur entière")
            if messages_number < 0:
                raise exceptions.UndersizedArgument(messages_number, 0)
            elif messages_number > 10 and not checker.has_any_mod_role(
                    context, print_error=False):
                raise exceptions.OversizedArgument(messages_number, 10)
        elif utils.is_option_enabled(options, 'number',
                                     has_value=True):  # No value assigned
            raise exceptions.MisformattedArgument(number_option,
                                                  "valeur entière")
        else:  # Option not used
            messages_number = 3
        do_ping = utils.is_option_enabled(options, 'ping')
        do_delete = utils.is_option_enabled(options, 'delete')
        if do_ping or do_delete:
            checker.has_any_mod_role(context, print_error=True)

        messages = await context.channel.history(limit=messages_number + 1
                                                 ).flatten()
        messages.reverse()  # Order by oldest first
        messages.pop()  # Remove message used for the command

        await context.message.delete()
        if not do_delete:
            await context.send(
                f"**Veuillez basculer cette discussion dans le canal {dest_channel.mention} qui "
                f"serait plus approprié !** 🧹")
        else:
            await context.send(
                f"**La discussion a été déplacée dans le canal {dest_channel.mention} !** "
                f"({len(messages)} messages supprimés) 🧹")
        if messages_number != 0:
            await dest_channel.send(
                f"**Suite de la discussion de {context.channel.mention}** 💨"
            )
        await self.move_messages(messages, dest_channel, do_ping, do_delete)
Beispiel #3
0
 async def help(self, context, *, args: str = ""):
     max_nest_level = utils.get_option_value(args, 'nest')
     if max_nest_level:
         try:
             max_nest_level = int(max_nest_level)
         except ValueError:
             raise exceptions.MisformattedArgument(max_nest_level,
                                                   "valeur entière")
     else:
         max_nest_level = Bot.MAX_COMMAND_NEST_LEVEL
     full_command_name = utils.remove_option(args, 'nest')
     if not full_command_name:  # No command specified
         if max_nest_level < 1:
             raise exceptions.UndersizedArgument(max_nest_level, 1)
         await self.display_generic_help(context, max_nest_level)
     else:  # Request help commands matching the given pattern
         command_name = full_command_name.split(' ')[-1]
         command_chain = full_command_name.split(' ')[:-1]
         matching_commands = utils.get_commands(context, command_chain,
                                                command_name)
         if not matching_commands:
             raise exceptions.UnknownCommand(command_name)
         else:
             sorted_matching_commands = sorted(matching_commands,
                                               key=lambda c: c.name)
             for command in sorted_matching_commands:
                 if isinstance(command, commands.Group):
                     await self.display_group_help(context, command,
                                                   max_nest_level)
                 else:
                     await self.display_command_help(context, command)
Beispiel #4
0
Datei: bot.py Projekt: Zedd7/ZBot
    async def help(self, context, *, args: str = ""):
        max_nest_level = utils.get_option_value(args, 'nest')
        if max_nest_level:
            try:
                max_nest_level = int(max_nest_level)
            except ValueError:
                raise exceptions.MisformattedArgument(max_nest_level,
                                                      "valeur entière")
        else:
            max_nest_level = self.DEFAULT_HELP_NEST_LEVEL
        full_command_name = utils.remove_option(args, 'nest')

        if not full_command_name:  # No command specified
            if max_nest_level < 1:
                raise exceptions.UndersizedArgument(max_nest_level, 1)
            await self.display_generic_help(context, max_nest_level)
        else:  # Request help for commands matching the given pattern
            command_name = full_command_name.split(' ')[-1]
            command_chain = full_command_name.split(' ')[:-1]
            matching_commands = utils.get_commands(context, command_chain,
                                                   command_name)
            if not matching_commands:
                raise exceptions.UnknownCommand(command_name)
            else:
                # Don't show the helper of all matching commands if one matches exactly
                if exactly_matching_commands := set(
                        filter(lambda c: c.qualified_name == full_command_name,
                               matching_commands)):
                    matching_commands = exactly_matching_commands

                # Don't show an error for missing permissions if there is at least one public command
                public_commands = list(
                    filter(lambda c: not c.hidden, matching_commands))
                if len(public_commands) < len(
                        matching_commands):  # At least one command is hidden
                    try:  # Don't print the error right away
                        checker.has_any_mod_role(context)
                    except exceptions.MissingRoles as error:
                        if not public_commands:  # All commands requires permissions
                            raise error  # Print the error
                        else:  # At least one command is public
                            matching_commands = public_commands  # Filter out hidden commands

                # Show the helper of matching commands
                sorted_matching_commands = sorted(
                    matching_commands, key=lambda c: c.qualified_name)
                for command in sorted_matching_commands:
                    if isinstance(command, commands.Group):
                        await self.display_group_help(context, command,
                                                      max_nest_level)
                    else:
                        await self.display_command_help(context, command)
Beispiel #5
0
Datei: bot.py Projekt: Zedd7/ZBot
    async def changelog(self, context, version: str = None):
        current_version = zbot.__version__
        if not version:
            version = current_version
        else:
            result = re.match(r'(\d+)\.(\d+)\.(\d+)', version)
            if not result:
                raise exceptions.MisformattedArgument(
                    version, 'a.b.c (a, b et c valeurs entières)')
            major, minor, patch = (int(value) for value in result.groups()
                                   )  # Cast as int to remove leading zeros
            if self.is_out_of_version_range(major, minor, patch):
                raise exceptions.OversizedArgument(version, current_version)
            version = '.'.join(str(value) for value in (major, minor, patch))

        changelog = self.get_changelog(version)
        bot_display_name = self.get_bot_display_name(self.user, self.guild)
        if not changelog:
            await context.send(
                f"Aucune note de version trouvée pour @{bot_display_name} v{version}"
            )
        else:  # This is only executed if there is a match and if both the date and changes are present
            date_iso, description, raw_changes = changelog
            changes = [
                raw_change.removeprefix('- ')
                for raw_change in raw_changes.split('\n') if raw_change
            ]
            embed = discord.Embed(
                title=f"Notes de version de @{bot_display_name} v{version}",
                description="",
                color=self.EMBED_COLOR)
            embed.add_field(name="Date",
                            value=converter.to_human_format(
                                converter.to_datetime(date_iso)),
                            inline=False)
            description and embed.add_field(
                name="Description", value=description, inline=False)
            embed.add_field(name="Changements",
                            value='\n'.join(
                                [f"• {change}" for change in changes]),
                            inline=False)
            await context.send(embed=embed)