I am trying to make my discord.py bot disconnect 10 seconds after it has joined. So far I can make it join, play the audio file but not yet leave after a set amount of seconds. I have a leave function that leaves, but I want to add it to the existing function that does everything
import discord
from discord.ext import commands
from discord.voice_client import VoiceClient
import youtube_dl
import time
TOKEN = 'mytoken'
intents = discord.Intents.default()
intents.members = True
players = {}
client = commands.Bot(command_prefix='.')
@client.event
async def on_ready():
print('bot online')
@client.command(pass_context=True)
async def leave(ctx):
server = ctx.message.guild.voice_client
await server.disconnect()
@client.event
async def on_voice_state_update(member, before, after):
if not before.channel and after.channel and member.id == 227490621084925953:
channel = client.get_channel(967887213939523584)
await channel.send('KARAN IS HERE')
voiceChannel = client.get_channel(821051726899052624)
vc = await voiceChannel.connect()
vc.play(discord.FFmpegPCMAudio(r"C:\Users\Amarn\OneDrive - Da Vinci College\Da Vinci College\software_developen\Assignments\jaar_1\periode_1\own_projects\discord\punjabi.mp3"), after=lambda e: print('done', e))
time.sleep(10)
leave()
client.run(TOKEN)
>Solution :
You can’t call the command after you’ve decorated it. The closest you can get is using invoke, but that requires creating context.
Instead, just inline it. You can get the guild from the member that the voice state update is about. Instead of doing leave(), which is invalid, you can do:
await member.guild.voice_client.disconnect()