diff options
| author | 2020-04-07 14:14:14 +0300 | |
|---|---|---|
| committer | 2020-04-07 14:14:14 +0300 | |
| commit | 3d92f7e907fcee767c1428a52bb503314b8f6b19 (patch) | |
| tree | d132319b798c4af8b58637576b3cd4a9e855abbf | |
| parent | (Constants, TicTacToe): Added X and O emojis. (diff) | |
(TicTacToe): Added `get_confirmation` function to `Game` class to make sure that opponent want to play.
Diffstat (limited to '')
| -rw-r--r-- | bot/exts/evergreen/tic_tac_toe.py | 41 | 
1 files changed, 41 insertions, 0 deletions
| diff --git a/bot/exts/evergreen/tic_tac_toe.py b/bot/exts/evergreen/tic_tac_toe.py index 0fa1902b..7d82e084 100644 --- a/bot/exts/evergreen/tic_tac_toe.py +++ b/bot/exts/evergreen/tic_tac_toe.py @@ -1,9 +1,16 @@ +import asyncio  import typing as t  import discord  from discord.ext.commands import Cog, Context  from bot.bot import SeasonalBot +from bot.constants import Emojis + +CONFIRMATION_MESSAGE = ( +    "{opponent}, {requester} want to play Tic-Tac-Toe against you. React to this message with " +    f"{Emojis.confirmation} to accept or with {Emojis.decline} to decline." +)  class Player: @@ -28,6 +35,40 @@ class Game:          self.winner: t.Optional[Player] = None          self.loser: t.Optional[Player] = None +    async def get_confirmation(self) -> t.Tuple[bool, t.Optional[str]]: +        """Ask does user want to play TicTacToe against requester. First player is always requester.""" +        confirm_message = await self.ctx.send( +            CONFIRMATION_MESSAGE.format( +                opponent=self.players[1].user.mention, +                requester=self.players[0].user.mention +            ) +        ) +        await confirm_message.add_reaction(Emojis.confirmation) +        await confirm_message.add_reaction(Emojis.decline) + +        def confirm_check(reaction: discord.Reaction, user: discord.User) -> bool: +            return ( +                reaction.emoji in (Emojis.confirmation, Emojis.decline) +                and reaction.message.id == confirm_message.id +                and user == self.players[1].user +            ) + +        try: +            reaction, user = await self.ctx.bot.wait_for( +                "reaction_add", +                timeout=60.0, +                check=confirm_check +            ) +        except asyncio.TimeoutError: +            await confirm_message.delete() +            return False, "Running out of time... Cancelled game." + +        await confirm_message.delete() +        if reaction.emoji == Emojis.confirmation: +            return True, None +        else: +            return False, "User declined" +  class TicTacToe(Cog):      """TicTacToe cog contains tic-tac-toe game commands.""" | 
