Discord.py Join/Leave 留言

Discord.py Join/Leave Message

我正在尝试让我的 Join/Leave 消息正常工作,但不确定我是否做对了。我与机器人用户进行了测试,但它在控制台中引发了错误。这是我的代码:

    @commands.Cog.listener()
    async def on_member_join(self, ctx, *, member):
        ctx.channel = get(ctx.member.guild.channels, name="join-leave")

        embed = discord.Embed(color=0x4a3d9a)
        embed.add_field(name="Welcome", value=f"{member.name} has joined {member.guild.name}", inline=False)
        embed.set_image(url="https://newgitlab.elaztek.com/NewHorizon-Development/discord-bots/Leha/-/raw/master/res/welcome.gif")
        await self.client.send_message(ctx.channel, embed=embed)

    @commands.Cog.listener()
    async def on_member_remove(self, ctx, *, member):
        ctx.channel = get(ctx.member.guild.channels, name="join-leave")

        embed = discord.Embed(color=0x4a3d9a)
        embed.add_field(name="Welcome", value=f"{member.name} has left {member.guild.name}", inline=False)
        embed.set_image(url="https://newgitlab.elaztek.com/NewHorizon-Development/discord-bots/Leha/-/raw/master/res/goodbye.gif")
        await self.client.send_message(ctx.channel, embed=embed)

这是它抛出的错误:

Ignoring exception in on_member_join
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/discord/client.py", line 312, in _run_event
    await coro(*args, **kwargs)
TypeError: on_member_join() missing 1 required positional argument: 'member'
Ignoring exception in on_member_remove
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/discord/client.py", line 312, in _run_event
    await coro(*args, **kwargs)
TypeError: on_member_remove() missing 1 required positional argument: 'member'

非常感谢任何帮助。如果有帮助的话,我也在重写分支上。

您只需为 on_member_join 传递一个参数,如 docs 中指定的那样。获取频道的最简单方法是循环遍历它们,然后按如下方式发送嵌入:

@commands.Cog.listener()
async def on_member_join(self, member):
    for channel in member.guild.channels:
        if str(channel) == "join-leave":
            embed = discord.Embed(color=0x4a3d9a)
            embed.add_field(name="Welcome", value=f"{member.name} has joined {member.guild.name}", inline=False)
            embed.set_image(url="https://newgitlab.elaztek.com/NewHorizon-Development/discord-bots/Leha/-/raw/master/res/welcome.gif")
            await channel.send(embed=embed)