I’ve created a Discord bot for my server that should join voice calls and queue up songs, but it doesn’t play any audio. Although the bot joins the channel and loads songs into the queue, no sound is output and there are no console errors.
Here’s an updated version of my code:
import discord
from discord.ext import commands
from youtube_dl import YoutubeDL
class MusicBot(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.now_playing = False
self.song_list = []
self.voice_client = None
async def play_song(self, ctx):
if self.song_list:
self.now_playing = True
song_url = self.song_list[0][0]['url']
self.voice_client.play(discord.FFmpegPCMAudio(song_url), after=lambda e: self.play_next())
self.song_list.pop(0)
else:
self.now_playing = False
@commands.command()
async def add(self, ctx, *, query):
voice_channel = ctx.author.voice.channel
if voice_channel:
song = self.find_song(query)
if song:
self.song_list.append([song, voice_channel])
await ctx.send('Song added to queue')
if not self.now_playing:
await self.play_song(ctx)
else:
await ctx.send('Song not found')
else:
await ctx.send('Join a voice channel first')
# Other commands like pause, resume, and skip would be here
I’m new to Discord bots, so any suggestions to help me resolve this issue would be greatly appreciated.