Exemplo n.º 1
0
    def predicate(self, ctx: commands.Context, announcement: discord.Message,
                  reaction: discord.Reaction, user: discord.Member) -> bool:
        # If they've joined a game since requesting a player 2
        if self.already_playing(ctx.author):
            return True  # Is dealt with later on
        if (user.id not in (ctx.me.id, ctx.author.id)
                and str(reaction.emoji) == ACCEPT
                and reaction.message.id == announcement.id):
            if self.already_playing(user):
                self.bot.loop.create_task(
                    ctx.send(f"{user.mention} You're already playing a game!"))
                self.bot.loop.create_task(
                    announcement.remove_reaction(reaction, user))
                return False

            if user in self.waiting:
                self.bot.loop.create_task(
                    ctx.send(
                        f"{user.mention} Please cancel your game first before joining another one."
                    ))
                self.bot.loop.create_task(
                    announcement.remove_reaction(reaction, user))
                return False

            return True

        if (user.id == ctx.author.id and str(reaction.emoji) == CANCEL
                and reaction.message.id == announcement.id):
            return True
        return False
Exemplo n.º 2
0
async def get_input(
        msg: discord.Message,
        user: discord.abc.Snowflake,
        reactions: Dict[Union[discord.Emoji, discord.PartialEmoji, str], T],
        *,
        timeout: Optional[float] = None,
        unreact: bool = True) -> Optional[Union[T, discord.Message]]:
    assert discord_client.client.user is not None
    reacts = {emoji_key(key): value for key, value in reactions.items()}
    with plugins.reactions.ReactionMonitor(
            channel_id=msg.channel.id,
            message_id=msg.id,
            author_id=user.id,
            event="add",
            filter=lambda _, p: emoji_key(p.emoji) in reacts,
            timeout_each=timeout) as mon:
        try:
            await asyncio.gather(*(msg.add_reaction(key) for key in reactions))
        except (discord.NotFound, discord.Forbidden):
            pass
        msg_task = asyncio.create_task(
            discord_client.client.wait_for("message",
                                           check=lambda m: m.channel == msg.
                                           channel and m.author.id == user.id))
        reaction_task = asyncio.ensure_future(mon)
        try:
            done, pending = await asyncio.wait(
                (msg_task, reaction_task),
                timeout=timeout,
                return_when=asyncio.FIRST_COMPLETED)
        except asyncio.TimeoutError:
            return None
    if msg_task in done:
        reaction_task.cancel()
        if unreact:
            try:
                await asyncio.gather(
                    *(msg.remove_reaction(key, discord_client.client.user)
                      for key in reactions))
            except (discord.NotFound, discord.Forbidden):
                pass
        return msg_task.result()
    elif reaction_task in done:
        msg_task.cancel()
        _, payload = reaction_task.result()
        if unreact:
            try:
                await asyncio.gather(
                    *(msg.remove_reaction(key, discord_client.client.user)
                      for key in reactions
                      if emoji_key(key) != emoji_key(payload.emoji)))
            except (discord.NotFound, discord.Forbidden):
                pass
        return reacts.get(emoji_key(payload.emoji))
    else:
        return None
Exemplo n.º 3
0
 async def remove_reactions(self, message: discord.Message):
     tasks = []
     for r in message.reactions:
         if r.me:
             tasks.append(asyncio.create_task(
                 message.remove_reaction(r, self.bot.user)))
     await asyncio.gather(*tasks)
Exemplo n.º 4
0
async def get_reaction(msg: discord.Message,
                       user: discord.abc.Snowflake,
                       reactions: Dict[Union[discord.Emoji,
                                             discord.PartialEmoji, str], T],
                       *,
                       timeout: Optional[float] = None,
                       unreact: bool = True) -> Optional[T]:
    assert discord_client.client.user is not None
    reacts = {emoji_key(key): value for key, value in reactions.items()}
    with plugins.reactions.ReactionMonitor(
            channel_id=msg.channel.id,
            message_id=msg.id,
            author_id=user.id,
            event="add",
            filter=lambda _, p: emoji_key(p.emoji) in reacts,
            timeout_each=timeout) as mon:
        try:
            await asyncio.gather(*(msg.add_reaction(key) for key in reactions))
        except (discord.NotFound, discord.Forbidden):
            pass
        try:
            _, payload = await mon
        except asyncio.TimeoutError:
            return None
    if unreact:
        try:
            await asyncio.gather(
                *(msg.remove_reaction(key, discord_client.client.user)
                  for key in reactions
                  if emoji_key(key) != emoji_key(payload.emoji)))
        except (discord.NotFound, discord.Forbidden):
            pass
    return reacts.get(emoji_key(payload.emoji))
Exemplo n.º 5
0
    def get_player(self, ctx: commands.Context, announcement: discord.Message,
                   reaction: discord.Reaction, user: discord.Member) -> bool:
        """ Predicate checking the criteria for the announcement message.
        :param ctx: The context.
        :param announcement: The message.
        :param reaction: The reaction used.
        :param user: The user to get. """

        if self.already_playing(
                ctx.author
        ):  # If they've joined a game since requesting a player 2
            return True  # It's dealt with later on

        if (user.id not in (ctx.me.id, ctx.author.id)
                and str(reaction.emoji) == Emojis.hand_raised
                and reaction.message.id == announcement.id):
            if self.already_playing(user):
                self.client.loop.create_task(
                    ctx.send(f"{user.mention} You're already playing a game!"))
                self.client.loop.create_task(
                    announcement.remove_reaction(reaction, user))
                return False

            if user in self.waiting:
                self.client.loop.create_task(
                    ctx.send(
                        f"{user.mention} Please cancel your game first before joining another one."
                    ))
                self.client.loop.create_task(
                    announcement.remove_reaction(reaction, user))
                return False

            return True

        if (user.id == ctx.author.id and str(reaction.emoji) == CROSS_EMOJI
                and reaction.message.id == announcement.id):
            return True
        return False