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:
@client.command() async def rolldice(ctx): dice4 = ["1","2","3","4"] dice6 = ["1","2","3","4","5","6"] dice8 = ["1","2","3","4","5","6","7","8"] dice10 = ["1","2","3","4","5","6","7","8","9","10"] dice12 = ["1","2","3","4","5","6","7","8","9","10","11","12"] dice20 = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"] message = await ctx.send("Choose a number:n**4**, **6**, **8**, **10**, **12**, **20** ") def check4(m): return m.author == ctx.author and m.content == "4" try: await client.wait_for('message', check=check4, timeout=30.0) coming = await ctx.send("Here it comes...") time.sleep(1) await coming.delete() await ctx.send(f"**{random.choice(dice4)}**") except asyncio.TimeoutError: await message.delete() await ctx.send("Procces has been canceled because you didn't answer in **30** second!") def check6(m): return m.author == ctx.author and m.content == "6" try: await client.wait_for('message', check=check6, timeout=30.0) coming = await ctx.send("Here it comes...") time.sleep(1) await coming.delete() await ctx.send(f"**{random.choice(dice6)}**") except asyncio.TimeoutError: await message.delete() await ctx.send("Procces has been canceled because you didn't answer in **30** second!") def check8(m): return m.author == ctx.author and m.content == "8" try: await client.wait_for('message', check=check8, timeout=30.0) coming = await ctx.send("Here it comes...") time.sleep(1) await coming.delete() await ctx.send(f"**{random.choice(dice8)}**") except asyncio.TimeoutError: await message.delete() await ctx.send("Procces has been canceled because you didn't answer in **30** second!") def check10(m): return m.author == ctx.author and m.content == "10" try: await client.wait_for('message', check=check10, timeout=30.0) coming = await ctx.send("Here it comes...") time.sleep(1) await coming.delete() await ctx.send(f"**{random.choice(dice10)}**") except asyncio.TimeoutError: await message.delete() await ctx.send("Procces has been canceled because you didn't answer in **30** second!") def check12(m): return m.author == ctx.author and m.content == "12" try: await client.wait_for('message', check=check12, timeout=30.0) coming = await ctx.send("Here it comes...") time.sleep(1) await coming.delete() await ctx.send(f"**{random.choice(dice12)}**") except asyncio.TimeoutError: await message.delete() await ctx.send("Procces has been canceled because you didn't answer in **30** second!") def check20(m): return m.author == ctx.author and m.content == "20" try: await client.wait_for('message', check=check20, timeout=30.0) coming = await ctx.send("Here it comes...") time.sleep(1) await coming.delete() await ctx.send(f"**{random.choice(dice20)}**") except asyncio.TimeoutError: await message.delete() await ctx.send("Procces has been canceled because you didn't answer in **30** second!")
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:
@client.command() async def rolldice(ctx): message = await ctx.send("Choose a number:n**4**, **6**, **8**, **10**, **12**, **20** ") def check(m): return m.author == ctx.author try: message = await client.wait_for("message", check = check, timeout = 30.0) m = message.content if m != "4" and m != "6" and m != "8" and m != "10" and m != "12" and m != "20": await ctx.send("Sorry, invalid choice.") return coming = await ctx.send("Here it comes...") time.sleep(1) await coming.delete() await ctx.send(f"**{random.randint(1, int(m))}**") except asyncio.TimeoutError: await message.delete() await ctx.send("Procces has been canceled because you didn't respond in **30** seconds.")
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.