0

I'm trying to make a bot that can join on command to play the 'Open the noor' sound as a joke for a friend. But every time I launch it and run the command, it connects but just sits there, it doesnt even disconnect like it does from the dedicated leave command. I'm scratching my head as to what I'm doing wrong here.

The logs don't have anything out of the ordinary, aside from it not printing the log message "Noored". The bot not leaving aa well tells me it gets hung up on the audio portion.

The command in question is the noor command.

ffmpeg is installed and in the system PATH.

import discord
from discord.ext import commands
import logging

handler = logging.FileHandler(filename='abyssal_log.log', encoding='utf-8', mode='w')

intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(command_prefix='/', intents=intents)
token = #Omitted

@bot.event
async def on_ready():
    print('Ready')

@bot.command()
async def hello(ctx):
    await ctx.send("Hello!")

@bot.command()
async def jabberjoin(ctx):
    await ctx.author.voice.channel.connect()
    print('Bot joined')

@bot.command()
async def jabberleave(ctx):
    await ctx.voice_client.disconnect()
    print('Bot left')

@bot.command()
async def noor(ctx):
    vc = await ctx.author.voice.channel.connect()
    await vc.play(discord.FFmpegPCMAudio('noor.mp3'))
    await ctx.voice_client.disconnect()
    print('Noored')

bot.run(token, log_handler=handler)
1
  • 1
    here you have link to staging-ground and you could add your solution as answer below. Commented May 23 at 13:39

1 Answer 1

1

Remove await from the vc.play() line. Because vc.play() executes instantly in the background, you must use an asynchronous asyncio.sleep() delay to let the audio finish playing before the bot disconnects.

New noor code:

import asyncio  # Add this import at the top of your file

@bot.command()
async def noor(ctx):
    vc = await ctx.author.voice.channel.connect()
    
    # Removed the await from vc.play
    vc.play(discord.FFmpegPCMAudio('noor.mp3')) 
    
    # Wait for the duration of your audio file (e.g., 3 seconds)
    await asyncio.sleep(3) 
    
    await ctx.voice_client.disconnect()
    print('Noored')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.