Skip to content

Commit

Permalink
fix deserializing InteractionCreate events
Browse files Browse the repository at this point in the history
  • Loading branch information
rksm committed Jun 3, 2023
1 parent 1ac78cb commit f5a7f56
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions twilight-model/src/application/interaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ pub struct Interaction {
#[serde(skip_serializing_if = "Option::is_none")]
pub app_permissions: Option<Permissions>,
/// ID of the associated application.
pub application_id: Id<ApplicationMarker>,
#[serde(default)]
pub application_id: Option<Id<ApplicationMarker>>,
/// The channel the interaction was invoked in.
///
/// Present on all interactions types, except [`Ping`].
Expand Down Expand Up @@ -87,8 +88,8 @@ pub struct Interaction {
/// ID of the interaction.
pub id: Id<InteractionMarker>,
/// Type of interaction.
#[serde(rename = "type")]
pub kind: InteractionType,
#[serde(rename = "type", default)]
pub kind: Option<InteractionType>,
/// Selected language of the user who invoked the interaction.
///
/// Present on all interactions types, except [`Ping`].
Expand All @@ -109,7 +110,8 @@ pub struct Interaction {
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<Message>,
/// Token for responding to the interaction.
pub token: String,
#[serde(default)]
pub token: Option<String>,
/// User that invoked the interaction.
///
/// Present when the interaction is invoked in a direct message.
Expand Down Expand Up @@ -342,47 +344,45 @@ impl<'de> Visitor<'de> for InteractionVisitor {
}
}

let application_id =
application_id.ok_or_else(|| DeError::missing_field("application_id"))?;
let id = id.ok_or_else(|| DeError::missing_field("id"))?;
let token = token.ok_or_else(|| DeError::missing_field("token"))?;
let kind = kind.ok_or_else(|| DeError::missing_field("kind"))?;
let kind = kind;

tracing::trace!(
%application_id,
?application_id,
%id,
%token,
?token,
?kind,
"common fields of all variants exist"
);

let data = match kind {
InteractionType::Ping => None,
InteractionType::ApplicationCommand => {
None => None,
Some(InteractionType::Ping) => None,
Some(InteractionType::ApplicationCommand) => {
let data = data
.ok_or_else(|| DeError::missing_field("data"))?
.deserialize_into()
.map_err(DeserializerError::into_error)?;

Some(InteractionData::ApplicationCommand(data))
}
InteractionType::MessageComponent => {
Some(InteractionType::MessageComponent) => {
let data = data
.ok_or_else(|| DeError::missing_field("data"))?
.deserialize_into()
.map_err(DeserializerError::into_error)?;

Some(InteractionData::MessageComponent(data))
}
InteractionType::ApplicationCommandAutocomplete => {
Some(InteractionType::ApplicationCommandAutocomplete) => {
let data = data
.ok_or_else(|| DeError::missing_field("data"))?
.deserialize_into()
.map_err(DeserializerError::into_error)?;

Some(InteractionData::ApplicationCommand(data))
}
InteractionType::ModalSubmit => {
Some(InteractionType::ModalSubmit) => {
let data = data
.ok_or_else(|| DeError::missing_field("data"))?
.deserialize_into()
Expand Down Expand Up @@ -461,7 +461,7 @@ mod tests {

let value = Interaction {
app_permissions: Some(Permissions::SEND_MESSAGES),
application_id: Id::new(100),
application_id: Some(Id::new(100)),
channel: Some(Channel {
bitrate: None,
guild_id: None,
Expand Down Expand Up @@ -556,7 +556,7 @@ mod tests {
guild_id: Some(Id::new(400)),
guild_locale: Some("de".to_owned()),
id: Id::new(500),
kind: InteractionType::ApplicationCommand,
kind: Some(InteractionType::ApplicationCommand),
locale: Some("en-GB".to_owned()),
member: Some(PartialMember {
avatar: None,
Expand Down Expand Up @@ -588,7 +588,7 @@ mod tests {
}),
}),
message: None,
token: "interaction token".into(),
token: Some("interaction token".into()),
user: None,
};

Expand Down

0 comments on commit f5a7f56

Please sign in to comment.