Tamong
Tamong

Reputation: 9

Discord.py guild invite bot

If the code is correct, I want to make a bot where the bot invites me to a specific server. but it has error.

Here's my code:

@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!")

and error:

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

Upvotes: 0

Views: 828

Answers (3)

Sujit
Sujit

Reputation: 1792

You need a Channel object to create an invite as the Guild class doesn't have a create_invite() method. You can the code given below. Note that the server should have at least one channel.

@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!")

Upvotes: 0

stijndcl
stijndcl

Reputation: 5650

As the error suggests, ctx.guild is None. This usually occurs when invoking a command in a DM rather than in a server, as there obviously is no server in DMs.

Judging by the fact that you have a guild variable that you never used I assume that you're trying to invite people to that guild instead.

# 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)

Upvotes: 0

Nitanshu
Nitanshu

Reputation: 846

ctx.guild is None, that is why you are getting this Exception.

You can check the value of ctx.guild before passing ctx as a parameter while calling invite function.

Upvotes: 1

Related Questions