Esempio n. 1
0
    async def cards(self, ctx: commands.Context, *, arg: commands.clean_content = ''):
        self.logger.info(f'Searching for cards "{arg}".')

        try:
            arguments = parse_arguments(arg)
            cards = self.get_cards(ctx, arguments)
            sort, sort_op = arguments.single('sort', None,
                                             allowed_operators=['<', '>', '='], converter=card_attribute_aliases)
            display, _ = arguments.single(['display', 'disp'], sort or CardAttribute.Power, allowed_operators=['='],
                                          converter=card_attribute_aliases)
        except ArgumentError as e:
            await ctx.send(str(e))
            return

        listing = []
        for card in cards:
            display_prefix = display.get_formatted_from_card(card)
            if display_prefix:
                listing.append(
                    f'{display_prefix} {self.format_card_name_for_list(card)}')
            else:
                listing.append(self.format_card_name_for_list(card))

        embed = discord.Embed(title=f'Card Search "{arg}"' if arg else 'Cards')
        asyncio.ensure_future(run_paged_message(ctx, embed, listing))
Esempio n. 2
0
    async def card(self, ctx: commands.Context, *, arg: commands.clean_content):
        self.logger.info(f'Searching for card "{arg}".')

        try:
            arguments = parse_arguments(arg)
            cards = self.get_cards(ctx, arguments)
        except ArgumentError as e:
            await ctx.send(str(e))
            return

        if not cards:
            await ctx.send(f'No results for card "{arg}"')
            return

        if len(cards) == 1 or arguments.text():
            embeds = self.get_card_embeds(cards[0])
            asyncio.ensure_future(run_tabbed_message(ctx, self.rarity_emoji, embeds, starting_index=1))
        else:
            message = await ctx.send(embed=self.get_card_embeds(cards[0])[1])

            emojis = self.rarity_emoji + ['◀', '▶']

            index = 0
            limit_break = 1

            async def callback(emoji):
                nonlocal index
                nonlocal limit_break
                try:
                    emoji_index = emojis.index(emoji)
                    if emoji_index == 0:
                        limit_break = 0
                    elif emoji_index == 1:
                        limit_break = 1
                    elif emoji_index == 2:
                        index -= 1
                    else:
                        index += 1

                    index = min(len(cards) - 1, max(0, index))

                    await message.edit(embed=self.get_card_embeds(cards[index])[limit_break])
                except ValueError:
                    pass

            asyncio.ensure_future(run_reaction_message(ctx, message, emojis, callback))
Esempio n. 3
0
    async def parse_event_argument(self, ctx, arg):
        arguments = parse_arguments(arg)
        await arguments.update_preferences(ctx)
        text = arguments.text()
        arguments.require_all_arguments_used()

        if text:
            # Allows relative id searches like `!event +1` for next event or `!event -2` for the event before last event
            if text[0] in ['-', '+']:
                try:
                    latest = self.bot.master_filters.events.get_latest_event(
                        ctx)
                    event = self.bot.master_filters.events.get(
                        str(latest.id + int(text)), ctx)
                except ValueError:
                    event = self.bot.master_filters.events.get(text, ctx)
            else:
                event = self.bot.master_filters.events.get(text, ctx)
        else:
            event = self.bot.master_filters.events.get_latest_event(ctx)
        return event, ctx.preferences.timezone
Esempio n. 4
0
    async def songs(self,
                    ctx: commands.Context,
                    *,
                    arg: commands.clean_content = ''):
        self.logger.info(
            f'Searching for songs "{arg}".' if arg else 'Listing songs.')
        arguments = parse_arguments(arg)

        try:
            sort, sort_op = arguments.single('sort',
                                             MusicAttribute.DefaultOrder,
                                             allowed_operators=['<', '>', '='],
                                             converter=music_attribute_aliases)
            reverse_sort = sort_op == '<' or arguments.tag('reverse')
            display, _ = arguments.single(['display', 'disp'],
                                          sort,
                                          allowed_operators=['='],
                                          converter=music_attribute_aliases)
            units = {
                self.bot.aliases.units_by_name[unit].id
                for unit in arguments.tags(
                    names=self.bot.aliases.units_by_name.keys(),
                    aliases=self.bot.aliases.unit_aliases)
            }

            def difficulty_converter(d):
                return int(d[:-1]) + 0.5 if d[-1] == '+' else int(d)

            difficulty = arguments.repeatable(['difficulty', 'diff', 'level'],
                                              is_list=True,
                                              converter=difficulty_converter)

            songs = self.bot.asset_filters.music.get_sorted(
                arguments.text(), ctx)

            arguments.require_all_arguments_used()
        except ArgumentError as e:
            await ctx.send(str(e))
            return

        for value, op in difficulty:
            operator = list_operator_for(op)
            songs = [
                song for song in songs if operator(song.charts[4].level, value)
            ]

        if units:
            songs = [song for song in songs if song.unit.id in units]

        if not (arguments.text_argument
                and sort == MusicAttribute.DefaultOrder):
            songs = sorted(songs,
                           key=lambda s: sort.get_sort_key_from_music(s))
            if sort == MusicAttribute.DefaultOrder and songs and songs[
                    0].id == 1:
                songs = [*songs[1:], songs[0]]
            if sort in [MusicAttribute.Level, MusicAttribute.Date]:
                songs = songs[::-1]
            if reverse_sort:
                songs = songs[::-1]

        listing = []
        for song in songs:
            display_prefix = display.get_formatted_from_music(song)
            if display_prefix:
                listing.append(
                    f'{display_prefix} : {song.name}{" (" + song.special_unit_name + ")" if song.special_unit_name else ""}'
                )
            else:
                listing.append(
                    f'{song.name}{" (" + song.special_unit_name + ")" if song.special_unit_name else ""}'
                )

        embed = discord.Embed(title=f'Song Search "{arg}"' if arg else 'Songs')
        asyncio.ensure_future(run_paged_message(ctx, embed, listing))