問題描述
discord.py ‑ 猜謎遊戲無響應(更新) (discord.py ‑ Guessing game no response (updated))
以下代碼應該在我的 discord 服務器上鍵入 $play
時運行,但運行時沒有任何反應。..
client = commands.Bot(command_prefix='$')
@client.command(name="play")
async def play(ctx):
def check(m):
return m.author == ctx.author and m.channel == ctx.message.channel and m.content.isdigit()
number = random.randint(1,100)
await ctx.send('I have a number in mind between 1 and 100, guess it')
for i in range(0,5):
guess = await client.wait_for("message", check=check)
if int(guess.content) > number:
await ctx.send("The number is greater")
elif int(guess.content) < number:
await ctx.send("The number is smaller")
elif int(guess.content) == number:
await ctx.send("You guessed the number!!.")
else:
return ("It has to be a positive integer between 1 to 100")
else:
await ctx.send("You lost, type $play to play again.")
參考解法
方法 1:
It looks like your on_message
event is blocking commands from working.
Add the following to your on_message
event:
@client.event
async def on_message(message):
if message.author == client.user:
return
# Your next events
await client.process_commands(message) # Process commands
- We added this because you have overwritten the normal
on_message
event. process_commands
makes sure thatcommands
are recognized.
See the docs for more information.