2
votes

I am using python to make my bot generate an instant invite. However I have trouble finding and creating the instant invite. Here is my code:

from progress.bar import Bar # Only to show a loading bar, all imports are successful.
print("Loading...")
imports = [
    'import os',
    'import sys',
    'import asyncio',
    'import discord',
    'import random',
    'import functools',
    'import time as tm',
    'from discord.ext import commands',
    'from discord.ext.commands import when_mentioned'

]

bar = Bar('', max=len(imports))
for i in range(0, len(imports)):
    exec(str(imports[i]))
    bar.next()
bar.finish()

BOT_PREFIX = "!"
BOT_TOKEN = 'token'
OWNER_ID = int("owner's user id for support")

class Stuff(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def create_invite(self, ctx):
        """- Create instant invite"""
        link = await discord.abc.GuildChannel.create_invite(self, max_age='300')
        await ctx.send("Here is an instant invite to your server: "+link)

When the command is run in discord, I get this error:

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

How can I properly generate an instant invite?

1

1 Answers

2
votes

Try using discord.TextChannel.create_invite()

Like so,

@commands.command()
async def create_invite(self, ctx):
    """Create instant invite"""
    link = await ctx.channel.create_invite(max_age = 300)
    await ctx.send("Here is an instant invite to your server: " + link)