None of my commands are running on discord.py and I'm not sure why

Viewed 44

I'm new to discord.py and I'm trying to make a music bot for me and my friends, I've probably made a dumb mistake and sorry if I have, I don't know what is the cause; could you please help. I'm using the hikari, discord and youtube_dl libraries If that helps. Also this is just one command if you want to see more of the code please ask.

import hikari
import discord
from discord.ext import commands

from youtube_dl import YoutubeDL

class music_cog(commands.Cog):
   def __init__(self, bot):
       self.bot = bot

       self.is_playing = False
       self.is_paused = False


       self.music_queue = []
       self.YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist': 'True'}
       self.FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}

       self.vc = None

def search_yt(self, item):
   with YoutubeDl(self.YDL_OPTIONS) as ydl:
       try:
           info = ydl.extract_info('ytsearch:%s' % item, download=false)['entries'][0]
       except Exception:
           return False
   return {'source': info['formats'][0]['url'], 'title': info['title']}
   
   def play_next(self):
       if len(self.music_queue) > 0:
           self.is_playing = True
           
           m_url = self.music_queue[0][0]['source']

           self.music_queue.pop(0)

           self.vc.play(discord.FFmpegPCMAudio(m_url, **self.FFMPEG_OPTIONS), after=lambda e: self_play())
       else:
           self.is_playing = False

   async def play_music(self, ctx):
       if len(self.music_queue) > 0:
           self.is_playing = True
           m_url = self.music_queue[0][0]['source']

           if self.vc == None or not self.vc.is_connected():
               self.vc = await self.musoc_queue[0][1].connect()

               if self.vc == None:
                   await ctx.send('could not connect to the voice channel, sorry Blooketeers')
                   return
           else:
               await self.vc.move_to(self.music_queue[0][1])

           self.music_queue.pop(0)

           self.vc.play(discord.FFmpegPCMAudio(m_url, **self.FFMPEG_OPTIONS), after=lambda e: self.play_next())

       else:
           self.is_playing = False

   @commands.command(name='play', aliases=['p', 'playing'], help='play the selected song chosen by the Blooketeers!')
   async def play(self, ctx, *args):
       query =' '.join(args)

       voice_channel = ctx.author.voice.channel
       if voice_channel is None:
           await ctx.send('connect to a voice channel blooketeers!')
       elif self.is_paused:
           self.vc.resume()
       else:
           song = self.search_yt(query)
           if type(song) == type(True):
               await ctx.send('Sorry Blooketeers, I couldn\'t download the song. Incorrect format, try a different keyword.')
           else:
               await ctx.send('Song added to the queue Blooketeers! Happy listening!')
               self.music_queue.append([song, voice_channel])

               if self.is_playing == False:
                   await self.play_music(ctx)
   @commands.command(name='pause', help='Pauses the current song chosen by the Blooketeers!')
   async def pause(self, ctx, *args):
       if self.is_playing:
           self.is_playing = False
           self.is_paused = True
           self.vc.pause()
       elif self.is_paused:
           self.is_playing = True
           self.is_paused = False
           self.vc.resume()
           

   @commands.command(name='resume', aliases=['r'], help='Resumes the current song chosen by the Blooketeers!')
   async def resume(self, ctx, *args):
       if self.is_paused:
           self.is_playing = True
           self.is_paused = False
           self.vc.resume()

   @commands.command(name='skip', aliases=['s'], help = 'Skips the current song chosen by the blooketeers!')
   async def skip(self, ctx, *args):
       if self.vc != None and self.vc:
           self.vc.stop()
           await self.play_music(ctx)

   @commands.command(name='queue', aliases=['q'], help='Displays all the songs currently in the queue Blooketeers!')
   async def queue(self, ctx):
       retval = ''

       for i in range(0, len(slef.music_queue)):
           if i > 4: break
           retval += self.music_queue[i][0]['title'] + '\n'

       if retval != '':
           await ctx.send(retval)
       else:
           await ctx.send('No music in the queue Blooketeers, go add some!')

       @commands.command(name='clear', aliases=['c', 'bin'], help='Stop the current song and clears the current queue Blooketeers"')
       async def clear(self, ctx, *args):
           if slef.vc != None and self.is_playing:
               self.vc.stop()
           self.music_queue = []
           await ctx.send('All the music is cleared Blooketeers, go add some more!')

   @commands.command(name='leave', aliases=['disconnect', 'l', 'd'], help='Kicks the bot from the voice channel Blooketeers!')
   async def leave(self, ctx):
       self.is_playing = False
       self.is_paused = False
       await self.vc.disconnect()



   
import hikari
import discord
from discord.ext import commands
import os
intents = discord.Intents.default()
intents.typing = False
intents.presences = False

from help_cog import help_cog 
from music_cog import music_cog

intents=discord.Intents().all()

bot = hikari.GatewayBot(token='tokenwashere')

bot = commands.Bot(command_prefix='!', intents=intents)

bot.remove_command('help')

async def setup(bot):
    await bot.add_cog(help_cog(bot))
    await bot.add_cog(music_cog(bot))

TOKEN = os.getenv('DISCORD_TOKEN')

bot.run('tokenwashere')
1 Answers
import hikari
# also import lightbulb, that works with hikari
import lightbulb
import discord
from discord.ext import commands
import os
from help_cog import help_cog 
from music_cog import music_cog

# intents : or you pick default(), or you pick all()
# but you have to modify your bot Gateways intents in discord portal accordingly
# since .all() requires to aknowledge all intents possible. 

intents=discord.Intents.all()

# now you want to define your token here, before passing it
# assuming you have a file ".env" in the same folder as your application

TOKEN = os.getenv('DISCORD_TOKEN')

# then you create your bot. Lightbulb is going deeper than hikari 
#since you can configure the command prefix aswell so.. 
# bot = hikari.GatewayBot(token=TOKEN, intents=intents)
# turns into : 
bot = lightbulb.BotApp(token=TOKEN, intents=intents, prefix="!")
bot.remove_command('help')

async def setup(bot):
    await bot.add_cog(help_cog(bot))
    await bot.add_cog(music_cog(bot))

bot.run()
#token isn't needed here since you use hikari/lightbulb that manages it

In the other file it should go this way :

import hikari
import lightbulb
import discord
from discord.ext import commands

from youtube_dl import YoutubeDL

class music_cog(commands.Cog):
   def __init__(self, bot):
       self.bot = bot

       self.is_playing = False
       self.is_paused = False


       self.music_queue = []
       self.YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist': 'True'}
       self.FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}

       self.vc = None

def search_yt(self, item):
   with YoutubeDl(self.YDL_OPTIONS) as ydl:
       try:
           info = ydl.extract_info('ytsearch:%s' % item, download=false)['entries'][0]
       except Exception:
           return False
   return {'source': info['formats'][0]['url'], 'title': info['title']}
   
   def play_next(self):
       if len(self.music_queue) > 0:
           self.is_playing = True
           
           m_url = self.music_queue[0][0]['source']

           self.music_queue.pop(0)

           self.vc.play(discord.FFmpegPCMAudio(m_url, **self.FFMPEG_OPTIONS), after=lambda e: self_play())
       else:
           self.is_playing = False

   async def play_music(self, ctx):
       if len(self.music_queue) > 0:
           self.is_playing = True
           m_url = self.music_queue[0][0]['source']

           if self.vc == None or not self.vc.is_connected():
               self.vc = await self.musoc_queue[0][1].connect()

               if self.vc == None:
                   await ctx.send('could not connect to the voice channel, sorry Blooketeers')
                   return
           else:
               await self.vc.move_to(self.music_queue[0][1])

           self.music_queue.pop(0)

           self.vc.play(discord.FFmpegPCMAudio(m_url, **self.FFMPEG_OPTIONS), after=lambda e: self.play_next())

       else:
           self.is_playing = False

# here, it's not commands.command but bot.command
# but we also want lightbulb to take over this, so we remove overwrite
@bot.command
@lightbulb.command("play", description="play the selected song chosen by the Blooketeers!")
@lightbulb.implements(lightbulb.PrefixCommand)
   async def play(self, ctx: lightbulb.Context, *args):
       query =' '.join(args)

       voice_channel = ctx.author.voice.channel
       if voice_channel is None:
           await ctx.send('connect to a voice channel blooketeers!')
       elif self.is_paused:
           self.vc.resume()
       else:
           song = self.search_yt(query)
           if type(song) == type(True):
               await ctx.send('Sorry Blooketeers, I couldn\'t download the song. Incorrect format, try a different keyword.')
           else:
               await ctx.send('Song added to the queue Blooketeers! Happy listening!')
               self.music_queue.append([song, voice_channel])

               if self.is_playing == False:
                   await self.play_music(ctx)
# here too 
@bot.command
@lightbulb.command("pause", description="Pauses the current song chosen by the Blooketeers!")
@lightbulb.implements(lightbulb.PrefixCommand)
async def pause(self, ctx: lightbulb.Context, *args):
       if self.is_playing:
           self.is_playing = False
           self.is_paused = True
           self.vc.pause()
       elif self.is_paused:
           self.is_playing = True
           self.is_paused = False
           self.vc.resume()
           
# and here 
@bot.command
@lightbulb.command("resume", description="Resumes the current song chosen by the Blooketeers!")
@lightbulb.implements(lightbulb.PrefixCommand)
async def resume(self, ctx: lightbulb.Context, *args):
       if self.is_paused:
           self.is_playing = True
           self.is_paused = False
           self.vc.resume()

# and here 
@bot.command
@lightbulb.command("skip", description="Skips the current song chosen by the blooketeers!")
@lightbulb.implements(lightbulb.PrefixCommand)
async def skip(self, ctx: lightbulb.Context, *args):
       if self.vc != None and self.vc:
           self.vc.stop()
           await self.play_music(ctx)
# and here 
@bot.command
@lightbulb.command("queue", description="Displays all the songs currently in the queue Blooketeers!")
@lightbulb.implements(lightbulb.PrefixCommand)
async def queue(self, ctx: lightbulb.Context):
       retval = ''

       for i in range(0, len(slef.music_queue)):
           if i > 4: break
           retval += self.music_queue[i][0]['title'] + '\n'

       if retval != '':
           await ctx.send(retval)
       else:
           await ctx.send('No music in the queue Blooketeers, go add some!')

# and here 
@bot.command
@lightbulb.command("clear", description="Stop the current song and clears the current queue Blooketeers")
@lightbulb.implements(lightbulb.PrefixCommand)

async def clear(self, ctx: lightbulb.Context, *args):
           if self.vc != None and self.is_playing:
               self.vc.stop()
           self.music_queue = []
           await ctx.send('All the music is cleared Blooketeers, go add some more!')

# and finally here
@bot.command
@lightbulb.command("leave", description="Kicks the bot from the voice channel Blooketeers!")
@lightbulb.implements(lightbulb.PrefixCommand)
   async def leave(self, ctx: lightbulb.Context):
       self.is_playing = False
       self.is_paused = False
       await self.vc.disconnect()

I am not completely sure it will work, as said in previous comments, I never used hikari/lightbulb, but following their doc, it seems to be the way. I've added comments where I changed/added items.

Related