【问题标题】:welcoming new members, member has no attribute 'server'欢迎新成员,成员没有“服务器”属性
【发布时间】:2020-09-12 09:48:42
【问题描述】:

我正在写这篇文章,当有新成员加入时,它会在特定公会的特定频道中发送消息:

@bot.event
async def on_member_join(member):
    channel = get(member.server.channels, id=464298877823221763)
    await c.send(channel,"welcome")

当有新成员加入时,我遇到了奇怪的错误:

Ignoring exception in on_member_join
Traceback (most recent call last):
  File "C:\Python38\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "d:/Documents/Bots/DS BOT/self_bot.py", line 53, in on_member_join
    channel = get(member.server.channels, id=464298877823221763)
AttributeError: 'Member' object has no attribute 'serve

有谁知道如何解决这个错误?

【问题讨论】:

    标签: python python-3.x discord discord.py discord.py-rewrite


    【解决方案1】:

    不是服务器,而是guild,你可以试试这段代码。

    @bot.event
    async def on_member_join(member):
        channel = bot.get_channel(channel-id)
        await channel.send(f"Welcome to {member.guild.name}!")
    

    如果你想发送会员加入的任何地方,你可以这样做

    @bot.event
    async def on_member_join(member):
        await member.channel.send(f"Welcome to {member.guild.name}!")
    

    查看guild documentationmember documentationuser documentation

    注意。有时阅读documentation 会很有帮助,不要忘记这一点。

    如果您在齿轮上使用它,则需要这样做。

    @commands.Cog.listener()
    async def on_member_join(self, member):
        channel = bot.get_channel(channel-id)
        await channel.send(f"Welcome to {member.guild.name}!")
    

    另一个注意事项,我有一段时间没有接触 commands.Cog.listener(),所以预计会出现一些错误

    【讨论】:

      【解决方案2】:

      在 API 中,您应该使用 guild,而不是 server。一个成员也可以在许多不同的公会中,所以在这里使用member.guild 将不起作用。尝试使用client.get_channel(id)

      @bot.event
      async def on_member_join(member):
          channel = bot.get_channel(464298877823221763)
          await channel.send("welcome")
      

      【讨论】:

      • 我编辑了。我忘了从send 方法中删除channel 参数。对不起
      • NameError: name 'send' is not defined 我认为提及我在 cog 中使用此事件很有用
      • 你确定是我的代码造成的吗?因为这里的例外是AttributeError: 'channel' has no attribute 'send'
      • 我在您发布的图片中没有看到任何与 send 相关的内容。我非常怀疑它是来自我的,因为这里应该发生的错误是AttributeError
      猜你喜欢
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 2022-01-01
      • 2021-05-05
      • 2020-10-14
      • 2021-08-20
      相关资源
      最近更新 更多