Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

Commit

Permalink
feat: add additional voice events (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
LordOfPolls authored Oct 17, 2022
1 parent d0acc68 commit f1adf18
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 9 deletions.
6 changes: 6 additions & 0 deletions naff/api/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"BanCreate",
"BanRemove",
"BaseEvent",
"BaseVoiceEvent",
"ButtonPressed",
"ChannelCreate",
"ChannelDelete",
Expand Down Expand Up @@ -77,6 +78,11 @@
"ThreadUpdate",
"TypingStart",
"VoiceStateUpdate",
"VoiceUserDeafen",
"VoiceUserJoin",
"VoiceUserLeave",
"VoiceUserMove",
"VoiceUserMute",
"WebhooksUpdate",
"WebsocketReady",
)
86 changes: 77 additions & 9 deletions naff/api/events/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ def on_guild_join(event):
from naff.client.utils.attr_utils import define, field, docs

__all__ = (
"BanCreate",
"BanRemove",
"AutoModExec",
"ApplicationCommandPermissionsUpdate",
"AutoModCreated",
"AutoModUpdated",
"AutoModDeleted",
"ApplicationCommandPermissionsUpdate",
"AutoModExec",
"AutoModUpdated",
"BanCreate",
"BanRemove",
"BaseVoiceEvent",
"ChannelCreate",
"ChannelDelete",
"ChannelPinsUpdate",
Expand Down Expand Up @@ -63,6 +64,7 @@ def on_guild_join(event):
"MessageReactionRemove",
"MessageReactionRemoveAll",
"MessageUpdate",
"NewThreadCreate",
"PresenceUpdate",
"RoleCreate",
"RoleDelete",
Expand All @@ -71,21 +73,25 @@ def on_guild_join(event):
"StageInstanceDelete",
"StageInstanceUpdate",
"ThreadCreate",
"NewThreadCreate",
"ThreadDelete",
"ThreadListSync",
"ThreadMemberUpdate",
"ThreadMembersUpdate",
"ThreadMemberUpdate",
"ThreadUpdate",
"TypingStart",
"VoiceStateUpdate",
"VoiceUserDeafen",
"VoiceUserJoin",
"VoiceUserLeave",
"VoiceUserMove",
"VoiceUserMute",
"WebhooksUpdate",
)


if TYPE_CHECKING:
from naff.models.discord.guild import Guild, GuildIntegration
from naff.models.discord.channel import BaseChannel, TYPE_THREAD_CHANNEL
from naff.models.discord.channel import BaseChannel, TYPE_THREAD_CHANNEL, VoiceChannel
from naff.models.discord.message import Message
from naff.models.discord.timestamp import Timestamp
from naff.models.discord.user import Member, User, BaseUser
Expand Down Expand Up @@ -543,9 +549,71 @@ class InteractionCreate(BaseEvent):

@define(kw_only=False)
class VoiceStateUpdate(BaseEvent):
"""Dispatched when a user joins/leaves/moves voice channels."""
"""Dispatched when a user's voice state changes."""

before: Optional["VoiceState"] = field()
"""The voice state before this event was created or None if the user was not in a voice channel"""
after: Optional["VoiceState"] = field()
"""The voice state after this event was created or None if the user is no longer in a voice channel"""


@define(kw_only=False)
class BaseVoiceEvent(BaseEvent):
state: "VoiceState" = field()
"""The current voice state of the user"""


@define(kw_only=False)
class VoiceUserMove(BaseVoiceEvent):
"""Dispatched when a user moves voice channels."""

author: Union["User", "Member"] = field()

previous_channel: "VoiceChannel" = field()
"""The previous voice channel the user was in"""
new_channel: "VoiceChannel" = field()
"""The new voice channel the user is in"""


@define(kw_only=False)
class VoiceUserMute(BaseVoiceEvent):
"""Dispatched when a user is muted or unmuted."""

author: Union["User", "Member"] = field()
"""The user who was muted or unmuted"""
channel: "VoiceChannel" = field()
"""The voice channel the user was muted or unmuted in"""
mute: bool = field()
"""The new mute state of the user"""


@define(kw_only=False)
class VoiceUserDeafen(BaseVoiceEvent):
"""Dispatched when a user is deafened or undeafened."""

author: Union["User", "Member"] = field()
"""The user who was deafened or undeafened"""
channel: "VoiceChannel" = field()
"""The voice channel the user was deafened or undeafened in"""
deaf: bool = field()
"""The new deaf state of the user"""


@define(kw_only=False)
class VoiceUserJoin(BaseVoiceEvent):
"""Dispatched when a user joins a voice channel."""

author: Union["User", "Member"] = field()
"""The user who joined the voice channel"""
channel: "VoiceChannel" = field()
"""The voice channel the user joined"""


@define(kw_only=False)
class VoiceUserLeave(BaseVoiceEvent):
"""Dispatched when a user leaves a voice channel."""

author: Union["User", "Member"] = field()
"""The user who left the voice channel"""
channel: "VoiceChannel" = field()
"""The voice channel the user left"""
12 changes: 12 additions & 0 deletions naff/api/events/processors/voice_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ async def _on_raw_voice_state_update(self, event: "RawGatewayEvent") -> None:
# noinspection PyProtectedMember
await vc._voice_state_update(before, after, event.data)

if before and after:
if (before.mute != after.mute) or (before.self_mute != after.self_mute):
self.dispatch(events.VoiceUserMute(after, after.member, after.channel, after.mute or after.self_mute))
if (before.deaf != after.deaf) or (before.self_deaf != after.self_deaf):
self.dispatch(events.VoiceUserDeafen(after, after.member, after.channel, after.deaf or after.self_deaf))
if before.channel != after.channel:
self.dispatch(events.VoiceUserMove(after, after.member, before.channel, after.channel))
elif not before and after:
self.dispatch(events.VoiceUserJoin(after, after.member, after.channel))
elif before and not after:
self.dispatch(events.VoiceUserLeave(before, before.member, before.channel))

@Processor.define()
async def _on_raw_voice_server_update(self, event: "RawGatewayEvent") -> None:
if vc := self.cache.get_bot_voice_state(event.data["guild_id"]):
Expand Down

0 comments on commit f1adf18

Please sign in to comment.