Ejemplo n.º 1
0
    async def report_error(self, error, *, fields):
        exception = "".join(
            traceback.format_exception(type(error), error,
                                       error.__traceback__))

        print(exception, file=sys.stderr)

        embed = discord.Embed(title="Unhandled error",
                              description=wrap_in_code(exception, block="py"))
        for field in fields:
            embed.add_field(**field)

        info = await self.bot.application_info()
        await info.owner.send(embed=embed)
Ejemplo n.º 2
0
    async def on_error(self, event, *args, **kwargs):
        error = sys.exc_info()[1]

        await self.report_error(
            error,
            fields=[
                {
                    "name": "Event",
                    "value": f"```{event}```",
                    "inline": False,
                },
                *({
                    "name": f"args[{index!r}]",
                    "value": wrap_in_code(repr(arg), block=True),
                    "inline": False,
                } for index, arg in enumerate(args)),
                *({
                    "name": f"kwargs[{index!r}]",
                    "value": wrap_in_code(repr(arg), block=True),
                    "inline": False,
                } for index, arg in kwargs.items()),
            ],
        )
Ejemplo n.º 3
0
    async def config(
        self,
        ctx: cmd.Context,
        option: typing.Optional[str],
        *,
        new_value: typing.Optional[str],
    ):
        """Manages server configuration for bot"""

        command = f"{get_clean_prefix(ctx)}{self.config.qualified_name}"

        if option:
            configurable = get(config.configurables, name=option.lower())
            if configurable is None:
                raise commands.UserInputError(
                    f"Option {wrap_in_code(option)} not found"
                )

            if new_value:
                await commands.has_guild_permissions(manage_guild=True).predicate(ctx)

                try:
                    parsed_value = config.resolve_value(configurable.type, new_value)
                    await self.cfg.set_value(ctx.guild, configurable, parsed_value)
                except:
                    raise commands.BadArgument(
                        f"Value {wrap_in_code(new_value)} is does not fit"
                        f" expected type {config.type_names[configurable.type]}"
                    )

            value = (
                parsed_value
                if new_value is not None
                else await self.cfg.get_value(ctx.guild, configurable)
            )
            value = (
                ("yes" if value else "no") if isinstance(value, bool) else str(value)
            )
            value = wrap_in_code(value)

            set_signature = wrap_in_code(f"{command} {configurable.name} <new value>")
            message = (
                f"Option {configurable.name} has been set to {value}."
                if new_value is not None
                else f"Option {configurable.name} is currently set to {value}."
                f"\nUse {set_signature} to set it."
            )

            await ctx.prompt(
                embed=discord.Embed(title="Configuration", description=message)
            )
            return

        get_signature = wrap_in_code(f"{command} <option>")
        set_signature = wrap_in_code(f"{command} <option> <new value>")

        embed = discord.Embed(
            title="Configuration",
            description="Command to manage the bot's configuration for a server."
            f"\nTo get the value of an option use {get_signature}."
            f"\nTo set the value of an option use {set_signature}."
            "\nList of options can be found below:",
        )
        embed.set_footer(
            text="Page {current_page}/{total_pages}, "
            "showing option {first_field}..{last_field}/{total_fields}"
        )
        paginator = menus.FieldPaginator(self.bot, base_embed=embed)

        for configurable in config.configurables:
            paginator.add_field(
                name=configurable.name.capitalize(),
                value=configurable.description,
            )

        await paginator.send(ctx)