[英]discord.py How to make a roll dice command
大家好,我正在 Python 中編寫 Discord 機器人,我想編寫擲骰子命令。 而且我認為我做錯了什么。 這是代碼:
@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!")
當我鍵入命令和 select 編號 4 時,我的命令確實有效,但是當我嘗試另一個數字時,例如 6,它不起作用。 我究竟做錯了什么? 請幫忙。
那是因為它首先檢查4 ,然后是6 ,依此類推......即使在你發送4之后,它也會檢查6 、 8等等。當你發送6時,它會先檢查4 ,然后6等等..你為什么不嘗試這樣的事情:
@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.")
注意:我建議使用await asyncio.sleep(1)
而不是 time.sleep( time.sleep(1)
。 這樣其他命令仍然可以正常工作。 在這種情況下,請記住導入asyncio
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.