【问题标题】:Discord.py guild invite botDiscord.py 公会邀请机器人
【发布时间】:2021-09-05 21:23:43
【问题描述】:

如果代码正确,我想制作一个机器人,让机器人邀请我访问特定服务器。 但它有错误。

这是我的代码:

@client.command()
async def invite(ctx, *, code):
    if code == "12320001":
        guild = "851841115896152084"
        invite = await ctx.guild.create_invite(max_uses=1)
        await ctx.send(f"Here's your invite : {invite}")
    else:
        await ctx.send(f"Code is wrong!")

和错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'create_invite'

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    您需要一个Channel 对象来创建邀请,因为Guild 类没有create_invite() 方法。您可以使用下面给出的代码。请注意,服务器应至少有一个频道。

    @client.command()
    async def invite(ctx, code):
        if code == "12320001":
            guild = client.get_guild(851841115896152084)
            invite = await guild.channels[0].create_invite(max_uses = 1)
            await ctx.send(f"Here's your invite: {invite}")
        else:
            await ctx.send("Invalid code!")
    

    【讨论】:

    • @Tamong,如果有帮助,请将此答案标记为解决方案。
    【解决方案2】:

    正如错误提示的那样,ctx.guildNone。这通常发生在在 DM 中而不是在服务器中调用命令时,因为 DM 中显然没有服务器。

    从您从未使用过的 guild 变量这一事实来看,我假设您正试图邀请人们加入 那个 公会。

    # Make sure the id is an int, NOT a string
    guild = client.get_guild(85184111589615208)
    
    # Invite users to the guild with the above id instead
    invite = await guild.create_invite(max_uses=1)
    

    【讨论】:

      【解决方案3】:

      ctx.guildNone,这就是你得到这个异常的原因。

      在调用invite 函数时,您可以在传递ctx 作为参数之前检查ctx.guild 的值。

      【讨论】:

        猜你喜欢
        • 2021-05-12
        • 2020-12-02
        • 2021-01-29
        • 2020-09-24
        • 2020-11-23
        • 2021-02-27
        • 2019-08-28
        • 2020-10-18
        • 2019-11-14
        相关资源
        最近更新 更多