how to make a music bot with lavalink python code example
Example: how to make a queue command for lavalink discord py
from discord.ext import commands
import lavalink
from discord import utils
from discord import Embed
import re
import math
import discord
url_rx = re.compile(r'https?://(?:www\.)?.+')
class MusicCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.bot.music = lavalink.Client(self.bot.user.id)
self.bot.music.add_node('localhost', 6666, 'Humpday!', 'na', 'music-node')
self.bot.add_listener(self.bot.music.voice_update_handler, 'on_socket_response')
self.bot.music.add_event_hook(self.track_hook)
async def cog_before_invoke(self, ctx):
guild_check = ctx.guild is not None
@commands.command(name='queue')
async def queue(self, ctx, page: int = 1):
player = self.bot.music.player_manager.get(ctx.guild.id)
items_per_page = 10
pages = math.ceil(len(player.queue) / items_per_page)
start = (page - 1) * items_per_page
end = start + items_per_page
queue_list = ''
for index, track in enumerate(player.queue[start:end], start=start):
queue_list += f'`{index + 1}.` [**{track.title}**]({track.uri})\n'
embed = discord.Embed(colour=discord.Color.blurple(),
description=f'**{len(player.queue)} tracks**\n\n{queue_list}')
embed.set_footer(text=f'Viewing page {page}/{pages}')
await ctx.send(embed=embed)
def setup(bot):
bot.add_cog(MusicCog(bot))