Discord.py开发反应响应机器人时wait_for缺失event参数报错如何解决?
问题原因
你遇到的报错本质是类和实例对象用法混淆:
discord.ext.commands.Bot是类模板,wait_for、get_channel都属于实例方法,只能通过你已经初始化的Bot实例(也就是你代码里的client对象)调用,不能直接用Bot类本身调用- 直接用类调用实例方法时,Python会把你传入的第一个参数识别为要求的
self占位符,所以你传的"reaction_add"被当成了self参数,系统自然提示你缺少必填的event参数
修复步骤
- 修改报错行的调用对象:将
confirmation = await Bot.wait_for("reaction_add", check=check)改为confirmation = await client.wait_for("reaction_add", check=check) - 同步修改下一行的get_channel调用:将
channel = Bot.get_channel(704609444092182632)改为channel = client.get_channel(704609444092182632)
修复后的完整代码
import discord from discord.utils import get from discord.ext import commands from discord.ext.commands import Bot client = commands.Bot(command_prefix = "!") # 此处替换为你自己的机器人TOKEN TOKEN = "xxx" @client.event async def on_ready(): print('We have logged in as {0.user}'.format(client)) @client.command(pass_context = "true") async def ping(ctx): def check(reaction, user): return user == ctx.author and str(reaction.emoji) in ["✅"] and reaction.message == message message = await ctx.send("Test emoji") await message.add_reaction("✅") confirmation = await client.wait_for("reaction_add", check=check) channel = client.get_channel(704609444092182632) if confirmation: await channel.send("yay") client.run(TOKEN)
内容的提问来源于stack exchange,提问作者LLiver
相关产品推荐
相关产品推荐

