【问题标题】:How to create invite when bot joins serverbot加入服务器时如何创建邀请
【发布时间】:2022-01-05 20:04:43
【问题描述】:

我正在尝试创建一个不和谐的机器人,我希望它创建一个对它加入的任何服务器的邀请。我需要帮助的只是一行代码。我需要实际创建邀请,但我得到:

Atribute Error: 'Guild' object has no attribute 'channel'

它发生在这一行:

invite = await guild.channel.create_invite(reason=None, max_age=0, max_uses=0, temporary=False, unique=True)

完整版块:

@client.event
async def on_guild_join(guild):
    invite = await guild.channel.create_invite(reason=None, max_age=0, max_uses=0, temporary=False, unique=True)
    
    with open("invites.json", "r") as f:
        invites = json.load(f)

    invites[guild.name] = invite # This line might have an issue but i havent got to test it yet

    with open("invites.json", "w") as f:
        json.dump(invites, f)

我知道如何使用命令创建邀请,但 on_guild_join 上没有 ctx(我不认为)。我只需要在事件参数中的某处添加“ctx”吗?

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您收到的错误中解释了您的代码存在问题。您正在尝试从公会获取频道,尽管它们以另一个名称 text_channels 存储。您可以访问此列表并选择任何一个,然后在特定频道上create_invite。

    你的代码应该是这样的:

    @client.event
    async def on_guild_join(guild):
        invite = await guild.text_channels[0].create_invite(reason=None, max_age=0, max_uses=0, temporary=False, unique=True)
    
        print(invite)
        with open("invites.json", "r") as f:
            invites = json.load(f)
    
        invites[guild.name] = invite
        with open("invites.json", "w") as f:
            json.dump(invites, f)
    

    【讨论】:

      【解决方案2】:

      您需要指定频道才能创建邀请。 例如:

      @client.event
      async def on_guild_join(guild):
          try:
              invite = await guild.text_channels[0].create_invite(reason=None, max_age=0, max_uses=0, temporary=False, unique=True)
          except IndexError:
              pass
      

      这段代码创建了一个到公会中第一个文本频道的邀请。

      【讨论】:

        猜你喜欢
        • 2021-09-13
        • 1970-01-01
        • 2021-07-25
        • 2021-03-21
        • 2020-10-18
        • 1970-01-01
        • 1970-01-01
        • 2018-08-28
        • 1970-01-01
        相关资源
        最近更新 更多