'Discord Music bot (Does not work but no errors)
I was building a discord bot which plays music with 2 diffrent files. It does not work but there are 0 errors. Help is appreciated.
My main.py file
import discord
from discord.ext import commands
import os
import time
import music
from keep_alive import keep_alive
TOKEN = os.environ['TOKEN']
bot = commands.Bot(command_prefix="$", intents = discord.Intents.all())
cogs = [music]
for i in range(len(cogs)):
cogs[i].setup(bot)
keep_alive()
bot.run(TOKEN)
Ignore the keep alive that works perfectly
And then there is the music.py file
import discord
from discord.ext import commands
import youtube_dl
print("Test 1")
class music(commands.Cog):
def __init__(self, client):
self.client = client
print("Test2")
@commands.command()
async def join(self, ctx):
print("E")
if ctx.author.voice is None:
await ctx.send("Youre not in a voice channel")
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
@commands.command()
async def disconnect(self, ctx):
await ctx.voice_client.disconnect()
@commands.command()
async def play(self, ctx, url):
ctx.voice_client.stop()
FFMPEG_OPTIONS = {'before_options':'-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
YDL_OPTIONS = {'format':'bestaudio'}
vc = ctx.voice_client
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url, download = False)
url2 = info['formats'][0]['url']
source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
vc.play(source)
@commands.command()
async def pause(self, ctx):
await ctx.voice_client.pause()
await ctx.send("Paused ⏸")
@commands.command()
async def resume(self, ctx):
await ctx.voice_client.resume()
await ctx.send("Resumed ⏯")
def setup(client):
client.add_cog(music(client))
When i set this up, test 1 and 2 run perfectly but the commands never get executed. I typed $join multiple times but nothing happened and no error popped up. Im running blind here so i would heavily appreciate help.
Solution 1:[1]
try this and reply if working
async def _join(self, ctx: commands.Context, *, channel: discord.VoiceChannel = None):
if not channel and not ctx.author.voice:
return await ctx.send('You are neither connected to a voice channel nor specified a channel to join.')
destination = channel or ctx.author.voice.channel
client = ctx.guild.voice_client
if client:
await client.move_to(destination)
else:
await destination.connect()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | majinvejetho |
