代码之家  ›  专栏  ›  技术社区  ›  As2Bax

discord.py-当on_member_ban事件运行时,“TypeError:on_member_ban()接受1个位置参数,但给定了2个”错误

  •  0
  • As2Bax  · 技术社区  · 2 年前

    我正在制作一个日志系统。(最后感谢上帝,这是另一件事)直到现在,一切都很顺利。我的on_member_ban事件有问题,它给了我一个错误,说 TypeError: on_member_ban() takes 1 positional argument but 2 were given 。我什么都试过了,没有答案。绝对没有。问题是,我没有看到两个位置论点。有人知道如何修复这个错误吗?谢谢

    代码:

    @bot.event
    async def on_member_ban(message):
        async for entry in message.guild.audit_logs(limit=1,action=discord.AuditLogAction.ban):
            with open("database\\wae.json", encoding="utf-8") as f:
                data = json.load(f)
            if str(message.guild.id) in data:
                isenabled = data[str(message.guild.id)]['logchlID']
                if isenabled == 0:
                    return
                else:
                    channelrs = bot.get_channel(data[str(message.guild.id)]['logchlID'])
                    embed = discord.Embed(title=f"Member Banned", description=f"", color=discord.Colour.red())
                    embed.add_field(name="Banned by", value=f"{message.author}")
                    embed.add_field(name="Reason", value=f"{message.reason}")
                    embed.set_author(name=f"{entry.author.display_name} (@{entry.author.name})", icon_url=message.author.avatar)
                    embed.set_thumbnail(url=message.author.avatar)
                    await channelrs.send(embed=embed) 
    
    1 回复  |  直到 2 年前
        1
  •  2
  •   Joseph Langford    2 年前

    https://discordpy.readthedocs.io/en/stable/api.html?highlight=on_member_ban#discord.on_member_ban

    A screenshot of the relevant docs attached

    编辑:包括一个文本块,以防网站关闭:)

    discord.on_member_ban(guild, user)¶
    Called when user gets banned from a Guild.
    
    This requires Intents.moderation to be enabled.
    
    Parameters
    guild (Guild) – The guild the user got banned from.
    
    user (Union[User, Member]) – The user that got banned. Can be either User or Member depending if the user was in the guild or not at the time of removal.
    

    根据这里的文档,看起来这个事件将同时传递用户被禁止加入的公会的公会对象和被禁止加入该公会的用户。看起来你正在覆盖这些事件回调,所以你需要坚持在discord.py端传递的参数,希望这有帮助!

    编辑:在被充分拍打手腕后,这里有一个你可以做的事情的例子

    @bot.event
    async def on_member_ban(guild, user):
        async for entry in guild.audit_logs(limit=1,action=discord.AuditLogAction.ban):
            with open("database\\wae.json", encoding="utf-8") as f:
                data = json.load(f)
            if str(guild.id) in data:
                isenabled = data[str(guild.id)]['logchlID']
                if isenabled == 0:
                    return
                else: ...