From 55b0f268e2d28e26a5f786b85239c29023652d38 Mon Sep 17 00:00:00 2001 From: Vincent Junge Date: Tue, 20 Jun 2023 22:03:29 +0200 Subject: [PATCH] more consistent use of snowflakes --- src/api/channels/channels.rs | 14 +++---- src/api/channels/messages.rs | 6 +-- src/api/channels/permissions.rs | 10 ++--- src/api/channels/reactions.rs | 7 +--- src/gateway.rs | 2 - src/types/entities/audit_log.rs | 2 +- src/types/entities/channel.rs | 26 +++++-------- src/types/entities/guild.rs | 24 ++++++------ src/types/entities/template.rs | 2 +- src/types/events/call.rs | 18 ++++----- src/types/events/channel.rs | 12 +++--- src/types/events/guild.rs | 46 +++++++++++----------- src/types/events/integration.rs | 12 +++--- src/types/events/invite.rs | 6 +-- src/types/events/lazy_request.rs | 4 +- src/types/events/message.rs | 59 +++++++++++++++-------------- src/types/events/passive_update.rs | 4 +- src/types/events/request_members.rs | 7 ++-- src/types/events/thread.rs | 13 ++++--- src/types/events/voice.rs | 8 ++-- src/types/events/webhooks.rs | 6 ++- src/types/interfaces/activity.rs | 4 +- src/types/schema/channel.rs | 8 ++-- tests/channel.rs | 24 ++++-------- tests/message.rs | 6 +-- 25 files changed, 157 insertions(+), 173 deletions(-) diff --git a/src/api/channels/channels.rs b/src/api/channels/channels.rs index 290e240..94e0993 100644 --- a/src/api/channels/channels.rs +++ b/src/api/channels/channels.rs @@ -5,7 +5,7 @@ use crate::{ api::common, errors::ChorusLibError, instance::UserMeta, - types::{Channel, ChannelModifySchema}, + types::{Channel, ChannelModifySchema, Message, Snowflake}, }; impl Channel { @@ -51,10 +51,8 @@ impl Channel { self.id )) .bearer_auth(user.token()); - let response = - common::handle_request_as_result(request, user, crate::api::limits::LimitType::Channel) - .await; - response + common::handle_request_as_result(request, user, crate::api::limits::LimitType::Channel) + .await } /// Modifies a channel. @@ -73,7 +71,7 @@ impl Channel { /// A `Result` that contains a `Channel` object if the request was successful, or an `ChorusLibError` if an error occurred during the request. pub async fn modify( modify_data: ChannelModifySchema, - channel_id: &str, + channel_id: Snowflake, user: &mut UserMeta, ) -> Result { let request = Client::new() @@ -84,13 +82,11 @@ impl Channel { )) .bearer_auth(user.token()) .body(to_string(&modify_data).unwrap()); - let channel = common::deserialize_response::( + common::deserialize_response::( request, user, crate::api::limits::LimitType::Channel, ) .await - .unwrap(); - Ok(channel) } } diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index 61801a8..c61756a 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -5,7 +5,7 @@ use serde_json::to_string; use crate::api::deserialize_response; use crate::instance::UserMeta; -use crate::types::{Message, MessageSendSchema, PartialDiscordFileAttachment}; +use crate::types::{Message, MessageSendSchema, PartialDiscordFileAttachment, Snowflake}; impl Message { /** @@ -21,7 +21,7 @@ impl Message { */ pub async fn send( user: &mut UserMeta, - channel_id: String, + channel_id: Snowflake, message: &mut MessageSendSchema, files: Option>, ) -> Result { @@ -89,7 +89,7 @@ impl UserMeta { pub async fn send_message( &mut self, message: &mut MessageSendSchema, - channel_id: String, + channel_id: Snowflake, files: Option>, ) -> Result { Message::send(self, channel_id, message, files).await diff --git a/src/api/channels/permissions.rs b/src/api/channels/permissions.rs index 263b3d1..361b4e1 100644 --- a/src/api/channels/permissions.rs +++ b/src/api/channels/permissions.rs @@ -2,10 +2,10 @@ use reqwest::Client; use serde_json::to_string; use crate::{ - api::{handle_request, handle_request_as_result}, + api::handle_request_as_result, errors::ChorusLibError, instance::UserMeta, - types::{self, PermissionOverwrite}, + types::{self, PermissionOverwrite, Snowflake}, }; impl types::Channel { @@ -22,7 +22,7 @@ impl types::Channel { /// This function returns a result that is either [`Ok(())`] if the request is successful, or an [`Err(ChorusLibError)`]. pub async fn edit_permissions( user: &mut UserMeta, - channel_id: &str, + channel_id: Snowflake, overwrite: PermissionOverwrite, ) -> Result<(), ChorusLibError> { let url = { @@ -58,8 +58,8 @@ impl types::Channel { /// This function returns a Result that is either [`Ok(())`] if the request is successfulm or an [`Err(ChorusLibError)`]. pub async fn delete_permission( user: &mut UserMeta, - channel_id: &str, - overwrite_id: &str, + channel_id: Snowflake, + overwrite_id: Snowflake, ) -> Result<(), ChorusLibError> { let url = format!( "{}/channels/{}/permissions/{}", diff --git a/src/api/channels/reactions.rs b/src/api/channels/reactions.rs index 77b7e32..0f04c5e 100644 --- a/src/api/channels/reactions.rs +++ b/src/api/channels/reactions.rs @@ -1,11 +1,6 @@ use reqwest::Client; -use crate::{ - api::{handle_request, handle_request_as_result}, - errors::ChorusLibError, - instance::UserMeta, - types, -}; +use crate::{api::handle_request_as_result, errors::ChorusLibError, instance::UserMeta, types}; /** Useful metadata for working with [`types::Reaction`], bundled together nicely. diff --git a/src/gateway.rs b/src/gateway.rs index 7ead0ce..2f90217 100644 --- a/src/gateway.rs +++ b/src/gateway.rs @@ -1,9 +1,7 @@ use crate::errors::GatewayError; -use crate::errors::ObserverError; use crate::gateway::events::Events; use crate::types; use crate::types::WebSocketEvent; -use std::any::Any; use std::sync::Arc; use futures_util::stream::SplitSink; diff --git a/src/types/entities/audit_log.rs b/src/types/entities/audit_log.rs index 21682ae..1e04e8b 100644 --- a/src/types/entities/audit_log.rs +++ b/src/types/entities/audit_log.rs @@ -7,7 +7,7 @@ use crate::types::utils::Snowflake; pub struct AuditLogEntry { pub target_id: Option, pub changes: Option>, - pub user_id: Option, + pub user_id: Option, pub id: Snowflake, // to:do implement an enum for these types pub action_type: u8, diff --git a/src/types/entities/channel.rs b/src/types/entities/channel.rs index 48dd0a5..5471e22 100644 --- a/src/types/entities/channel.rs +++ b/src/types/entities/channel.rs @@ -1,9 +1,6 @@ use chrono::Utc; use serde::{Deserialize, Serialize}; -use serde_aux::prelude::{ - deserialize_number_from_string, deserialize_option_number_from_string, - deserialize_string_from_number, -}; +use serde_aux::prelude::deserialize_string_from_number; use serde_repr::{Deserialize_repr, Serialize_repr}; use crate::types::{ @@ -27,7 +24,7 @@ pub struct Channel { pub name: Option, pub topic: Option, pub nsfw: Option, - pub last_message_id: Option, + pub last_message_id: Option, pub bitrate: Option, pub user_limit: Option, pub rate_limit_per_user: Option, @@ -37,7 +34,7 @@ pub struct Channel { pub owner_id: Option, pub application_id: Option, pub managed: Option, - pub parent_id: Option, + pub parent_id: Option, pub last_pin_timestamp: Option, pub rtc_region: Option, pub video_quality_mode: Option, @@ -70,20 +67,16 @@ pub struct Channel { #[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)] pub struct Tag { - #[serde(default)] - #[serde(deserialize_with = "deserialize_number_from_string")] - pub id: u64, + pub id: Snowflake, pub name: String, pub moderated: bool, - #[serde(default)] - #[serde(deserialize_with = "deserialize_option_number_from_string")] - pub emoji_id: Option, + pub emoji_id: Option, pub emoji_name: Option, } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] pub struct PermissionOverwrite { - pub id: String, + pub id: Snowflake, #[serde(rename = "type")] #[serde(deserialize_with = "deserialize_string_from_number")] pub overwrite_type: String, @@ -107,8 +100,8 @@ pub struct ThreadMetadata { #[derive(Default, Debug, Deserialize, Serialize, Clone, PartialEq, Eq)] pub struct ThreadMember { - pub id: Option, - pub user_id: Option, + pub id: Option, + pub user_id: Option, pub join_timestamp: Option, pub flags: Option, pub member: Option, @@ -117,8 +110,7 @@ pub struct ThreadMember { #[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)] pub struct DefaultReaction { #[serde(default)] - #[serde(deserialize_with = "deserialize_option_number_from_string")] - pub emoji_id: Option, + pub emoji_id: Option, pub emoji_name: Option, } diff --git a/src/types/entities/guild.rs b/src/types/entities/guild.rs index 5e513d9..857ed2a 100644 --- a/src/types/entities/guild.rs +++ b/src/types/entities/guild.rs @@ -16,7 +16,7 @@ pub struct Guild { pub afk_channel_id: Option, pub afk_timeout: Option, #[cfg_attr(feature = "sqlx", sqlx(skip))] - pub application_id: Option, + pub application_id: Option, #[cfg_attr(feature = "sqlx", sqlx(skip))] pub approximate_member_count: Option, #[cfg_attr(feature = "sqlx", sqlx(skip))] @@ -110,13 +110,13 @@ pub struct GuildInvite { pub max_age: Option, pub created_at: DateTime, pub expires_at: Option>, - pub guild_id: String, + pub guild_id: Snowflake, pub guild: Option, - pub channel_id: String, + pub channel_id: Snowflake, pub channel: Option, - pub inviter_id: Option, + pub inviter_id: Option, pub inviter: Option, - pub target_user_id: Option, + pub target_user_id: Option, pub target_user: Option, pub target_user_type: Option, pub vanity_url: Option, @@ -124,22 +124,22 @@ pub struct GuildInvite { #[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)] pub struct UnavailableGuild { - id: String, + id: Snowflake, unavailable: bool, } #[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)] pub struct GuildCreateResponse { - pub id: String, + pub id: Snowflake, } #[derive(Serialize, Deserialize, Debug, Default, Clone)] /// See https://discord.com/developers/docs/resources/guild-scheduled-event#guild-scheduled-event-object pub struct GuildScheduledEvent { - pub id: String, - pub guild_id: String, - pub channel_id: Option, - pub creator_id: Option, + pub id: Snowflake, + pub guild_id: Snowflake, + pub channel_id: Option, + pub creator_id: Option, pub name: String, pub description: String, pub scheduled_start_time: DateTime, @@ -147,7 +147,7 @@ pub struct GuildScheduledEvent { pub privacy_level: GuildScheduledEventPrivacyLevel, pub status: GuildScheduledEventStatus, pub entity_type: GuildScheduledEventEntityType, - pub entity_id: Option, + pub entity_id: Option, pub entity_metadata: Option, pub creator: Option, pub user_count: Option, diff --git a/src/types/entities/template.rs b/src/types/entities/template.rs index c2a2aca..b6eb3e9 100644 --- a/src/types/entities/template.rs +++ b/src/types/entities/template.rs @@ -19,7 +19,7 @@ pub struct GuildTemplate { pub creator: User, pub created_at: DateTime, pub updated_at: DateTime, - pub source_guild_id: String, + pub source_guild_id: Snowflake, #[cfg_attr(feature = "sqlx", sqlx(skip))] pub source_guild: Vec, // Unsure how a {recursive: Guild} looks like, might be a Vec? diff --git a/src/types/events/call.rs b/src/types/events/call.rs index 1f7401a..67225fb 100644 --- a/src/types/events/call.rs +++ b/src/types/events/call.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::types::{VoiceState, WebSocketEvent}; +use crate::types::{Snowflake, VoiceState, WebSocketEvent}; #[derive(Debug, Deserialize, Serialize, Default, Clone)] /// Officially Undocumented; @@ -13,10 +13,10 @@ pub struct CallCreate { pub ringing: Vec, pub region: String, // milan - pub message_id: String, + pub message_id: Snowflake, /// What is this? pub embedded_activities: Vec, - pub channel_id: String, + pub channel_id: Snowflake, } impl WebSocketEvent for CallCreate {} @@ -28,12 +28,12 @@ impl WebSocketEvent for CallCreate {} /// Ex: {"t":"CALL_UPDATE","s":5,"op":0,"d":{"ringing":["837606544539254834"],"region":"milan","message_id":"1107191540234846308","guild_id":null,"channel_id":"837609115475771392"}} pub struct CallUpdate { /// Seems like a vec of channel ids - pub ringing: Vec, + pub ringing: Vec, pub region: String, // milan - pub message_id: String, - pub guild_id: Option, - pub channel_id: String, + pub message_id: Snowflake, + pub guild_id: Option, + pub channel_id: Snowflake, } impl WebSocketEvent for CallUpdate {} @@ -43,7 +43,7 @@ impl WebSocketEvent for CallUpdate {} /// Deletes a ringing call; /// Ex: {"t":"CALL_DELETE","s":8,"op":0,"d":{"channel_id":"837609115475771392"}} pub struct CallDelete { - pub channel_id: String, + pub channel_id: Snowflake, } impl WebSocketEvent for CallDelete {} @@ -54,7 +54,7 @@ impl WebSocketEvent for CallDelete {} /// /// Ex: {"op":13,"d":{"channel_id":"837609115475771392"}} pub struct CallSync { - pub channel_id: String, + pub channel_id: Snowflake, } impl WebSocketEvent for CallSync {} diff --git a/src/types/events/channel.rs b/src/types/events/channel.rs index 43ffa96..99c7640 100644 --- a/src/types/events/channel.rs +++ b/src/types/events/channel.rs @@ -1,13 +1,13 @@ -use crate::types::entities::Channel; use crate::types::events::WebSocketEvent; +use crate::types::{entities::Channel, Snowflake}; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; #[derive(Debug, Default, Deserialize, Serialize)] /// See https://discord.com/developers/docs/topics/gateway-events#channel-pins-update pub struct ChannelPinsUpdate { - pub guild_id: Option, - pub channel_id: String, + pub guild_id: Option, + pub channel_id: Snowflake, pub last_pin_timestamp: Option>, } @@ -37,15 +37,15 @@ impl WebSocketEvent for ChannelUpdate {} /// {"channel_unread_updates": [{"id": "816412869766938648", "last_message_id": "1085892012085104680"}} pub struct ChannelUnreadUpdate { pub channel_unread_updates: Vec, - pub guild_id: String, + pub guild_id: Snowflake, } #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// Contains very few fields from [Channel] /// See also [ChannelUnreadUpdates] pub struct ChannelUnreadUpdateObject { - pub id: String, - pub last_message_id: String, + pub id: Snowflake, + pub last_message_id: Snowflake, pub last_pin_timestamp: Option, } diff --git a/src/types/events/guild.rs b/src/types/events/guild.rs index decc2e8..ae69681 100644 --- a/src/types/events/guild.rs +++ b/src/types/events/guild.rs @@ -3,7 +3,9 @@ use serde::{Deserialize, Serialize}; use crate::types::entities::{Guild, PublicUser, UnavailableGuild}; use crate::types::events::WebSocketEvent; -use crate::types::{AuditLogEntry, Emoji, GuildMember, GuildScheduledEvent, RoleObject, Sticker}; +use crate::types::{ + AuditLogEntry, Emoji, GuildMember, GuildScheduledEvent, RoleObject, Snowflake, Sticker, +}; use super::PresenceUpdate; @@ -35,7 +37,7 @@ impl WebSocketEvent for GuildCreate {} /// See https://discord.com/developers/docs/topics/gateway-events#guild-ban-add-guild-ban-add-event-fields; /// Received to give info about a user being banned from a guild; pub struct GuildBanAdd { - pub guild_id: String, + pub guild_id: Snowflake, pub user: PublicUser, } @@ -45,7 +47,7 @@ impl WebSocketEvent for GuildBanAdd {} /// See https://discord.com/developers/docs/topics/gateway-events#guild-ban-remove; /// Received to give info about a user being unbanned from a guild; pub struct GuildBanRemove { - pub guild_id: String, + pub guild_id: Snowflake, pub user: PublicUser, } @@ -85,7 +87,7 @@ impl WebSocketEvent for GuildAuditLogEntryCreate {} /// See https://discord.com/developers/docs/topics/gateway-events#guild-emojis-update; /// Received to tell the client about a change to a guild's emoji list; pub struct GuildEmojisUpdate { - pub guild_id: String, + pub guild_id: Snowflake, pub emojis: Vec, } @@ -95,7 +97,7 @@ impl WebSocketEvent for GuildEmojisUpdate {} /// See https://discord.com/developers/docs/topics/gateway-events#guild-stickers-update; /// Received to tell the client about a change to a guild's sticker list; pub struct GuildStickersUpdate { - pub guild_id: String, + pub guild_id: Snowflake, pub stickers: Vec, } @@ -104,7 +106,7 @@ impl WebSocketEvent for GuildStickersUpdate {} #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#guild-integrations-update pub struct GuildIntegrationsUpdate { - pub guild_id: String, + pub guild_id: Snowflake, } impl WebSocketEvent for GuildIntegrationsUpdate {} @@ -115,7 +117,7 @@ impl WebSocketEvent for GuildIntegrationsUpdate {} pub struct GuildMemberAdd { #[serde(flatten)] pub member: GuildMember, - pub guild_id: String, + pub guild_id: Snowflake, } impl WebSocketEvent for GuildMemberAdd {} @@ -124,7 +126,7 @@ impl WebSocketEvent for GuildMemberAdd {} /// See https://discord.com/developers/docs/topics/gateway-events#guild-member-remove; /// Received to tell the client about a user leaving a guild; pub struct GuildMemberRemove { - pub guild_id: String, + pub guild_id: Snowflake, pub user: PublicUser, } @@ -133,8 +135,8 @@ impl WebSocketEvent for GuildMemberRemove {} #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#guild-member-update pub struct GuildMemberUpdate { - pub guild_id: String, - pub roles: Vec, + pub guild_id: Snowflake, + pub roles: Vec, pub user: PublicUser, pub nick: Option, pub avatar: Option, @@ -151,11 +153,11 @@ impl WebSocketEvent for GuildMemberUpdate {} #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#guild-members-chunk pub struct GuildMembersChunk { - pub guild_id: String, + pub guild_id: Snowflake, pub members: Vec, pub chunk_index: u16, pub chunk_count: u16, - pub not_found: Option>, + pub not_found: Option>, pub presences: Option, pub nonce: Option, } @@ -165,7 +167,7 @@ impl WebSocketEvent for GuildMembersChunk {} #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#guild-role-create pub struct GuildRoleCreate { - pub guild_id: String, + pub guild_id: Snowflake, pub role: RoleObject, } @@ -174,7 +176,7 @@ impl WebSocketEvent for GuildRoleCreate {} #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#guild-role-update pub struct GuildRoleUpdate { - pub guild_id: String, + pub guild_id: Snowflake, pub role: RoleObject, } @@ -183,8 +185,8 @@ impl WebSocketEvent for GuildRoleUpdate {} #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#guild-role-delete pub struct GuildRoleDelete { - pub guild_id: String, - pub role_id: String, + pub guild_id: Snowflake, + pub role_id: Snowflake, } impl WebSocketEvent for GuildRoleDelete {} @@ -219,9 +221,9 @@ impl WebSocketEvent for GuildScheduledEventDelete {} #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#guild-scheduled-event-user-add pub struct GuildScheduledEventUserAdd { - pub guild_scheduled_event_id: String, - pub user_id: String, - pub guild_id: String, + pub guild_scheduled_event_id: Snowflake, + pub user_id: Snowflake, + pub guild_id: Snowflake, } impl WebSocketEvent for GuildScheduledEventUserAdd {} @@ -229,9 +231,9 @@ impl WebSocketEvent for GuildScheduledEventUserAdd {} #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#guild-scheduled-event-user-remove pub struct GuildScheduledEventUserRemove { - pub guild_scheduled_event_id: String, - pub user_id: String, - pub guild_id: String, + pub guild_scheduled_event_id: Snowflake, + pub user_id: Snowflake, + pub guild_id: Snowflake, } impl WebSocketEvent for GuildScheduledEventUserRemove {} diff --git a/src/types/events/integration.rs b/src/types/events/integration.rs index 9a38e83..de55a5b 100644 --- a/src/types/events/integration.rs +++ b/src/types/events/integration.rs @@ -1,13 +1,13 @@ use serde::{Deserialize, Serialize}; -use crate::types::{Integration, WebSocketEvent}; +use crate::types::{Integration, Snowflake, WebSocketEvent}; #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#integration-create pub struct IntegrationCreate { #[serde(flatten)] pub integration: Integration, - pub guild_id: String, + pub guild_id: Snowflake, } impl WebSocketEvent for IntegrationCreate {} @@ -17,7 +17,7 @@ impl WebSocketEvent for IntegrationCreate {} pub struct IntegrationUpdate { #[serde(flatten)] pub integration: Integration, - pub guild_id: String, + pub guild_id: Snowflake, } impl WebSocketEvent for IntegrationUpdate {} @@ -25,9 +25,9 @@ impl WebSocketEvent for IntegrationUpdate {} #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#integration-delete pub struct IntegrationDelete { - pub id: String, - pub guild_id: String, - pub application_id: Option, + pub id: Snowflake, + pub guild_id: Snowflake, + pub application_id: Option, } impl WebSocketEvent for IntegrationDelete {} diff --git a/src/types/events/invite.rs b/src/types/events/invite.rs index fc6c80d..f04134d 100644 --- a/src/types/events/invite.rs +++ b/src/types/events/invite.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::types::{GuildInvite, WebSocketEvent}; +use crate::types::{GuildInvite, Snowflake, WebSocketEvent}; #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#invite-create @@ -14,8 +14,8 @@ impl WebSocketEvent for InviteCreate {} #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#invite-delete pub struct InviteDelete { - pub channel_id: String, - pub guild_id: Option, + pub channel_id: Snowflake, + pub guild_id: Option, pub code: String, } diff --git a/src/types/events/lazy_request.rs b/src/types/events/lazy_request.rs index 0cbea8a..2fd98af 100644 --- a/src/types/events/lazy_request.rs +++ b/src/types/events/lazy_request.rs @@ -2,6 +2,8 @@ use std::collections::HashMap; use serde::{Deserialize, Serialize}; +use crate::types::Snowflake; + use super::WebSocketEvent; #[derive(Debug, Deserialize, Serialize, Default, Clone)] @@ -15,7 +17,7 @@ use super::WebSocketEvent; /// /// {"op":14,"d":{"guild_id":"848582562217590824","typing":true,"activities":true,"threads":true}} pub struct LazyRequest { - pub guild_id: String, + pub guild_id: Snowflake, pub typing: bool, pub activities: bool, pub threads: bool, diff --git a/src/types/events/message.rs b/src/types/events/message.rs index f4f8115..5a67417 100644 --- a/src/types/events/message.rs +++ b/src/types/events/message.rs @@ -1,14 +1,17 @@ use serde::{Deserialize, Serialize}; -use crate::types::entities::{Emoji, GuildMember, Message, PublicUser}; +use crate::types::{ + entities::{Emoji, GuildMember, Message, PublicUser}, + Snowflake, +}; use super::WebSocketEvent; #[derive(Debug, Deserialize, Serialize, Default, Clone)] pub struct TypingStartEvent { - pub channel_id: String, - pub guild_id: Option, - pub user_id: String, + pub channel_id: Snowflake, + pub guild_id: Option, + pub user_id: Snowflake, pub timestamp: i64, pub member: Option, } @@ -20,7 +23,7 @@ impl WebSocketEvent for TypingStartEvent {} pub struct MessageCreate { #[serde(flatten)] message: Message, - guild_id: Option, + guild_id: Option, member: Option, mentions: Option>, } @@ -39,7 +42,7 @@ impl WebSocketEvent for MessageCreate {} pub struct MessageUpdate { #[serde(flatten)] message: Message, - guild_id: Option, + guild_id: Option, member: Option, mentions: Option>, } @@ -48,28 +51,28 @@ impl WebSocketEvent for MessageUpdate {} #[derive(Debug, Serialize, Deserialize, Default, Clone)] pub struct MessageDelete { - id: String, - channel_id: String, - guild_id: Option, + id: Snowflake, + channel_id: Snowflake, + guild_id: Option, } impl WebSocketEvent for MessageDelete {} #[derive(Debug, Serialize, Deserialize, Default, Clone)] pub struct MessageDeleteBulk { - ids: Vec, - channel_id: String, - guild_id: Option, + ids: Vec, + channel_id: Snowflake, + guild_id: Option, } impl WebSocketEvent for MessageDeleteBulk {} #[derive(Debug, Serialize, Deserialize, Default, Clone)] pub struct MessageReactionAdd { - user_id: String, - channel_id: String, - message_id: String, - guild_id: Option, + user_id: Snowflake, + channel_id: Snowflake, + message_id: Snowflake, + guild_id: Option, member: Option, emoji: Emoji, } @@ -78,10 +81,10 @@ impl WebSocketEvent for MessageReactionAdd {} #[derive(Debug, Serialize, Deserialize, Default, Clone)] pub struct MessageReactionRemove { - user_id: String, - channel_id: String, - message_id: String, - guild_id: Option, + user_id: Snowflake, + channel_id: Snowflake, + message_id: Snowflake, + guild_id: Option, emoji: Emoji, } @@ -89,18 +92,18 @@ impl WebSocketEvent for MessageReactionRemove {} #[derive(Debug, Serialize, Deserialize, Default, Clone)] pub struct MessageReactionRemoveAll { - channel_id: String, - message_id: String, - guild_id: Option, + channel_id: Snowflake, + message_id: Snowflake, + guild_id: Option, } impl WebSocketEvent for MessageReactionRemoveAll {} #[derive(Debug, Serialize, Deserialize, Default, Clone)] pub struct MessageReactionRemoveEmoji { - channel_id: String, - message_id: String, - guild_id: Option, + channel_id: Snowflake, + message_id: Snowflake, + guild_id: Option, emoji: Emoji, } @@ -118,13 +121,13 @@ impl WebSocketEvent for MessageReactionRemoveEmoji {} pub struct MessageACK { /// ? pub version: u16, - pub message_id: String, + pub message_id: Snowflake, /// This is an integer??? /// Not even unix, see '3070'??? pub last_viewed: Option, /// What flags? pub flags: Option, - pub channel_id: String, + pub channel_id: Snowflake, } impl WebSocketEvent for MessageACK {} diff --git a/src/types/events/passive_update.rs b/src/types/events/passive_update.rs index 7417467..234af3e 100644 --- a/src/types/events/passive_update.rs +++ b/src/types/events/passive_update.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use super::{ChannelUnreadUpdateObject, WebSocketEvent}; -use crate::types::{GuildMember, VoiceState}; +use crate::types::{GuildMember, Snowflake, VoiceState}; #[derive(Debug, Deserialize, Serialize, Default)] /// Officially Undocumented @@ -10,7 +10,7 @@ use crate::types::{GuildMember, VoiceState}; pub struct PassiveUpdateV1 { pub voice_states: Vec, pub members: Option>, - pub guild_id: String, + pub guild_id: Snowflake, pub channels: Vec, } diff --git a/src/types/events/request_members.rs b/src/types/events/request_members.rs index 67f37ce..2d537b9 100644 --- a/src/types/events/request_members.rs +++ b/src/types/events/request_members.rs @@ -1,14 +1,15 @@ -use crate::types::events::WebSocketEvent; +use crate::types::{events::WebSocketEvent, Snowflake}; use serde::{Deserialize, Serialize}; #[derive(Debug, Deserialize, Serialize, Default)] /// See https://discord.com/developers/docs/topics/gateway-events#request-guild-members-request-guild-members-structure pub struct GatewayRequestGuildMembers { - pub guild_id: String, + pub guild_id: Snowflake, pub query: Option, pub limit: u64, pub presences: Option, - pub user_ids: Option, + // TODO: allow array + pub user_ids: Option, pub nonce: Option, } diff --git a/src/types/events/thread.rs b/src/types/events/thread.rs index 88684ac..e8276e7 100644 --- a/src/types/events/thread.rs +++ b/src/types/events/thread.rs @@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize}; use crate::types::entities::{Channel, ThreadMember}; use crate::types::events::WebSocketEvent; +use crate::types::Snowflake; #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#thread-create @@ -33,8 +34,8 @@ impl WebSocketEvent for ThreadDelete {} #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#thread-list-sync pub struct ThreadListSync { - pub guild_id: String, - pub channel_ids: Option>, + pub guild_id: Snowflake, + pub channel_ids: Option>, pub threads: Vec, pub members: Option>, } @@ -47,7 +48,7 @@ impl WebSocketEvent for ThreadListSync {} pub struct ThreadMemberUpdate { #[serde(flatten)] pub member: ThreadMember, - pub guild_id: String, + pub guild_id: Snowflake, } impl WebSocketEvent for ThreadMemberUpdate {} @@ -55,12 +56,12 @@ impl WebSocketEvent for ThreadMemberUpdate {} #[derive(Debug, Default, Deserialize, Serialize, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#thread-members-update pub struct ThreadMembersUpdate { - pub id: String, - pub guild_id: String, + pub id: Snowflake, + pub guild_id: Snowflake, /// Capped at 50 pub member_count: u8, pub added_members: Option>, - pub removed_members: Option>, + pub removed_members: Option>, } impl WebSocketEvent for ThreadMembersUpdate {} diff --git a/src/types/events/voice.rs b/src/types/events/voice.rs index e896393..63e740a 100644 --- a/src/types/events/voice.rs +++ b/src/types/events/voice.rs @@ -1,4 +1,4 @@ -use crate::types::{events::WebSocketEvent, VoiceState}; +use crate::types::{events::WebSocketEvent, Snowflake, VoiceState}; use serde::{Deserialize, Serialize}; #[derive(Debug, Deserialize, Serialize, Default)] @@ -7,8 +7,8 @@ use serde::{Deserialize, Serialize}; /// /// Not to be confused with [VoiceStateUpdate]; pub struct UpdateVoiceState { - pub guild_id: Option, - pub channel_id: Option, + pub guild_id: Option, + pub channel_id: Option, pub self_mute: bool, pub self_deaf: bool, } @@ -34,7 +34,7 @@ impl WebSocketEvent for VoiceStateUpdate {} /// Received to indicate which voice endpoint, token and guild_id to use; pub struct VoiceServerUpdate { pub token: String, - pub guild_id: String, + pub guild_id: Snowflake, pub endpoint: Option, } diff --git a/src/types/events/webhooks.rs b/src/types/events/webhooks.rs index 3a93c60..3f0158e 100644 --- a/src/types/events/webhooks.rs +++ b/src/types/events/webhooks.rs @@ -1,12 +1,14 @@ use serde::{Deserialize, Serialize}; +use crate::types::Snowflake; + use super::WebSocketEvent; #[derive(Debug, Deserialize, Serialize, Default, Clone)] /// See https://discord.com/developers/docs/topics/gateway-events#webhooks-update pub struct WebhooksUpdate { - pub guild_id: String, - pub channel_id: String, + pub guild_id: Snowflake, + pub channel_id: Snowflake, } impl WebSocketEvent for WebhooksUpdate {} diff --git a/src/types/interfaces/activity.rs b/src/types/interfaces/activity.rs index 94a7168..1a48dfd 100644 --- a/src/types/interfaces/activity.rs +++ b/src/types/interfaces/activity.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::types::entities::Emoji; +use crate::types::{entities::Emoji, Snowflake}; #[derive(Debug, Deserialize, Serialize, Clone)] pub struct Activity { @@ -10,7 +10,7 @@ pub struct Activity { url: Option, created_at: i64, timestamps: Option, - application_id: Option, + application_id: Option, details: Option, state: Option, emoji: Option, diff --git a/src/types/schema/channel.rs b/src/types/schema/channel.rs index fb626bb..eff8277 100644 --- a/src/types/schema/channel.rs +++ b/src/types/schema/channel.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::types::entities::PermissionOverwrite; +use crate::types::{entities::PermissionOverwrite, Snowflake}; #[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "snake_case")] @@ -15,8 +15,8 @@ pub struct ChannelCreateSchema { pub rate_limit_per_user: Option, pub position: Option, pub permission_overwrites: Option>, - pub parent_id: Option, - pub id: Option, + pub parent_id: Option, + pub id: Option, pub nsfw: Option, pub rtc_region: Option, pub default_auto_archive_duration: Option, @@ -38,7 +38,7 @@ pub struct ChannelModifySchema { pub rate_limit_per_user: Option, pub position: Option, pub permission_overwrites: Option>, - pub parent_id: Option, + pub parent_id: Option, pub nsfw: Option, pub rtc_region: Option, pub default_auto_archive_duration: Option, diff --git a/tests/channel.rs b/tests/channel.rs index d73a318..2fad72c 100644 --- a/tests/channel.rs +++ b/tests/channel.rs @@ -47,13 +47,9 @@ async fn modify_channel() { default_thread_rate_limit_per_user: None, video_quality_mode: None, }; - let result = Channel::modify( - modify_data, - &bundle.channel.id.to_string(), - &mut bundle.user, - ) - .await - .unwrap(); + let result = Channel::modify(modify_data, bundle.channel.id, &mut bundle.user) + .await + .unwrap(); assert_eq!(result.name, Some("beepboop".to_string())); let permission_override = PermissionFlags::from_vec(Vec::from([ @@ -61,7 +57,7 @@ async fn modify_channel() { PermissionFlags::MANAGE_MESSAGES, ])); let permission_override = PermissionOverwrite { - id: bundle.user.object.id.to_string(), + id: bundle.user.object.id, overwrite_type: "1".to_string(), allow: permission_override, deny: "0".to_string(), @@ -69,19 +65,15 @@ async fn modify_channel() { Channel::edit_permissions( &mut bundle.user, - bundle.channel.id.to_string().as_str(), + bundle.channel.id, permission_override.clone(), ) .await .unwrap(); - Channel::delete_permission( - &mut bundle.user, - bundle.channel.id.to_string().as_str(), - &permission_override.id, - ) - .await - .unwrap(); + Channel::delete_permission(&mut bundle.user, bundle.channel.id, permission_override.id) + .await + .unwrap(); common::teardown(bundle).await } diff --git a/tests/message.rs b/tests/message.rs index 7f1da9d..a8bd2bd 100644 --- a/tests/message.rs +++ b/tests/message.rs @@ -14,7 +14,7 @@ async fn send_message() { }; let _ = bundle .user - .send_message(&mut message, bundle.channel.id.to_string(), None) + .send_message(&mut message, bundle.channel.id, None) .await .unwrap(); common::teardown(bundle).await @@ -57,7 +57,7 @@ async fn send_message_attachment() { .user .send_message( &mut message, - bundle.channel.id.to_string(), + bundle.channel.id, Some(vec![attachment.clone()]), ) .await @@ -76,7 +76,7 @@ async fn read_messages() { }; let _ = bundle .user - .send_message(&mut message, bundle.channel.id.to_string(), None) + .send_message(&mut message, bundle.channel.id, None) .await .unwrap(); common::teardown(bundle).await