-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice_client.py
67 lines (51 loc) · 2.13 KB
/
voice_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from discord import VoiceClient, VoiceChannel
from discord.utils import get
from lavalink import DefaultPlayer
from lava.bot import Bot
class LavalinkVoiceClient(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
"""
def __init__(self, bot: Bot, channel: VoiceChannel):
self.bot = bot
self.channel = channel
self.lavalink = bot.lavalink
super().__init__(bot, channel)
async def on_voice_server_update(self, data):
lavalink_data = {
't': 'VOICE_SERVER_UPDATE',
'd': data
}
await self.bot.lavalink.voice_update_handler(lavalink_data)
async def on_voice_state_update(self, data):
lavalink_data = {
't': 'VOICE_STATE_UPDATE',
'd': data
}
channel = get(self.channel.guild.voice_channels, id=int(data['channel_id']))
self.channel = channel
await self.lavalink.voice_update_handler(lavalink_data)
if not data['channel_id']:
self.cleanup()
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.
"""
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: DefaultPlayer = self.lavalink.player_manager.get(self.channel.guild.id)
if not force and not player.is_connected:
return
await self.channel.guild.change_voice_state(channel=None)
player.channel_id = None
self.cleanup()