Skip to content

Commit

Permalink
Update VoiceProtocol implementation to work with d.py 2.0 (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackenmen authored Sep 17, 2022
1 parent d427a8e commit 8d7548d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
6 changes: 3 additions & 3 deletions lavalink/lavalink.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async def initialize(
return lavalink_node


async def connect(channel: discord.VoiceChannel, deafen: bool = False):
async def connect(channel: discord.VoiceChannel, *, self_deaf: bool = False):
"""
Connects to a discord voice channel.
Expand All @@ -102,7 +102,7 @@ async def connect(channel: discord.VoiceChannel, deafen: bool = False):
----------
channel : discord.VoiceChannel
The channel to connect to.
deafen: bool
self_deaf: bool
Whether to deafen the bot user upon join.
Returns
Expand All @@ -116,7 +116,7 @@ async def connect(channel: discord.VoiceChannel, deafen: bool = False):
If there are no available lavalink nodes ready to connect to discord.
"""
node_ = node.get_node(channel.guild.id)
p = await node_.create_player(channel, deafen=deafen)
p = await node_.create_player(channel, self_deaf=self_deaf)
return p


Expand Down
10 changes: 4 additions & 6 deletions lavalink/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def register_state_handler(self, func):
def unregister_state_handler(self, func):
self._state_handlers.remove(func)

async def create_player(self, channel: VoiceChannel, *, deafen: bool = False) -> Player:
async def create_player(self, channel: VoiceChannel, *, self_deaf: bool = False) -> Player:
"""
Connects to a discord voice channel.
Expand All @@ -515,7 +515,7 @@ async def create_player(self, channel: VoiceChannel, *, deafen: bool = False) ->
Parameters
----------
channel: VoiceChannel
deafen: bool
self_deaf: bool
Returns
-------
Expand All @@ -524,11 +524,9 @@ async def create_player(self, channel: VoiceChannel, *, deafen: bool = False) ->
"""
if self._already_in_guild(channel):
player = self.get_player(channel.guild.id)
await player.move_to(channel, deafen=deafen)
await player.move_to(channel, self_deaf=self_deaf)
else:
player: Player = await channel.connect(cls=Player) # type: ignore
if deafen:
await player.guild.change_voice_state(channel=player.channel, self_deaf=True)
player: Player = await channel.connect(cls=Player, self_deaf=self_deaf) # type: ignore
return player

def _already_in_guild(self, channel: VoiceChannel) -> bool:
Expand Down
19 changes: 12 additions & 7 deletions lavalink/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ def connected(self) -> bool:
"""
return self._connected

async def on_voice_server_update(self, data: dict) -> None:
async def on_voice_server_update(self, data: dict, /) -> None:
self._pending_server_update = data
await self._send_lavalink_voice_update()

async def on_voice_state_update(self, data: dict) -> None:
async def on_voice_state_update(self, data: dict, /) -> None:
self._session_id = data["session_id"]
if (channel_id := data["channel_id"]) is None:
ws_rll_log.info("Received voice disconnect from discord, removing player.")
Expand Down Expand Up @@ -181,7 +181,12 @@ async def wait_until_ready(
raise

async def connect(
self, *, timeout: float = 2.0, reconnect: bool = False, deafen: bool = False
self,
*,
timeout: float = 2.0,
reconnect: bool = False,
self_mute: bool = False,
self_deaf: bool = False,
) -> None:
"""
Connects to the voice channel associated with this Player.
Expand All @@ -192,24 +197,24 @@ async def connect(
self.node._players_dict[self.guild.id] = self
await self.node.refresh_player_state(self)
await self.guild.change_voice_state(
channel=self.channel, self_mute=False, self_deaf=deafen
channel=self.channel, self_mute=self_mute, self_deaf=self_deaf
)

async def move_to(self, channel: discord.VoiceChannel, *, deafen: bool = False) -> None:
async def move_to(self, channel: discord.VoiceChannel, *, self_deaf: bool = False) -> None:
"""
Moves this player to a voice channel.
Parameters
----------
channel : discord.VoiceChannel
deafen : bool
self_deaf : bool
"""
if channel.guild != self.guild:
raise TypeError(f"Cannot move {self!r} to a different guild.")
if self.channel:
self._last_channel_id = self.channel.id
self.channel = channel
await self.connect(deafen=deafen)
await self.connect(self_deaf=self_deaf)
if self.current:
await self.resume(
track=self.current, replace=True, start=self.position, pause=self._paused
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ classifiers = [
requires-python = ">=3.8.1"
dependencies = [
"aiohttp>=3.6.0",
"discord.py>=1.5.1",
"discord.py>=2.0.0",
"Red-Commons>=1.0.0,<2",
]
dynamic = ["version"]
Expand Down

0 comments on commit 8d7548d

Please sign in to comment.