devoxin / Lavalink.py

A wrapper for Lavalink in Python, which aims to be powerful and intuitive.
https://lavalink.readthedocs.io/
MIT License
224 stars 95 forks source link

update the expmaels #146

Closed japlic closed 4 months ago

japlic commented 4 months ago

pls update the expamels for the music cog

devoxin commented 4 months ago

What needs updating?

japlic commented 4 months ago

What needs updating?

the https://github.com/devoxin/Lavalink.py/blob/master/examples/music.py

devoxin commented 4 months ago

Yes, what bit specifically? As far as I'm aware the example works.

japlic commented 4 months ago

Yes, what bit specifically? As far as I'm aware the example works.

Code ```py """ This example cog demonstrates basic usage of Lavalink.py, using the DefaultPlayer. As this example primarily showcases usage in conjunction with discord.py, you will need to make modifications as necessary for use with another Discord library. Usage of this cog requires Python 3.6 or higher due to the use of f-strings. Compatibility with Python 3.5 should be possible if f-strings are removed. """ import re import discord import lavalink from discord.ext import commands from lavalink.events import TrackStartEvent, QueueEndEvent from lavalink.errors import ClientError from lavalink.filters import LowPass from lavalink.server import LoadType url_rx = re.compile(r'https?://(?:www\.)?.+') class LavalinkVoiceClient(discord.VoiceProtocol): """ This is the preferred way to handle external voice sending This client will be created via a cls in the connect method of the channel see the following documentation: https://discordpy.readthedocs.io/en/latest/api.html#voiceprotocol """ def __init__(self, client: discord.Client, channel: discord.abc.Connectable): self.client = client self.channel = channel self.guild_id = channel.guild.id self._destroyed = False if not hasattr(self.client, 'lavalink'): # Instantiate a client if one doesn't exist. # We store it in `self.client` so that it may persist across cog reloads, # however this is not mandatory. self.client.lavalink = lavalink.Client(client.user.id) self.client.lavalink.add_node(host='localhost', port=2333, password='youshallnotpass', region='us', name='default-node') # Create a shortcut to the Lavalink client here. self.lavalink = self.client.lavalink async def on_voice_server_update(self, data): # the data needs to be transformed before being handed down to # voice_update_handler lavalink_data = { 't': 'VOICE_SERVER_UPDATE', 'd': data } await self.lavalink.voice_update_handler(lavalink_data) async def on_voice_state_update(self, data): channel_id = data['channel_id'] if not channel_id: await self._destroy() return self.channel = self.client.get_channel(int(channel_id)) # the data needs to be transformed before being handed down to # voice_update_handler lavalink_data = { 't': 'VOICE_STATE_UPDATE', 'd': data } await self.lavalink.voice_update_handler(lavalink_data) async def connect(self, *, timeout: float, reconnect: bool, self_deaf: bool = False, self_mute: bool = False) -> None: """ Connect the bot to the voice channel and create a player_manager if it doesn't exist yet. """ # ensure there is a player_manager when creating a new voice_client self.lavalink.player_manager.create(guild_id=self.channel.guild.id) await self.channel.guild.change_voice_state(channel=self.channel, self_mute=self_mute, self_deaf=self_deaf) async def disconnect(self, *, force: bool = False) -> None: """ Handles the disconnect. Cleans up running player and leaves the voice client. """ player = self.lavalink.player_manager.get(self.channel.guild.id) # no need to disconnect if we are not connected if not force and not player.is_connected: return # None means disconnect await self.channel.guild.change_voice_state(channel=None) # update the channel_id of the player to None # this must be done because the on_voice_state_update that would set channel_id # to None doesn't get dispatched after the disconnect player.channel_id = None await self._destroy() async def _destroy(self): self.cleanup() if self._destroyed: # Idempotency handling, if `disconnect()` is called, the changed voice state # could cause this to run a second time. return self._destroyed = True try: await self.lavalink.player_manager.destroy(self.guild_id) except ClientError: pass class Music(commands.Cog): def __init__(self, bot): self.bot = bot if not hasattr(bot, 'lavalink'): bot.lavalink = lavalink.Client(bot.user.id) bot.lavalink.add_node(host='localhost', port=2333, password='youshallnotpass', region='us', name='default-node') self.lavalink: lavalink.Client = bot.lavalink self.lavalink.add_event_hooks(self) def cog_unload(self): """ This will remove any registered event hooks when the cog is unloaded. They will subsequently be registered again once the cog is loaded. This effectively allows for event handlers to be updated when the cog is reloaded. """ self.lavalink._event_hooks.clear() async def cog_command_error(self, ctx, error): if isinstance(error, commands.CommandInvokeError): await ctx.send(error.original) # The above handles errors thrown in this cog and shows them to the user. # This shouldn't be a problem as the only errors thrown in this cog are from `ensure_voice` # which contain a reason string, such as "Join a voicechannel" etc. You can modify the above # if you want to do things differently. async def create_player(ctx: commands.Context): """ A check that is invoked before any commands marked with `@commands.check(create_player)` can run. This function will try to create a player for the guild associated with this Context, or raise an error which will be relayed to the user if one cannot be created. """ if ctx.guild is None: raise commands.NoPrivateMessage() player = ctx.bot.lavalink.player_manager.create(ctx.guild.id) # Create returns a player if one exists, otherwise creates. # This line is important because it ensures that a player always exists for a guild. # Most people might consider this a waste of resources for guilds that aren't playing, but this is # the easiest and simplest way of ensuring players are created. # These are commands that require the bot to join a voicechannel (i.e. initiating playback). # Commands such as volume/skip etc don't require the bot to be in a voicechannel so don't need listing here. should_connect = ctx.command.name in ('play',) voice_client = ctx.voice_client if not ctx.author.voice or not ctx.author.voice.channel: # Check if we're in a voice channel. If we are, tell the user to join our voice channel. if voice_client is not None: raise commands.CommandInvokeError('You need to join my voice channel first.') # Otherwise, tell them to join any voice channel to begin playing music. raise commands.CommandInvokeError('Join a voicechannel first.') voice_channel = ctx.author.voice.channel if voice_client is None: if not should_connect: raise commands.CommandInvokeError("I'm not playing music.") permissions = voice_channel.permissions_for(ctx.me) if not permissions.connect or not permissions.speak: raise commands.CommandInvokeError('I need the `CONNECT` and `SPEAK` permissions.') if voice_channel.user_limit > 0: # A limit of 0 means no limit. Anything higher means that there is a member limit which we need to check. # If it's full, and we don't have "move members" permissions, then we cannot join it. if len(voice_channel.members) >= voice_channel.user_limit and not ctx.me.guild_permissions.move_members: raise commands.CommandInvokeError('Your voice channel is full!') player.store('channel', ctx.channel.id) await ctx.author.voice.channel.connect(cls=LavalinkVoiceClient) elif voice_client.channel.id != voice_channel.id: raise commands.CommandInvokeError('You need to be in my voicechannel.') return True @lavalink.listener(TrackStartEvent) async def on_track_start(self, event: TrackStartEvent): guild_id = event.player.guild_id channel_id = event.player.fetch('channel') guild = self.bot.get_guild(guild_id) if not guild: return await self.lavalink.player_manager.destroy(guild_id) channel = guild.get_channel(channel_id) if channel: await channel.send('Now playing: {} by {}'.format(event.track.title, event.track.author)) @lavalink.listener(QueueEndEvent) async def on_queue_end(self, event: QueueEndEvent): guild_id = event.player.guild_id guild = self.bot.get_guild(guild_id) if guild is not None: await guild.voice_client.disconnect(force=True) ```
devoxin commented 4 months ago

This does not narrow down what bit you're having issues with. The provided portion of the cog is functional and I don't believe there's anything to be updated there. You will need to be more specific and state the exact issue you're having.