我想在 discord.py 中创建一个命令,以便稍后检测其他消息,但是在正确检测到消息后,机器人不知道为什么它开始发送垃圾邮件,这是我的代码:
@client.command(aliases = ['dragon-panic'])
async def dp(ctx):
await ctx.send("Welcome to the dragon panic. Scheme win is below. Drag 3 cards.\n" +
"\n Dragon Cards: Fire, Iron, Forest, Sea, Mountain")
await ctx.send("Fire beats Forest\nFire beats Mountain\nForest beats Sea\nForest beats Iron" +
"\nSea beats Fire\nSea beats Mountain\nMountain beats Forest\nMountain beats Iron" +
"\nIron beats Fire\nIron beats Sea")
dragon = ['fire', 'forest', 'sea', 'mountain', 'iron']
@client.event
async def on_message(message):
for i in "dragon":
if 'Fire' in message.content:
await ctx.send('You picked Fire Dragon')
break
elif 'Forest' in message.content:
await ctx.send('You picked Forest Dragon')
break
elif 'Sea' in message.content:
await ctx.send('You picked Sea Dragon')
break
elif 'Mountain' in message.content:
await ctx.send('You picked Mountain Dragon')
break
elif 'Iron' in message.content:
await ctx.send('You picked Iron Dragon')
else:
await ctx.send('No dragon with that name!')
break
break
答案 0 :(得分:0)
正如@Łukasz Kwieciński 指出的那样,不要将事件放入命令中;您的 on_message
事件应该独立于任何命令之外。相反,您应该使用 wait_for
。用这个替换你的代码,它应该可以正常工作:
@client.command(aliases = ['dragon-panic'])
async def dp(ctx):
await ctx.send("Welcome to the dragon panic. Scheme win is below. Drag 3 cards.\n" +
"\n Dragon Cards: Fire, Iron, Forest, Sea, Mountain")
await ctx.send("Fire beats Forest\nFire beats Mountain\nForest beats Sea\nForest beats Iron" +
"\nSea beats Fire\nSea beats Mountain\nMountain beats Forest\nMountain beats Iron" +
"\nIron beats Fire\nIron beats Sea")
dragon = ['Fire', 'Forest', 'Sea', 'Mountain', 'Iron']
def check(m):
return m.channel == ctx.channel and m.author == ctx.author
try:
message = await client.wait_for('message', timeout=7.0, check=check)
except asyncio.TimeoutError:
await ctx.send('Dragon not picked in time')
else:
for i in dragon:
if i.lower() in message.content.lower():
await ctx.send(f'You picked {i} Dragon')
break
else:
await ctx.send('No dragon with that name!')
timeout=7.0
用于限制命令等待下一条消息的时间 - 在本例中为 7 秒。您可以将其修改为您自己的时间值。在此示例中,如果用户在 7 秒内没有响应,则机器人将打印出 Dragon not picked in time
。