Discord.py create an invite link to join a voice channel

183 views Asked by At

I want my discord bot to create an invite link to join the voice channel that the person who called the command is in.

@client.command()
async def voiceinvite(ctx):
    if ctx.author.voice:
        await ctx.send(ctx.message.author.voice.create_invite(max_age=120, max_uses=10))
    else:
        await ctx.send("Join a voice channel")

I've tried this but there's an error about references.

1

There are 1 answers

0
DawoleQ On

First, you should include the error message you got so we can figure out what is wrong

But as for the question:

The problem here is that you're trying to call create_invite() on a VoiceState object. You need to call it on a VoiceChannel object as documented in the docs.

The solution would be:

@client.command()
async def voiceinvite(ctx):
    channel = ctx.author.voice.channel
    if channel:
        invite = channel.create_invite(max_age=120, max_uses=10)
        await ctx.send(invite)
    else:
        await ctx.send("Join a voice channel")

API Reference

Next time, Please include the error when there is one. It helps people answer questions as errors have information about the problem and sometimes even the solution itself!