Hello guys i’m coding a Discord bot in Python and i wanted to code a roll dice command. And i think im doing something wrong. Here is the code:
JavaScript
x
95
95
1
@client.command()
2
async def rolldice(ctx):
3
dice4 = ["1","2","3","4"]
4
dice6 = ["1","2","3","4","5","6"]
5
dice8 = ["1","2","3","4","5","6","7","8"]
6
dice10 = ["1","2","3","4","5","6","7","8","9","10"]
7
dice12 = ["1","2","3","4","5","6","7","8","9","10","11","12"]
8
dice20 = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]
9
10
message = await ctx.send("Choose a number:n**4**, **6**, **8**, **10**, **12**, **20** ")
11
12
def check4(m):
13
return m.author == ctx.author and m.content == "4"
14
15
try:
16
await client.wait_for('message', check=check4, timeout=30.0)
17
coming = await ctx.send("Here it comes...")
18
time.sleep(1)
19
await coming.delete()
20
await ctx.send(f"**{random.choice(dice4)}**")
21
22
except asyncio.TimeoutError:
23
await message.delete()
24
await ctx.send("Procces has been canceled because you didn't answer in **30** second!")
25
26
def check6(m):
27
return m.author == ctx.author and m.content == "6"
28
29
try:
30
await client.wait_for('message', check=check6, timeout=30.0)
31
coming = await ctx.send("Here it comes...")
32
time.sleep(1)
33
await coming.delete()
34
await ctx.send(f"**{random.choice(dice6)}**")
35
36
except asyncio.TimeoutError:
37
await message.delete()
38
await ctx.send("Procces has been canceled because you didn't answer in **30** second!")
39
40
def check8(m):
41
return m.author == ctx.author and m.content == "8"
42
43
try:
44
await client.wait_for('message', check=check8, timeout=30.0)
45
coming = await ctx.send("Here it comes...")
46
time.sleep(1)
47
await coming.delete()
48
await ctx.send(f"**{random.choice(dice8)}**")
49
50
except asyncio.TimeoutError:
51
await message.delete()
52
await ctx.send("Procces has been canceled because you didn't answer in **30** second!")
53
54
def check10(m):
55
return m.author == ctx.author and m.content == "10"
56
57
try:
58
await client.wait_for('message', check=check10, timeout=30.0)
59
coming = await ctx.send("Here it comes...")
60
time.sleep(1)
61
await coming.delete()
62
await ctx.send(f"**{random.choice(dice10)}**")
63
64
except asyncio.TimeoutError:
65
await message.delete()
66
await ctx.send("Procces has been canceled because you didn't answer in **30** second!")
67
68
def check12(m):
69
return m.author == ctx.author and m.content == "12"
70
71
try:
72
await client.wait_for('message', check=check12, timeout=30.0)
73
coming = await ctx.send("Here it comes...")
74
time.sleep(1)
75
await coming.delete()
76
await ctx.send(f"**{random.choice(dice12)}**")
77
78
except asyncio.TimeoutError:
79
await message.delete()
80
await ctx.send("Procces has been canceled because you didn't answer in **30** second!")
81
82
def check20(m):
83
return m.author == ctx.author and m.content == "20"
84
85
try:
86
await client.wait_for('message', check=check20, timeout=30.0)
87
coming = await ctx.send("Here it comes...")
88
time.sleep(1)
89
await coming.delete()
90
await ctx.send(f"**{random.choice(dice20)}**")
91
92
except asyncio.TimeoutError:
93
await message.delete()
94
await ctx.send("Procces has been canceled because you didn't answer in **30** second!")
95
When i type the command and select number 4 my command does works but when i try another number, for an example 6, it doesn’t work. What am i doing wrong? Please help.
Advertisement
Answer
That’s because it checks for 4 first, then 6, and so on… Even after you send 4, it’ll check for 6, 8 and so on.. And when you send 6, it’ll check for 4 first, then 6 and so on.. Why don’t you try something like this instead:
JavaScript
1
23
23
1
@client.command()
2
async def rolldice(ctx):
3
message = await ctx.send("Choose a number:n**4**, **6**, **8**, **10**, **12**, **20** ")
4
5
def check(m):
6
return m.author == ctx.author
7
8
try:
9
message = await client.wait_for("message", check = check, timeout = 30.0)
10
m = message.content
11
12
if m != "4" and m != "6" and m != "8" and m != "10" and m != "12" and m != "20":
13
await ctx.send("Sorry, invalid choice.")
14
return
15
16
coming = await ctx.send("Here it comes...")
17
time.sleep(1)
18
await coming.delete()
19
await ctx.send(f"**{random.randint(1, int(m))}**")
20
except asyncio.TimeoutError:
21
await message.delete()
22
await ctx.send("Procces has been canceled because you didn't respond in **30** seconds.")
23
Note: I recommend using await asyncio.sleep(1)
instead of time.sleep(1)
. That way other commands will still be functional. Remember to import asyncio
in this case.