mirror of
https://github.com/UiO-Gaming/ShiteMusicBot.git
synced 2026-11-05 13:56:51 +01:00
Minor cleanup of music decorators
This commit is contained in:
1 parent
8b35088d04
commit
506f2f8694
1 file changed
+4
-49
@@ -7,69 +7,29 @@ import functools
|
||||
import inspect
|
||||
import math
|
||||
|
||||
|
||||
from ...utils.checks import is_dj
|
||||
from .music_errors import WrongVoiceChannelError
|
||||
|
||||
|
||||
class LavalinkVoiceClient(discord.VoiceClient):
|
||||
"""
|
||||
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
|
||||
"""
|
||||
|
||||
class BasicVoiceClient(discord.VoiceClient):
|
||||
def __init__(self, client: discord.Client, channel: discord.abc.Connectable):
|
||||
self.client = client
|
||||
self.channel = channel
|
||||
# ensure a client already exists
|
||||
if hasattr(self.client, 'lavalink'):
|
||||
self.lavalink = self.client.lavalink
|
||||
else:
|
||||
raise Exception("client did not have defined lavalink before connect")
|
||||
self.client.lavalink = lavalink.Client(client.user.id)
|
||||
self.client.lavalink.add_node(
|
||||
'localhost',
|
||||
2334,
|
||||
'butiactuallydid',
|
||||
'us',
|
||||
'default-node'
|
||||
)
|
||||
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)
|
||||
await self.lavalink.voice_update_handler({'t': 'VOICE_SERVER_UPDATE', 'd': data})
|
||||
|
||||
async def on_voice_state_update(self, data):
|
||||
# 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)
|
||||
await self.lavalink.voice_update_handler({'t': 'VOICE_STATE_UPDATE', 'd': 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
|
||||
@@ -78,10 +38,6 @@ class LavalinkVoiceClient(discord.VoiceClient):
|
||||
|
||||
# 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
|
||||
self.cleanup()
|
||||
|
||||
@@ -120,8 +76,7 @@ def require_voice_connection(should_connect=False):
|
||||
'You need to be in the right voice channel', channels=voice_channels)
|
||||
|
||||
player.store('channel', ctx.channel.id)
|
||||
#await self.connect_to(ctx.guild.id, str(ctx.author.voice.channel.id))
|
||||
await ctx.author.voice.channel.connect(cls=LavalinkVoiceClient)
|
||||
await ctx.author.voice.channel.connect(cls=BasicVoiceClient)
|
||||
|
||||
|
||||
elif int(player.channel_id) != ctx.author.voice.channel.id:
|
||||
|
||||
Reference in new issue
Block a user