Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(model): Add guild field to interaction. #2383

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>,
}

Comment on lines +487 to +495
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably those fields are guaranteed to exist for interactions, but it's not documented and I'm not 100% sure about that. ig option is fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this partial guild is strange so I will rather go the safe route. For example the guild id is sent in the interaction so maybe they will stop duplication it here.

#[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