guessing_game.Bot.guess()   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 35
rs 7.904
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
8
class Bot(Client):
9
    @command()  # note that the parenthesis are optional
10
    async def guess(self, ctx: MessageContext, biggest_number: int):
11
        await ctx.reply(
12
            f"Starting the guessing game!"
13
            f" Pick a number between 0 and {biggest_number}."
14
        )
15
16
        number = random.randint(0, biggest_number)
17
18
        try:
19
            async for next_message in self.loop_for(
20
                "on_message", loop_timeout=60
21
            ):
22
                if next_message.author.bot:
23
                    continue
24
25
                if not next_message.content.isdigit():
26
                    await ctx.channel.send(
27
                        f"{next_message.content} is not a number. Try again!"
28
                    )
29
                    continue
30
31
                guessed_number = int(next_message.content)
32
33
                if guessed_number > number:
34
                    await ctx.channel.send("Number is too high!")
35
                elif guessed_number < number:
36
                    await ctx.channel.send("Number is too low!")
37
                else:
38
                    await next_message.react("🚀")
39
                    await ctx.channel.send("Number is correct!")
40
                    break
41
42
        except TimeoutError:
43
            await ctx.channel.send("You took too long! The game timed out.")
44
45
46
if __name__ == "__main__":
47
    bot = Bot("XXXYOURBOTTOKENHEREXXX", intents=Intents.GUILD_MESSAGES)
48
    bot.run()
49