How do I make a discord bot that will automatically join a Discord voice call when it goes online?

Viewed 3400

I've been trying to figure this out. I don't want to have to use commands to get the bot to join a voice call. I just want it to do it by itself as soon as it goes online. Could someone help me with that? Just so you know I'm using python.

Here are some things I've tried:

@client.command()
async def join(ctx):
   channel = ctx.author.voice.channel
   await channel.connect()

@client.command()
   async def leave(ctx):
await ctx.voice_client.disconnect()

This is a good method but it requires user input and for the user to already be in a voice call. I want the bot to just automatically join a voice call as soon as it goes online. No user input required.

2 Answers

There's a function called on_ready() that you can use. Inside the function you can get the voice channel desired and tell the bot to join it.

@client.event
async def on_ready():
    channel = discord.utils.get(bot.get_all_channels(), id=<the channel id>)
    await channel.connect()

Pointed out by comments, this version also works

@client.event
async def on_ready():
    channel = bot.get_channel(<the channel id>)
    await channel.connect()

Note I have not tested this, it just should work.

In the on_ready() function, write:

@client.event
async def on_ready():
   channel = client.get_channel(<id>)
   await channel.connect()
Related