Пример #1
0
    async def poll_debug(self, ctx: MyContext, poll: PollManager):
        """Create a dummy reaction on a running poll"""

        user_id = random.choice([
            member.id for member in poll.message.guild.members
            if not member.bot
        ])
        if user_id in poll.votes:
            event = 'REACTION REMOVE'
            emoji = poll.emojis[poll.votes[user_id]]
        else:
            event = 'REACTION ADD'
            emoji = random.choice(poll.emojis)
        payload = discord.RawReactionActionEvent(
            {
                'guild_id': poll.message.guild.id,
                'channel_id': poll.channel_id,
                'message_id': poll.message_id,
                'user_id': user_id
            },
            discord.PartialEmoji(name=emoji),
            event
        )
        self.bot.dispatch('raw_' + event.lower().replace(' ', '_'), payload)
        await ctx.reply(f'Dispatched a {event} event')
Пример #2
0
    async def fake_button_press(self, menu, member, button):
        """Call the function that handles button press with a fake payload."""

        # this is hackish
        emoji = discord.PartialEmoji(name=button)
        payload = discord.RawReactionActionEvent(
            data={
                'message_id': menu.message.id,
                'channel_id': menu.message.channel.id,
                'user_id': member.id,
                'guild_id': menu.message.guild.id,
            },
            emoji=emoji,
            event_type='REACTION_ADD',
        )

        if emoji.name == menus.BUTTONS['clear']:
            await menu.on_clear(payload)
        else:
            await menu._button_add_role(payload)
Пример #3
0
async def _test(votes: int):
    from app.discordbot import bot
    import asyncio

    import discord

    data = {
        "message_id": 807296983286415411,
        "channel_id": 807289103920922684,
        "user_id": 102795037498167296,
    }
    if votes == 1:
        event_type = "REACTION_ADD"
    else:
        event_type = "REACTION_REMOVE"
    emoji = 667825930923671567

    reaction = discord.RawReactionActionEvent(data, emoji, event_type)
    for _ in range(50):
        await bot.on_raw_reaction_remove(reaction)
        await asyncio.sleep(0.1)
    return "123"
Пример #4
0
async def test_paginator_interface():
    bot = commands.Bot('?')

    with open(__file__, 'rb') as file:
        paginator = FilePaginator(file, max_size=200)

    interface = PaginatorInterface(bot, paginator)

    assert interface.pages == paginator.pages
    assert interface.page_count == len(paginator.pages)

    assert interface.page_size > 200
    assert interface.page_size < interface.max_page_size

    send_kwargs = interface.send_kwargs

    assert isinstance(send_kwargs, dict)
    assert 'content' in send_kwargs

    content = send_kwargs['content']

    assert isinstance(content, str)
    assert len(content) <= interface.page_size

    assert interface.display_page == 0

    # pages have been closed, so adding a line should make a new page
    old_page_count = interface.page_count

    await interface.add_line('a' * 150)

    assert interface.page_count > old_page_count

    # push the page to the end (rounded into bounds)
    interface.display_page = 999
    old_display_page = interface.display_page

    assert interface.pages == paginator.pages

    # page closed, so create new page
    await interface.add_line('b' * 150)

    # ensure page has followed tail
    assert interface.display_page > old_display_page

    # testing with embed interface
    embed_interface = PaginatorEmbedInterface(bot, paginator)

    assert embed_interface.pages[0] == interface.pages[0]

    send_kwargs = embed_interface.send_kwargs

    assert isinstance(send_kwargs, dict)
    assert 'embed' in send_kwargs

    embed = send_kwargs['embed']

    assert isinstance(embed, discord.Embed)

    description = embed.description

    assert content.startswith(description)

    # check for raise on too large page size
    with pytest.raises(ValueError):
        PaginatorInterface(None, commands.Paginator(max_size=2000))

    # check for raise on not-paginator
    with pytest.raises(TypeError):
        PaginatorInterface(None, 4)

    paginator = commands.Paginator(max_size=100)
    for _ in range(100):
        paginator.add_line("test text")

    # test interfacing
    with utils.mock_ctx(bot) as ctx:
        interface = PaginatorInterface(bot, paginator)

        assert not interface.closed

        await interface.send_to(ctx)

        await asyncio.sleep(0.1)
        await interface.add_line("test text")

        assert interface.page_count > 1
        assert not interface.closed

        interface.message.id = utils.sentinel()

        current_page = interface.display_page

        payload = {
            'message_id': interface.message.id,
            'user_id': ctx.author.id,
            'channel_id': ctx.channel.id,
            'guild_id': ctx.guild.id
        }

        # push right button
        emoji = discord.PartialEmoji(
            animated=False,
            name="\N{BLACK RIGHT-POINTING TRIANGLE}",
            id=None
        )
        bot.dispatch(
            'raw_reaction_add',
            discord.RawReactionActionEvent(payload, emoji, 'REACTION_ADD')
            if discord.version_info >= (1, 3) else
            discord.RawReactionActionEvent(payload, emoji)
        )

        await asyncio.sleep(0.1)

        assert interface.display_page > current_page
        assert not interface.closed

        current_page = interface.display_page

        # push left button
        emoji = discord.PartialEmoji(
            animated=False,
            name="\N{BLACK LEFT-POINTING TRIANGLE}",
            id=None
        )
        bot.dispatch(
            'raw_reaction_add',
            discord.RawReactionActionEvent(payload, emoji, 'REACTION_ADD')
            if discord.version_info >= (1, 3) else
            discord.RawReactionActionEvent(payload, emoji)
        )

        await asyncio.sleep(0.1)

        assert interface.display_page < current_page
        assert not interface.closed

        # push last page button
        emoji = discord.PartialEmoji(
            animated=False,
            name="\N{BLACK RIGHT-POINTING DOUBLE TRIANGLE WITH VERTICAL BAR}",
            id=None
        )
        bot.dispatch(
            'raw_reaction_add',
            discord.RawReactionActionEvent(payload, emoji, 'REACTION_ADD')
            if discord.version_info >= (1, 3) else
            discord.RawReactionActionEvent(payload, emoji)
        )

        await asyncio.sleep(0.1)

        assert interface.display_page == interface.page_count - 1
        assert not interface.closed

        # push first page button
        emoji = discord.PartialEmoji(
            animated=False,
            name="\N{BLACK LEFT-POINTING DOUBLE TRIANGLE WITH VERTICAL BAR}",
            id=None
        )
        bot.dispatch(
            'raw_reaction_add',
            discord.RawReactionActionEvent(payload, emoji, 'REACTION_ADD')
            if discord.version_info >= (1, 3) else
            discord.RawReactionActionEvent(payload, emoji)
        )

        await asyncio.sleep(0.1)

        assert interface.display_page == 0
        assert not interface.closed

        # push close button
        emoji = discord.PartialEmoji(
            animated=False,
            name="\N{BLACK SQUARE FOR STOP}",
            id=None
        )
        bot.dispatch(
            'raw_reaction_add',
            discord.RawReactionActionEvent(payload, emoji, 'REACTION_ADD')
            if discord.version_info >= (1, 3) else
            discord.RawReactionActionEvent(payload, emoji)
        )

        await asyncio.sleep(0.1)

        assert interface.closed
        ctx.send.coro.return_value.delete.assert_called_once()

    # test resend, no delete
    with utils.mock_ctx(bot) as ctx:
        interface = PaginatorInterface(bot, paginator)

        assert not interface.closed

        await interface.send_to(ctx)

        await asyncio.sleep(0.1)
        await interface.add_line("test text")

        assert interface.page_count > 1
        assert not interface.closed

        # resend
        await interface.send_to(ctx)

        await asyncio.sleep(0.1)
        await interface.add_line("test text")

        ctx.send.coro.return_value.delete.assert_not_called()

        interface.task.cancel()
        await asyncio.sleep(0.1)

        assert interface.closed

    # test resend, delete
    with utils.mock_ctx(bot) as ctx:
        interface = PaginatorInterface(bot, paginator, delete_message=True)

        assert not interface.closed

        await interface.send_to(ctx)

        await asyncio.sleep(0.1)
        await interface.add_line("test text")

        assert interface.page_count > 1
        assert not interface.closed

        # resend
        await interface.send_to(ctx)

        await asyncio.sleep(0.1)
        await interface.add_line("test text")

        ctx.send.coro.return_value.delete.assert_called_once()

        interface.task.cancel()
        await asyncio.sleep(0.1)

        assert interface.closed