'Why doesn't my bot doesn't accept my commands?
I'm having issues building my music bot. It's accepting any of my commands. I tried to making the Bot join the voice chat, and subsequently play music, but it doesn't work for some reason.
Here's my code:
import discord
from discord.ext import commands
import os
from dotenv import load_dotenv
bot = commands.Bot(command_prefix='.', description="L's very own Jukebox", intents=discord.Intents.all())
@bot.event
async def on_ready():
print(f"Logged in as {bot.user} ({bot.user.id})")
print("-----")
async def on_load():
print(f"Starting to load cogs...")
for cog in os.listdir("cog"):
if cog.endswith(".py"):
try:
await bot.load_extension(f"cog.{cog.strip('py')}")
print("{cog} cog has been loaded")
except Exception as e:
print(e)
print("{cog} cog can't be loaded.")
load_dotenv('development.env')
on_ready()
on_load()
TOKEN = os.getenv('TOKEN')
bot.run(TOKEN)
Here's also the code for the cog I'm using. It's under the 'cog' folder:
import youtube_dl
from discord.ext import commands
import YTDLSource
youtube_dl.utils.bug_reports_message = lambda: ''
class Music(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def ping(self, ctx):
await ctx.send('Ping!')
@commands.command()
async def join(self, ctx):
if ctx.author.voice is None:
await ctx.send("You're currently 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):
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=self.bot.loop)
ctx.voice_client.play(player, after=lambda e: ctx.send(f'Player error: {e}') if e else None)
await ctx.send(f'Now playing {player.title}')
@commands.command()
async def stream(self, ctx, *, url):
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True)
ctx.voice_client.play(player, after=lambda e: print(f'Player error: {e}') if e else None)
await ctx.send(f'Now playing: {player.title}')
@commands.command()
async def volume(self, ctx, volume: int):
if ctx.voice_client is None:
return await ctx.send("Not connected to a voice channel.")
ctx.voice_client.source.volume = volume / 100
await ctx.send(f"Changed volume to {volume}%")
@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("Resuming ▶")
@commands.command()
async def stop(self, ctx):
await ctx.voice_client.disconnect()
@play.before_invoke
@stream.before_invoke
async def ensure_voice(self, ctx):
if ctx.voice_client is None:
if ctx.author.voice:
await ctx.author.voice.channel.connect()
else:
await ctx.send("You aren't connected to a voice channel!")
raise commands.CommandError("Author is not connected to a voice channel.")
elif ctx.voice_client.is_playing():
ctx.voice_client.stop()
def setup(client):
client.add_cog(Music(client))
Anything that can help me fix my bot would help. Thanks in advance.
Solution 1:[1]
Thanks to you guys, you helped me find the issue. As it turns out, I don't need await in on_ready() method. I also fixed the on_ready() method overall.
There was also a typo in the code. I wrote py, instead of .py for the bot.load_extension(f"cog.{cog.strip('.py')}").
So the code for the method looks like this:
@bot.event
async def on_ready():
print(f"Logged in as {bot.user} ({bot.user.id})")
print("-----")
print(f"Starting to load cogs...")
for cog in os.listdir("cog"):
if cog.endswith(".py"):
try:
bot.load_extension(f"cog.{cog.strip('.py')}")
print("{cog} cog has been loaded")
except Exception as e:
print(e)
print("{cog} cog can't be loaded.")
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 | TheSuperBigL |
