Skip to content
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
7 changes: 4 additions & 3 deletions twilight-cache-inmemory/src/event/guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@ mod tests {
GuildCreate, GuildUpdate, MemberAdd, MemberRemove, UnavailableGuild,
},
guild::{
DefaultMessageNotificationLevel, ExplicitContentFilter, Guild, MfaLevel, NSFWLevel,
PartialGuild, Permissions, PremiumTier, SystemChannelFlags, VerificationLevel,
AfkTimeout, DefaultMessageNotificationLevel, ExplicitContentFilter, Guild, MfaLevel,
NSFWLevel, PartialGuild, Permissions, PremiumTier, SystemChannelFlags,
VerificationLevel,
},
id::Id,
util::datetime::{Timestamp, TimestampParseError},
Expand Down Expand Up @@ -388,7 +389,7 @@ mod tests {

let guild = Guild {
afk_channel_id: None,
afk_timeout: 300,
afk_timeout: AfkTimeout::FIFTEEN_MINUTES,
application_id: None,
approximate_member_count: None,
approximate_presence_count: None,
Expand Down
8 changes: 4 additions & 4 deletions twilight-cache-inmemory/src/model/guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::slice::Iter;
use serde::Serialize;
use twilight_model::{
guild::{
DefaultMessageNotificationLevel, ExplicitContentFilter, GuildFeature, MfaLevel, NSFWLevel,
Permissions, PremiumTier, SystemChannelFlags, VerificationLevel,
AfkTimeout, DefaultMessageNotificationLevel, ExplicitContentFilter, GuildFeature, MfaLevel,
NSFWLevel, Permissions, PremiumTier, SystemChannelFlags, VerificationLevel,
},
id::{
marker::{ApplicationMarker, ChannelMarker, GuildMarker, UserMarker},
Expand All @@ -19,7 +19,7 @@ use twilight_model::{
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct CachedGuild {
pub(crate) afk_channel_id: Option<Id<ChannelMarker>>,
pub(crate) afk_timeout: u64,
pub(crate) afk_timeout: AfkTimeout,
pub(crate) application_id: Option<Id<ApplicationMarker>>,
pub(crate) banner: Option<ImageHash>,
pub(crate) default_message_notifications: DefaultMessageNotificationLevel,
Expand Down Expand Up @@ -63,7 +63,7 @@ impl CachedGuild {
}

/// AFK timeout in seconds.
pub const fn afk_timeout(&self) -> u64 {
pub const fn afk_timeout(&self) -> AfkTimeout {
self.afk_timeout
}

Expand Down
6 changes: 3 additions & 3 deletions twilight-cache-inmemory/src/permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,8 +660,8 @@ mod tests {
ChannelCreate, GuildCreate, MemberAdd, MemberUpdate, RoleCreate, ThreadCreate,
},
guild::{
DefaultMessageNotificationLevel, ExplicitContentFilter, Guild, MfaLevel, NSFWLevel,
Permissions, PremiumTier, Role, SystemChannelFlags, VerificationLevel,
AfkTimeout, DefaultMessageNotificationLevel, ExplicitContentFilter, Guild, MfaLevel,
NSFWLevel, Permissions, PremiumTier, Role, SystemChannelFlags, VerificationLevel,
},
id::{
marker::{ChannelMarker, GuildMarker, RoleMarker, UserMarker},
Expand Down Expand Up @@ -708,7 +708,7 @@ mod tests {
Guild {
id: GUILD_ID,
afk_channel_id: None,
afk_timeout: 300,
afk_timeout: AfkTimeout::FIVE_MINUTES,
application_id: None,
banner: None,
channels: Vec::new(),
Expand Down
6 changes: 3 additions & 3 deletions twilight-cache-inmemory/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use twilight_model::{
GatewayReaction,
},
guild::{
DefaultMessageNotificationLevel, Emoji, ExplicitContentFilter, Guild, Member, MfaLevel,
NSFWLevel, PartialMember, Permissions, PremiumTier, Role, SystemChannelFlags,
AfkTimeout, DefaultMessageNotificationLevel, Emoji, ExplicitContentFilter, Guild, Member,
MfaLevel, NSFWLevel, PartialMember, Permissions, PremiumTier, Role, SystemChannelFlags,
VerificationLevel,
},
id::{
Expand Down Expand Up @@ -361,7 +361,7 @@ pub fn user(id: Id<UserMarker>) -> User {
pub fn guild(id: Id<GuildMarker>, member_count: Option<u64>) -> Guild {
Guild {
afk_channel_id: None,
afk_timeout: 0,
afk_timeout: AfkTimeout::FIFTEEN_MINUTES,
application_id: None,
approximate_member_count: None,
approximate_presence_count: None,
Expand Down
8 changes: 4 additions & 4 deletions twilight-http/src/request/guild/create_guild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use std::{
use twilight_model::{
channel::ChannelType,
guild::{
DefaultMessageNotificationLevel, ExplicitContentFilter, PartialGuild, Permissions,
SystemChannelFlags, VerificationLevel,
AfkTimeout, DefaultMessageNotificationLevel, ExplicitContentFilter, PartialGuild,
Permissions, SystemChannelFlags, VerificationLevel,
},
http::permission_overwrite::PermissionOverwrite,
id::{
Expand Down Expand Up @@ -103,7 +103,7 @@ struct CreateGuildFields<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
afk_channel_id: Option<Id<ChannelMarker>>,
#[serde(skip_serializing_if = "Option::is_none")]
afk_timeout: Option<u64>,
afk_timeout: Option<AfkTimeout>,
#[serde(skip_serializing_if = "Option::is_none")]
channels: Option<Vec<GuildChannelFields>>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -278,7 +278,7 @@ impl<'a> CreateGuild<'a> {
}

/// Set the AFK timeout, in seconds.
pub const fn afk_timeout(mut self, afk_timeout: u64) -> Self {
pub const fn afk_timeout(mut self, afk_timeout: AfkTimeout) -> Self {
self.fields.afk_timeout = Some(afk_timeout);

self
Expand Down
134 changes: 134 additions & 0 deletions twilight-model/src/guild/afk_timeout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Duration of a user being AFK before being timed out from a voice channel.
///
/// This value is configured [for guilds][`Guild::afk_timeout`].
///
/// # Examples
///
/// ```
/// use twilight_model::guild::AfkTimeout;
///
/// assert_eq!(300, AfkTimeout::FIVE_MINUTES);
/// ```
///
/// [`Guild::afk_timeout`]: super::Guild::afk_timeout
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
pub struct AfkTimeout(u16);

impl AfkTimeout {
/// AFK timeout of one minute.
pub const ONE_MINUTE: Self = Self(60);

/// AFK timeout of five minutes.
pub const FIVE_MINUTES: Self = Self(300);

/// AFK timeout of fifteen minutes.
pub const FIFTEEN_MINUTES: Self = Self(900);

/// AFK timeout of thirty minutes.
pub const THIRTY_MINUTES: Self = Self(1800);

/// AFK timeout of one hour.
pub const ONE_HOUR: Self = Self(3600);

/// Retrieve the duration of the AFK timeout in seconds.
///
/// # Examples
///
/// ```
/// use twilight_model::guild::AfkTimeout;
///
/// assert_eq!(60, AfkTimeout::ONE_MINUTE.get());
/// ```
pub const fn get(self) -> u16 {
self.0
}
}

impl From<u16> for AfkTimeout {
fn from(value: u16) -> Self {
Self(value)
}
}
Comment on lines +51 to +55
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just highlighting that this allows any value

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is fine since it's non exaustive and we have no guarantee discord won't add any extra values later


impl From<AfkTimeout> for Duration {
fn from(value: AfkTimeout) -> Self {
Self::from_secs(u64::from(value.get()))
}
}

impl PartialEq<u16> for AfkTimeout {
fn eq(&self, other: &u16) -> bool {
self.get() == *other
}
}

impl PartialEq<AfkTimeout> for u16 {
fn eq(&self, other: &AfkTimeout) -> bool {
*self == other.get()
}
}

#[cfg(test)]
mod tests {
use super::AfkTimeout;
use serde::{Deserialize, Serialize};
use serde_test::Token;
use static_assertions::assert_impl_all;
use std::{fmt::Debug, hash::Hash, time::Duration};

assert_impl_all!(
AfkTimeout: Clone,
Copy,
Debug,
Deserialize<'static>,
Eq,
Hash,
PartialEq,
Send,
Serialize,
Sync
);

const MAP: &[(AfkTimeout, u16)] = &[
(AfkTimeout::ONE_MINUTE, 60),
(AfkTimeout::FIVE_MINUTES, 300),
(AfkTimeout::FIFTEEN_MINUTES, 900),
(AfkTimeout::THIRTY_MINUTES, 1800),
(AfkTimeout::ONE_HOUR, 3600),
];

#[test]
fn serde() {
for (value, seconds) in MAP {
serde_test::assert_tokens(
value,
&[
Token::NewtypeStruct { name: "AfkTimeout" },
Token::U16(*seconds),
],
);
assert_eq!(*value, AfkTimeout::from(*seconds));
assert_eq!(*seconds, value.get());
}
}

/// Test two-way equality implementation.
#[test]
fn eq() {
assert_eq!(300, AfkTimeout::FIVE_MINUTES);
assert_eq!(AfkTimeout::FIVE_MINUTES, 300);
}

/// Test conversion to [`std::time::Duration`].
#[test]
fn std_time_duration() {
for (kind, _) in MAP {
let std_duration = Duration::from(*kind);
assert_eq!(u64::from(kind.get()), std_duration.as_secs());
}
}
}
17 changes: 10 additions & 7 deletions twilight-model/src/guild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod member;
pub mod scheduled_event;
pub mod template;

mod afk_timeout;
mod ban;
mod default_message_notification_level;
mod emoji;
Expand Down Expand Up @@ -46,7 +47,8 @@ mod widget;
#[doc(inline)]
pub use self::member::Member;
pub use self::{
ban::Ban, default_message_notification_level::DefaultMessageNotificationLevel, emoji::Emoji,
afk_timeout::AfkTimeout, ban::Ban,
default_message_notification_level::DefaultMessageNotificationLevel, emoji::Emoji,
explicit_content_filter::ExplicitContentFilter, feature::GuildFeature, info::GuildInfo,
integration::GuildIntegration, integration_account::IntegrationAccount,
integration_application::IntegrationApplication,
Expand Down Expand Up @@ -79,7 +81,7 @@ use std::fmt::{Formatter, Result as FmtResult};
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct Guild {
pub afk_channel_id: Option<Id<ChannelMarker>>,
pub afk_timeout: u64,
pub afk_timeout: AfkTimeout,
pub application_id: Option<Id<ApplicationMarker>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub approximate_member_count: Option<u64>,
Expand Down Expand Up @@ -687,7 +689,7 @@ impl<'de> Deserialize<'de> for Guild {

tracing::trace!(
?afk_channel_id,
%afk_timeout,
?afk_timeout,
?application_id,
?approximate_member_count,
?approximate_presence_count,
Expand Down Expand Up @@ -865,8 +867,8 @@ impl<'de> Deserialize<'de> for Guild {
#[cfg(test)]
mod tests {
use super::{
DefaultMessageNotificationLevel, ExplicitContentFilter, Guild, GuildFeature, MfaLevel,
NSFWLevel, Permissions, PremiumTier, SystemChannelFlags, VerificationLevel,
AfkTimeout, DefaultMessageNotificationLevel, ExplicitContentFilter, Guild, GuildFeature,
MfaLevel, NSFWLevel, Permissions, PremiumTier, SystemChannelFlags, VerificationLevel,
};
use crate::{
id::Id,
Expand All @@ -883,7 +885,7 @@ mod tests {

let value = Guild {
afk_channel_id: Some(Id::new(2)),
afk_timeout: 900,
afk_timeout: AfkTimeout::FIFTEEN_MINUTES,
application_id: Some(Id::new(3)),
approximate_member_count: Some(1_200),
approximate_presence_count: Some(900),
Expand Down Expand Up @@ -943,7 +945,8 @@ mod tests {
Token::NewtypeStruct { name: "Id" },
Token::Str("2"),
Token::Str("afk_timeout"),
Token::U64(900),
Token::NewtypeStruct { name: "AfkTimeout" },
Token::U16(900),
Token::Str("application_id"),
Token::Some,
Token::NewtypeStruct { name: "Id" },
Expand Down
Loading