Discord.py帮会邀请机器人

2024-05-23 17:44:47 发布

您现在位置:Python中文网/ 问答频道 /正文

如果代码正确,我想创建一个bot,bot会邀请我访问特定的服务器。 但它有错误

这是我的密码:

@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'

Tags: 代码服务器clientsend密码asyncbot错误
3条回答

您需要一个Channel对象来创建invite,因为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!")

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

根据您从未使用过的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)

ctx.guildNone,这就是为什么会出现此异常

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

相关问题 更多 >