Skip to content

Commit

Permalink
Implement VOICE_CHANNEL_STATUS_UPDATE (serenity-rs#2565)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbt365 authored and mkrasnitski committed Oct 24, 2023
1 parent 2ed5a31 commit 9d56255
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions command_attr/src/structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ impl Permissions {
"SEND_MESSAGES_IN_THREADS" => 1 << 38,
"USE_EMBEDDED_ACTIVITIES" => 1 << 39,
"MODERATE_MEMBERS" => 1 << 40,
"SET_VOICE_CHANNEL_STATUS" => 1 << 48,
_ => return None,
}))
}
Expand Down
2 changes: 2 additions & 0 deletions src/builder/edit_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub struct EditChannel<'a> {
default_sort_order: Option<SortOrder>,
#[serde(skip_serializing_if = "Option::is_none")]
default_forum_layout: Option<ForumLayoutType>,
#[serde(skip_serializing_if = "Option::is_none")]
status: Option<String>,

#[serde(skip)]
audit_log_reason: Option<&'a str>,
Expand Down
17 changes: 17 additions & 0 deletions src/cache/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::model::event::{
ThreadDeleteEvent,
ThreadUpdateEvent,
UserUpdateEvent,
VoiceChannelStatusUpdateEvent,
VoiceStateUpdateEvent,
};
use crate::model::gateway::ShardInfo;
Expand Down Expand Up @@ -624,3 +625,19 @@ impl CacheUpdate for VoiceStateUpdateEvent {
}
}
}

impl CacheUpdate for VoiceChannelStatusUpdateEvent {
type Output = String;

fn update(&mut self, cache: &Cache) -> Option<Self::Output> {
if let Some(mut channel) = cache.channels.get_mut(&self.id) {
let old = channel.status.clone();
channel.status = self.status.clone();
// Discord updates topic but doesn't fire ChannelUpdate.
channel.topic = self.status.clone();
old
} else {
None
}
}
}
12 changes: 12 additions & 0 deletions src/client/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,18 @@ fn update_cache_with_event(ctx: Context, event: Event) -> Option<(FullEvent, Opt
new: event.voice_state,
}
},
Event::VoiceChannelStatusUpdate(mut event) => {
let old = if_cache!(update_cache(&ctx, &mut event));

FullEvent::VoiceChannelStatusUpdate {
ctx,
old,
status: event.status,
id: event.id,
guild_id: event.guild_id,
}
},

Event::WebhookUpdate(event) => FullEvent::WebhookUpdate {
ctx,
guild_id: event.guild_id,
Expand Down
5 changes: 5 additions & 0 deletions src/client/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ event_handler! {
/// [`GatewayIntents::GUILDS`] is enabled) and the new state of the guild's voice channels.
async fn voice_state_update(&self, VoiceStateUpdate { ctx: Context, old: Option<VoiceState>, new: VoiceState });

/// Dispatched when a voice channel's status is updated.
///
/// Provides the status, channel's id and the guild's id.
async fn voice_channel_status_update(&self, VoiceChannelStatusUpdate { ctx: Context, old: Option<String>, status: Option<String>, id: ChannelId, guild_id: GuildId });

/// Dispatched when a guild's webhook is updated.
///
/// Provides the guild's id and the channel's id the webhook belongs in.
Expand Down
4 changes: 4 additions & 0 deletions src/model/channel/guild_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ pub struct GuildChannel {
///
/// **Note**: This is only available in a forum or text channel.
pub default_thread_rate_limit_per_user: Option<u16>,
/// The status of a voice channel.
///
/// **Note**: This is only available in voice channels.
pub status: Option<String>,
/// The default sort order type used to order posts
///
/// **Note**: This is only available in a forum.
Expand Down
13 changes: 13 additions & 0 deletions src/model/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,17 @@ pub struct VoiceStateUpdateEvent {
pub voice_state: VoiceState,
}

/// Requires [`GatewayIntents::GUILDS`].
///
/// [Incomplete documentation](https://github.com/discord/discord-api-docs/pull/6398)
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct VoiceChannelStatusUpdateEvent {
pub status: Option<String>,
pub id: ChannelId,
pub guild_id: GuildId,
}

/// Requires [`GatewayIntents::GUILD_WEBHOOKS`].
///
/// [Discord docs](https://discord.com/developers/docs/topics/gateway-events#webhooks-update).
Expand Down Expand Up @@ -1149,6 +1160,8 @@ pub enum Event {
VoiceStateUpdate(VoiceStateUpdateEvent),
/// Voice server information is available
VoiceServerUpdate(VoiceServerUpdateEvent),
/// Fired when the status of a Voice Channel changes.
VoiceChannelStatusUpdate(VoiceChannelStatusUpdateEvent),
/// A webhook for a [channel][`GuildChannel`] was updated in a [`Guild`].
WebhookUpdate(WebhookUpdateEvent),
/// An interaction was created.
Expand Down
15 changes: 15 additions & 0 deletions src/model/guild/audit_log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum Action {
ScheduledEvent(ScheduledEventAction),
Thread(ThreadAction),
AutoMod(AutoModAction),
VoiceChannelStatus(VoiceChannelStatusAction),
Unknown(u8),
}

Expand All @@ -57,6 +58,7 @@ impl Action {
Self::ScheduledEvent(x) => x as u8,
Self::Thread(x) => x as u8,
Self::AutoMod(x) => x as u8,
Self::VoiceChannelStatus(x) => x as u8,
Self::Unknown(x) => x,
}
}
Expand All @@ -79,6 +81,7 @@ impl Action {
100..=102 => Action::ScheduledEvent(unsafe { transmute(value) }),
110..=112 => Action::Thread(unsafe { transmute(value) }),
140..=145 => Action::AutoMod(unsafe { transmute(value) }),
192..=193 => Action::VoiceChannelStatus(unsafe { transmute(value) }),
_ => Action::Unknown(value),
}
}
Expand Down Expand Up @@ -248,6 +251,15 @@ pub enum AutoModAction {
UserCommunicationDisabled = 145,
}

/// [Incomplete documentation](https://github.com/discord/discord-api-docs/pull/6398)
#[derive(Copy, Clone, Debug)]
#[non_exhaustive]
#[repr(u8)]
pub enum VoiceChannelStatusAction {
StatusUpdate = 192,
StatusDelete = 193,
}

/// [Discord docs](https://discord.com/developers/docs/resources/audit-log#audit-log-object).
#[derive(Debug, Deserialize, Serialize)]
#[non_exhaustive]
Expand Down Expand Up @@ -346,6 +358,9 @@ pub struct Options {
/// Name of the role if type is "role"
#[serde(default)]
pub role_name: Option<String>,
/// The status of a voice channel when set.
#[serde(default)]
pub status: Option<String>,
}

#[cfg(test)]
Expand Down
3 changes: 3 additions & 0 deletions src/model/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ bitflags::bitflags! {
const USE_EXTERNAL_SOUNDS = 1 << 45;
/// Allows sending voice messages.
const SEND_VOICE_MESSAGES = 1 << 46;
// Allows setting the status of a voice channel.
const SET_VOICE_CHANNEL_STATUS = 1 << 48;

}
}

Expand Down

0 comments on commit 9d56255

Please sign in to comment.