【问题标题】:How to make the bot send a message with a invite link of the guild to a specific channel in my guild whenever it joins a new server?如何让机器人在加入新服务器时向我的公会中的特定频道发送带有公会邀请链接的消息?
【发布时间】:2020-12-09 22:14:47
【问题描述】:

所以基本上我想要的是每当我的机器人加入任何公会时,它应该向我服务器中的特定频道发送一条消息,说明它被邀请到具有其名称的服务器,如果可能的话,还包括该服务器的邀请链接。我尝试了一些方法,但它们从未真正奏效。

@client.event
async def on_guild_join(guild):
    channel = client.get_channel('736984092368830468')
    await channel.send(f"Bot was added to {guild.name}")

它不起作用并引发以下错误:

Ignoring exception in on_guild_join
Traceback (most recent call last):
  File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\Rohit\Desktop\discord bots\test bot\main.py", line 70, in on_guild_join
    await channel.send(f"Bot was added to {guild.name}")
AttributeError: 'NoneType' object has no attribute 'send'

而且我真的不知道如何让机器人发送带有公会名称的公会邀请链接。

【问题讨论】:

  • 尝试channel = client.get_channel(736984092368830468) 将频道作为整数。
  • 不起作用@ThRnk 已经尝试过了

标签: python discord.py


【解决方案1】:

您收到'NoneType' object has no attribute 'send' 异常的原因是您的机器人未能找到提供的频道。

这一行:

channel = client.get_channel('736984092368830468')

不行,这是因为频道ID必须是整数,你可以试试这个:

channel = client.get_channel(int(736984092368830468))

如果这仍然不起作用,请确保机器人可以访问频道、频道存在并且提供的 ID 正确。

【讨论】:

    【解决方案2】:

    假设您的机器人具有所需的权限,您将如何获得邀请、名称和公会图标。

    @client.event
    async def on_guild_join(guild):
        channel = client.get_channel(745056821777006762)
        invite = await guild.system_channel.create_invite()
    
        e = discord.Embed(title="I've joined a server.")
        e.add_field(name="Server Name", value=guild.name, inline=False)
        e.add_field(name="Invite Link", value=invite, inline=False)
        e.set_thumbnail(url=guild.icon_url)
        await channel.send(embed=e)
    

    【讨论】:

      猜你喜欢
      • 2021-04-08
      • 2019-01-31
      • 2019-08-28
      • 2022-01-25
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      相关资源
      最近更新 更多