Passed
Pull Request — main (#176)
by Yohann
02:05
created

guessing_game.Bot.guess()   B

Complexity

Conditions 7

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 29
rs 7.9279
c 0
b 0
f 0
cc 7
nop 3
1
import random
2
3
from pincer import Client, command
4
from pincer.objects import Intents, MessageContext
5
from pincer.exceptions import TimeoutError
6
7
import logging
8
logging.basicConfig(level=logging.DEBUG)
9
10
11
class Bot(Client):
12
13
    @command(guild=881531065859190804)
14
    async def guess(self, ctx: MessageContext, biggest_number: int):
15
16
        await ctx.reply(f"Starting the guessing game! Pick a number between 0 and {biggest_number}.")
17
        channel = await self.get_channel(ctx.channel_id)
18
        number = random.randint(0, biggest_number)
19
20
        try:
21
            async for next_message, in self.loop_for('on_message', loop_timeout=60):
22
                if next_message.author.bot:
23
                    continue
24
25
                try:
26
                    guessed_number = int(next_message.content)
27
                except ValueError:
28
                    await channel.send(f"{next_message.content} is not a number. Try again!")
29
                    continue
30
31
                if guessed_number > number:
32
                    await channel.send("Number is too high!")
33
                elif guessed_number < number:
34
                    await channel.send("Number is too low!")
35
                else:
36
                    await next_message.react("🚀")
37
                    await channel.send("Number is correct!")
38
                    break
39
40
        except TimeoutError:
41
            await channel.send("You took too long! The game timed out.")
42
43
44
if __name__ == "__main__":
45
    bot = Bot("ODgxNTgzMzc5NTA0NTgyNzI3.YSu8gA.kUPT7OsIwGLH5MrAzLxI1px6wuY", intents=Intents.GUILD_MESSAGES)
46
    bot.run()
47