0

所以我希望机器人在添加到服务器时向服务器所有者发送消息。

这是代码:

@client.event
async def on_guild_join(guild, ctx):
  guild_owner = client.get_user(int(ctx.guild.owner.id))
  await guild_owner.send("hi")

但我总是得到这个错误:

TypeError: on_guild_join() missing 1 required positional argument: 'ctx'

我知道这意味着什么,但我不知道如何解决。

4

1 回答 1

0

首先,正如mousetail 所说,on_guild_join只需要一个参数。你应该只guild作为函数的参数。其次,由于您使用的是guild对象,因此您应该已经能够通过guild.owner. 请查看下面的修改后的代码。

@client.event
async def on_guild_join(guild):
    try:
        await guild.owner.send("I'm now in your server!")
    except: # occurs if your bot cannot send dm to the owner
        print(f"{guild.owner} has their dms turned off")
于 2021-12-29T10:17:52.357 回答