Esempio n. 1
0
def get_embed(
    style: Style,
    desc: str = None,
    title: str = None,
    color: Union[int, Color] = None,
    author: User = None,
    guild: Guild = None,
    translator: Translator = Translator()) -> Embed:
    embed: Embed = style.value.copy()

    if title:
        embed.title = title
    else:
        embed.title = translator.translate(embed.title)

    if desc:
        embed.description = desc

    if color:
        embed.colour = color

    if author:
        embed.set_footer(text=translator.format(
            "mrvn_api_embed_requestedby",
            f"{author.name}#{author.discriminator}"),
                         icon_url=author.avatar.url)

    if guild and style == Style.INFO and guild.me.top_role.position != 0:
        embed.colour = guild.me.top_role.colour

    return embed
Esempio n. 2
0
    def get_translatable_desc(self, command: Union[SlashCommand,
                                                   SlashCommandGroup],
                              tr: Translator):
        desc = getattr(command, "__mrvn_translatable_desc__", None)

        if not desc:
            return tr.translate("mrvn_api_command_no_desc")

        return tr.translate(desc)
Esempio n. 3
0
    def parse(cls, ctx: MrvnMessageContext, args: PreparedArguments,
              translator: Translator = Translator()) -> any:

        try:
            value = cls.parse_value(ctx, args, translator)
        except IndexError:
            raise ArgumentParseException.with_pointer(translator.translate("mrvn_api_command_parse_out_of_range"), args)

        return value
Esempio n. 4
0
 def parse_value(
     cls,
     ctx: MrvnMessageContext,
     args: PreparedArguments,
     translator: Translator = Translator()) -> any:
     try:
         return int(args.next().value)
     except ValueError:
         raise ArgumentParseException.with_pointer(
             translator.translate("mrvn_api_command_parse_integer_error"),
             args)
Esempio n. 5
0
    def parse_value(
        cls,
        ctx: MrvnMessageContext,
        args: PreparedArguments,
        translator: Translator = Translator()) -> any:
        value = args.next().value.lower()

        if value in ["yes", "yeah", "true", "da", "+"]:
            return True
        elif value in ["no", "false", "nah", "net", "-"]:
            return False
        else:
            raise ArgumentParseException.with_pointer(
                translator.translate("mrvn_api_command_parse_bool_error"),
                args)
Esempio n. 6
0
    def parse_value(
        cls,
        ctx: MrvnMessageContext,
        args: PreparedArguments,
        translator: Translator = Translator()) -> any:
        value = args.next().value.lower()

        try:
            search = MENTION_PATTERN.search(value)

            if not search:
                raise ArgumentParseException.with_pointer(
                    translator.translate(
                        "mrvn_api_command_parse_mention_error"), args)

            snowflake = int(search.group(7)) if value != "@everyone" else None

            if (search.group(5) and
                (cls.option == SlashCommandOptionType.role
                 or cls.option == SlashCommandOptionType.mentionable)
                ) or value == "@everyone":
                if value == "@everyone":
                    return ctx.guild.roles[0]
                else:
                    return ctx.guild.get_role(snowflake)
            elif search.group(6) and (cls.option == SlashCommandOptionType.user
                                      or cls.option
                                      == SlashCommandOptionType.mentionable):
                if ctx.guild:
                    return ctx.guild.get_member(snowflake)
                else:
                    return ctx.bot.get_user(snowflake)
            elif search.group(4) and (
                    cls.option == SlashCommandOptionType.channel
                    or cls.option == SlashCommandOptionType.mentionable):
                return ctx.guild.get_channel(snowflake)
            else:
                raise ArgumentParseException.with_pointer(
                    translator.translate(
                        "mrvn_api_command_parse_mention_error"), args)
        except IndexError:
            logging.error(traceback.format_exc())

            raise ArgumentParseException.with_pointer(
                translator.translate("mrvn_api_command_parse_mention_error"),
                args)
Esempio n. 7
0
    def get_subcommand_tree(self,
                            command: SlashCommandGroup,
                            translator: Translator = Translator()):
        tree = defaultdict(dict)
        node = tree[f"__{command.name}__"]

        for sub_cmd in command.subcommands:
            if isinstance(sub_cmd, SlashCommandGroup):
                node.update(self.get_subcommand_tree(sub_cmd, translator))
            else:
                node[self.get_command_desc(sub_cmd, translator)] = {}

        return tree
Esempio n. 8
0
    def get_command_desc(self,
                         command: Union[SlashCommand, SlashCommandGroup],
                         translator: Translator = Translator(),
                         as_tree=False):
        if isinstance(command, SlashCommand):
            options = []

            for option in command.options:
                options.append(
                    ("<%s>" if option.required else "[%s]") %
                    f"`{option.name}`: *{translator.translate(f'mrvn_core_commands_option_{option.input_type.name}')}*"
                )

            parents = []

            parent = command

            while parent.parent is not None:
                parent = parent.parent

                parents.append(parent.name)

            parents.reverse()

            return f"{' '.join(parents)} **{command.name}** {' '.join(options) if len(options) else translator.translate('mrvn_core_commands_no_args')}"
        else:
            if as_tree:
                return LeftAligned()(self.get_subcommand_tree(
                    command, translator))
            else:
                sub_cmds = []

                for sub_cmd in command.subcommands:
                    if isinstance(sub_cmd, SlashCommandGroup):
                        sub_cmds.append(
                            f"<{self.get_command_desc(sub_cmd, translator)}>")
                    else:
                        sub_cmds.append(sub_cmd.name)

                return f"**{command.name}** <{'|'.join(sub_cmds)}>"
Esempio n. 9
0
 def parse_value(
     cls,
     ctx: MrvnMessageContext,
     args: PreparedArguments,
     translator: Translator = Translator()) -> any:
     return args.next().value