当我在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.")
看起来on_message
事件正在阻止命令工作。
将以下内容添加到您的on_message
事件中:
@client.event
async def on_message(message):
if message.author == client.user:
return
# Your next events
await client.process_commands(message) # Process commands
- 我们添加此项是因为您已经覆盖了正常的
on_message
事件 - CCD_ 5确保CCD_ 6被识别
有关详细信息,请参阅文档