Skip to content

Commit

Permalink
feat(model): Add guild field to interaction. (#2383)
Browse files Browse the repository at this point in the history
Note that this adds a new partial guild since there is a issue with
the name of the fields of this specific partial guild.

Closes #2381
  • Loading branch information
Erk- authored Nov 18, 2024
1 parent f773e51 commit a68d245
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions twilight-cache-inmemory/src/event/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ mod tests {
target_id: None,
}))),
entitlements: Vec::new(),
guild: None,
guild_id: Some(Id::new(3)),
guild_locale: None,
id: Id::new(4),
Expand Down
32 changes: 31 additions & 1 deletion twilight-model/src/application/interaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use self::{
};
use crate::{
channel::{Channel, Message},
guild::{PartialMember, Permissions},
guild::{GuildFeature, PartialMember, Permissions},
id::{
marker::{ApplicationMarker, ChannelMarker, GuildMarker, InteractionMarker, UserMarker},
AnonymizableId, Id,
Expand Down Expand Up @@ -93,6 +93,9 @@ pub struct Interaction {
pub data: Option<InteractionData>,
/// For monetized apps, any entitlements for the invoking user, representing access to premium SKUs
pub entitlements: Vec<Entitlement>,
/// Guild that the interaction was sent from.
#[serde(skip_serializing_if = "Option::is_none")]
pub guild: Option<InteractionPartialGuild>,
/// ID of the guild the interaction was invoked in.
#[serde(skip_serializing_if = "Option::is_none")]
pub guild_id: Option<Id<GuildMarker>>,
Expand Down Expand Up @@ -193,6 +196,7 @@ enum InteractionField {
ChannelId,
Data,
Entitlements,
Guild,
GuildId,
GuildLocale,
Id,
Expand Down Expand Up @@ -224,6 +228,7 @@ impl<'de> Visitor<'de> for InteractionVisitor {
let mut context: Option<InteractionContextType> = None;
let mut data: Option<Value> = None;
let mut entitlements: Option<Vec<Entitlement>> = None;
let mut guild: Option<InteractionPartialGuild> = None;
let mut guild_id: Option<Id<GuildMarker>> = None;
let mut guild_locale: Option<String> = None;
let mut id: Option<Id<InteractionMarker>> = None;
Expand Down Expand Up @@ -298,6 +303,13 @@ impl<'de> Visitor<'de> for InteractionVisitor {

entitlements = map.next_value()?;
}
InteractionField::Guild => {
if guild.is_some() {
return Err(DeError::duplicate_field("guild"));
}

guild = map.next_value()?;
}
InteractionField::GuildId => {
if guild_id.is_some() {
return Err(DeError::duplicate_field("guild_id"));
Expand Down Expand Up @@ -430,6 +442,7 @@ impl<'de> Visitor<'de> for InteractionVisitor {
context,
data,
entitlements,
guild,
guild_id,
guild_locale,
id,
Expand Down Expand Up @@ -464,6 +477,22 @@ pub enum InteractionData {
ModalSubmit(ModalInteractionData),
}

/// Partial guild containing only the fields sent in the partial guild
/// in interactions.
///
/// # Note that the field `locale` does not exists on the full guild
/// object, and is only found here. See
/// <https://github.com/discord/discord-api-docs/issues/6938> for more
/// info.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize, Hash)]
pub struct InteractionPartialGuild {
/// Id of the guild.
pub id: Option<Id<GuildMarker>>,
/// Enabled guild features
pub features: Option<Vec<GuildFeature>>,
pub locale: Option<String>,
}

#[cfg(test)]
mod tests {
use super::{
Expand Down Expand Up @@ -606,6 +635,7 @@ mod tests {
starts_at: None,
user_id: None,
}],
guild: None,
guild_id: Some(Id::new(400)),
guild_locale: Some("de".to_owned()),
id: Id::new(500),
Expand Down
1 change: 1 addition & 0 deletions twilight-standby/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,7 @@ mod tests {
},
))),
entitlements: Vec::new(),
guild: None,
guild_id: Some(Id::new(3)),
guild_locale: None,
id: Id::new(4),
Expand Down

0 comments on commit a68d245

Please sign in to comment.