Discord.py-on_reaction_add()缺少1个必需的位置参数:“ user”

时间:2020-09-29 12:35:57

标签: discord.py discord.py-rewrite

获取用户输入或获取用户反应的表情符号时出现问题。

我收到typeError on_reaction_add() missing 1 required positional argument: 'user',我的代码在user中有on_reaction_add(),但不确定参数user的作用是什么?

这是我给机器人的代码:

class Changelog(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_ready(self):
        print('Application is loaded')

    @commands.group(invoke_without_command=True)
    async def application(self, ctx):
        embed = discord.Embed(title="Application Commands",
                              description="Channel: <#channel>", color=0)
        await ctx.send(embed=embed)

    @application.command()
    async def channel(self, ctx, channel: discord.TextChannel):
        if ctx.message.author.guild_permissions.administrator:

            db = sqlite3.connect('main.sqlite')
            cursor = db.cursor()
            cursor.execute(
                f'SELECT channel_id FROM application WHERE guild_id = {ctx.guild.id}')
            result = cursor.fetchone()
            if result is None:
                sql = ('INSERT INTO application(guild_id, channel_id) VALUES(?,?)')
                val = (ctx.guild.id, channel.id)
                await ctx.send(f'Message has been sent and channel has been set to {channel.mention}')
            elif result is not None:
                sql = ('UPDATE application SET channel_id = ? WHERE guild_id = ?')
                val = (channel.id, ctx.guild.id)
                await ctx.send(f'Message has been sent and channel has been updated to {channel.mention}')
            youtube = ':play_pause:'
            staff = ':envelope_with_arrow:'
            embed = discord.Embed(title="ReefCraft Applications", color=0)
            embed.add_field(
                name="** **", value=f"{youtube} YouTube Application\n\n{staff} Staff Application", inline=False)
            embed.add_field(name="\n\nInformation",
                            value="Reacting to one of the emotes will create a new text-channel, where you will write your applicaiton!")
            reaction_message = await channel.send(embed=embed)
            for emoji in emojis:
                await reaction_message.add_reaction(emoji)
            cursor.execute(sql, val)
            db.commit()
            cursor.close()
            db.close()

    @commands.Cog.listener()
    async def on_reaction_add(self, ctx, reaction, user):
        emoji = reaction.emoji

        if user.bot:
            return

        if emoji == "\U0001F4E9":
            await ctx.send("You clicked the Staff Application")
        elif emoji == "\U000023EF":
            await ctx.send("You clicked the Youtube Application")
        else:
            return


def setup(client):
    client.add_cog(Changelog(client))

1 个答案:

答案 0 :(得分:1)

离开documentationon_reaction_add仅需要2个参数,您提供了3个(ctx,reaction和user)。这样,每当Discord触发此事件时,它只会传入2个参数,而user将被忽略,从而导致您的错误(缺少第三个参数user)。

您应该只删除ctx作为参数。

但不是很确定参数用户实际上是做什么的吗?

user代表添加了反应的人的discord.User实例。

@commands.Cog.listener()
async def on_reaction_add(self, reaction, user):