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

Add missing Message fields #2214

Merged
merged 3 commits into from
Oct 12, 2022
Merged
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
9 changes: 9 additions & 0 deletions examples/testing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "testing"
version = "0.1.0"
authors = ["my name <my@email.address>"]
edition = "2018"

[dependencies]
serenity = { path = "../../", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
13 changes: 13 additions & 0 deletions examples/testing/Makefile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extend = "../../Makefile.toml"

[tasks.examples_build]
alias = "build"

[tasks.examples_build_release]
alias = "build_release"

[tasks.examples_run]
alias = "run"

[tasks.examples_run_release]
alias = "run_release"
42 changes: 42 additions & 0 deletions examples/testing/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use serenity::model::prelude::interaction::application_command::*;
use serenity::model::prelude::*;
use serenity::prelude::*;

async fn message(ctx: &Context, msg: Message) -> Result<(), serenity::Error> {
if let Some(_args) = msg.content.strip_prefix("testmessage ") {
println!("command message: {:#?}", msg);
} else {
return Ok(());
}

msg.react(&ctx, '✅').await?;
Ok(())
}

async fn interaction(
_ctx: &Context,
_interaction: ApplicationCommandInteraction,
) -> Result<(), serenity::Error> {
Ok(())
}

struct Handler;
#[serenity::async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
message(&ctx, msg).await.unwrap();
}

async fn interaction_create(&self, ctx: Context, i: Interaction) {
if let Interaction::ApplicationCommand(i) = i {
interaction(&ctx, i).await.unwrap();
}
}
}

#[tokio::main]
async fn main() -> Result<(), serenity::Error> {
let token = std::env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT;
Client::builder(token, intents).event_handler(Handler).await?.start().await
}
3 changes: 3 additions & 0 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,7 @@ mod test {
name: "user 1".to_owned(),
public_flags: None,
banner: None,
member: None,
accent_colour: None,
},
channel_id: ChannelId(2),
Expand Down Expand Up @@ -1088,6 +1089,8 @@ mod test {
referenced_message: None,
interaction: None,
components: vec![],
application_id: None,
thread: None,
},
};

Expand Down
71 changes: 39 additions & 32 deletions src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,33 @@ use crate::{
/// A representation of a message over a guild's text channel, a group, or a
/// private channel.
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#message-object).
/// [Discord docs](https://discord.com/developers/docs/resources/channel#message-object) with some
/// [extra fields](https://discord.com/developers/docs/topics/gateway-events#message-create-message-create-extra-fields).
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Message {
/// The unique Id of the message. Can be used to calculate the creation date
/// of the message.
pub id: MessageId,
/// An vector of the files attached to a message.
pub attachments: Vec<Attachment>,
/// The user that sent the message.
pub author: User,
/// The Id of the [`Channel`] that the message was sent to.
pub channel_id: ChannelId,
/// The user that sent the message.
pub author: User,
/// The content of the message.
pub content: String,
/// Initial message creation timestamp, calculated from its Id.
pub timestamp: Timestamp,
/// The timestamp of the last time the message was updated, if it was.
pub edited_timestamp: Option<Timestamp>,
/// Array of embeds sent with the message.
pub embeds: Vec<Embed>,
/// The Id of the [`Guild`] that the message was sent in. This value will
/// only be present if this message was received over the gateway.
pub guild_id: Option<GuildId>,
/// Indicator of the type of message this is, i.e. whether it is a regular
/// message or a system message.
#[serde(rename = "type")]
pub kind: MessageType,
/// A partial amount of data about the user's member data, if this message
/// was sent in a guild.
pub member: Option<PartialMember>,
/// Indicator of whether the command is to be played back via
/// text-to-speech.
///
/// In the client, this is done via the `/tts` slash command.
pub tts: bool,
/// Indicator of whether the message mentions everyone.
pub mention_everyone: bool,
/// Array of users mentioned in the message.
pub mentions: Vec<User>,
/// Array of [`Role`]s' Ids mentioned in the message.
pub mention_roles: Vec<RoleId>,
/// Channels specifically mentioned in this message.
Expand All @@ -91,45 +87,56 @@ pub struct Message {
/// [discord-docs]: https://discord.com/developers/docs/resources/channel#message-object
#[serde(default = "Vec::new")]
pub mention_channels: Vec<ChannelMention>,
/// Array of users mentioned in the message.
pub mentions: Vec<User>,
/// An vector of the files attached to a message.
pub attachments: Vec<Attachment>,
/// Array of embeds sent with the message.
pub embeds: Vec<Embed>,
/// Array of reactions performed on the message.
#[serde(default)]
pub reactions: Vec<MessageReaction>,
/// Non-repeating number used for ensuring message order.
#[serde(default)]
pub nonce: Value,
/// Indicator of whether the message is pinned.
pub pinned: bool,
/// Array of reactions performed on the message.
#[serde(default)]
pub reactions: Vec<MessageReaction>,
/// Initial message creation timestamp, calculated from its Id.
pub timestamp: Timestamp,
/// Indicator of whether the command is to be played back via
/// text-to-speech.
///
/// In the client, this is done via the `/tts` slash command.
pub tts: bool,
/// The Id of the webhook that sent this message, if one did.
pub webhook_id: Option<WebhookId>,
/// Indicator of the type of message this is, i.e. whether it is a regular
/// message or a system message.
#[serde(rename = "type")]
pub kind: MessageType,
/// Sent with Rich Presence-related chat embeds.
pub activity: Option<MessageActivity>,
/// Sent with Rich Presence-related chat embeds.
pub application: Option<MessageApplication>,
/// If the message is an Interaction or application-owned webhook, this is the id of the
/// application.
pub application_id: Option<ApplicationId>,
/// Reference data sent with crossposted messages.
pub message_reference: Option<MessageReference>,
/// Bit flags describing extra features of the message.
pub flags: Option<MessageFlags>,
/// Array of message sticker item objects.
#[serde(default)]
pub sticker_items: Vec<StickerItem>,
/// The message that was replied to using this message.
pub referenced_message: Option<Box<Message>>, // Boxed to avoid recursion
/// Sent if the message is a response to an [`Interaction`].
///
/// [`Interaction`]: crate::model::application::interaction::Interaction
pub interaction: Option<MessageInteraction>,
/// The thread that was started from this message, includes thread member object.
pub thread: Option<GuildChannel>,
/// The components of this message
#[serde(default)]
pub components: Vec<ActionRow>,
/// Array of message sticker item objects.
#[serde(default)]
pub sticker_items: Vec<StickerItem>,
// Field omitted: stickers (it's deprecated by Discord)
/// The Id of the [`Guild`] that the message was sent in. This value will
/// only be present if this message was received over the gateway.
pub guild_id: Option<GuildId>,
/// A partial amount of data about the user's member data, if this message
/// was sent in a guild.
pub member: Option<PartialMember>,
}

#[cfg(feature = "model")]
Expand Down
1 change: 1 addition & 0 deletions src/model/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ mod test {
public_flags: None,
banner: None,
accent_colour: None,
member: None,
},
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/model/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ pub struct ClientStatus {

/// Information about the user of a [`Presence`] event.
///
/// [Discord docs](https://discord.com/developers/docs/topics/gateway#presence-update).
/// [Discord docs](https://discord.com/developers/docs/topics/gateway-events#presence-update).
#[derive(Clone, Default, Debug, Deserialize, Serialize)]
#[non_exhaustive]
#[serde(default)]
Expand Down Expand Up @@ -457,6 +457,7 @@ impl PresenceUser {
public_flags: self.public_flags,
banner: None,
accent_colour: None,
member: None,
})
}

Expand All @@ -476,6 +477,7 @@ impl PresenceUser {
public_flags: self.public_flags,
banner: None,
accent_colour: None,
member: None,
})
}

Expand Down
1 change: 1 addition & 0 deletions src/model/mention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ mod test {
public_flags: None,
banner: None,
accent_colour: None,
member: None,
};
let member = Member {
deaf: false,
Expand Down
8 changes: 8 additions & 0 deletions src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,11 @@ pub struct User {
#[cfg(not(feature = "utils"))]
#[serde(rename = "accent_color")]
pub accent_colour: Option<u32>,
/// Only included in [`Message::mentions`] for messages from the gateway.
///
/// [Discord docs](https://discord.com/developers/docs/topics/gateway-events#message-create-message-create-extra-fields).
// Box required to avoid infinitely recursive types
pub member: Option<Box<PartialMember>>,
}

bitflags! {
Expand Down Expand Up @@ -713,6 +718,7 @@ impl Default for User {
public_flags: None,
banner: None,
accent_colour: None,
member: None,
}
}
}
Expand Down Expand Up @@ -1185,6 +1191,7 @@ impl From<CurrentUser> for User {
public_flags: user.public_flags,
banner: user.banner,
accent_colour: user.accent_colour,
member: None,
}
}
}
Expand All @@ -1200,6 +1207,7 @@ impl<'a> From<&'a CurrentUser> for User {
public_flags: user.public_flags,
banner: user.banner.clone(),
accent_colour: user.accent_colour,
member: None,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils/content_safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ mod tests {
public_flags: None,
banner: None,
accent_colour: None,
member: None,
};

let outside_cache_user = User {
Expand All @@ -303,6 +304,7 @@ mod tests {
public_flags: None,
banner: None,
accent_colour: None,
member: None,
};

let mut guild = Guild {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/custom_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ fn dummy_message() -> Message {
public_flags: None,
banner: None,
accent_colour: None,
member: None,
},
channel_id: ChannelId::default(),
content: String::new(),
Expand All @@ -277,5 +278,7 @@ fn dummy_message() -> Message {
referenced_message: None,
interaction: None,
components: vec![],
application_id: None,
thread: None,
}
}