Skip to main content

twilight_model/guild/
feature.rs

1#![allow(deprecated)]
2use std::borrow::Cow;
3
4use serde::{Deserialize, Serialize};
5
6/// Special and optional guild features.
7///
8/// See [Discord Docs/Guild Features].
9///
10/// [Discord Docs/Guild Features]: https://discord.com/developers/docs/resources/guild#guild-object-guild-features
11#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
12#[non_exhaustive]
13#[serde(from = "String", into = "Cow<'static, str>")]
14pub enum GuildFeature {
15    /// Has access to set an animated guild banner image.
16    AnimatedBanner,
17    /// Has access to set an animated guild icon.
18    AnimatedIcon,
19    /// Has set up auto moderation rules.
20    AutoModeration,
21    /// Has access to set a guild banner image.
22    Banner,
23    /// Can enable welcome screen, membership screening, stage channels,
24    /// discovery, and receives community updates.
25    Community,
26    /// Guild has enabled monetization.
27    CreatorMonetizableProvisional,
28    /// Guild has enabled the role subscription promotional page.
29    CreatorStorePage,
30    /// Guild has been set as a support server on the App Directory.
31    DeveloperSupportServer,
32    /// Is able to be discovered in the directory.
33    Discoverable,
34    /// Is able to be featured in the directory.
35    Featurable,
36    /// Invites have been paused, this prevents new users from joining.
37    InvitesDisabled,
38    /// Has access to set an invite splash background.
39    InviteSplash,
40    /// Has enabled membership screening.
41    MemberVerificationGateEnabled,
42    /// Has increased custom sticker slots.
43    MoreStickers,
44    /// Has access to create news channels.
45    News,
46    /// Is partnered.
47    Partnered,
48    /// Can be previewed before joining via membership screening or the directory.
49    PreviewEnabled,
50    /// Has access to create private threads.
51    PrivateThreads,
52    /// Guild has enabled requiring admin to prune members.
53    ///
54    /// If this feature is enabled then the
55    /// [`ADMINISTRATOR`][`Permissions::ADMINISTRATOR`] guild permission is
56    /// required for guild prune operations; otherwise, only the
57    /// [`MANAGE_GUILD`][`Permissions::MANAGE_GUILD`] and
58    /// [`KICK_MEMBERS`][`Permissions::KICK_MEMBERS`] permissions are required.
59    ///
60    /// [`Permissions::ADMINISTRATOR`]: super::Permissions::ADMINISTRATOR
61    /// [`Permissions::KICK_MEMBERS`]: super::Permissions::KICK_MEMBERS
62    /// [`Permissions::MANAGE_GUILD`]: super::Permissions::MANAGE_GUILD
63    PruneRequiresAdmin,
64    /// Guild has disabled alerts for join raids in the configured safety alerts channel.
65    RaidAlertsDisabled,
66    /// Is able to set role icons.
67    RoleIcons,
68    /// Guild has role subscriptions that can be purchased.
69    RoleSubscriptionsAvailableForPurchase,
70    /// Guild has enabled role subscriptions.
71    RoleSubscriptionsEnabled,
72    /// Has enabled ticketed events.
73    TicketedEventsEnabled,
74    /// Has access to set a vanity URL.
75    VanityUrl,
76    /// Is verified.
77    Verified,
78    /// Has access to set 384kps bitrate in voice (previously VIP voice servers).
79    VipRegions,
80    /// Has enabled the welcome screen.
81    WelcomeScreenEnabled,
82    /// Variant value is unknown to the library.
83    Unknown(String),
84}
85
86impl From<GuildFeature> for Cow<'static, str> {
87    fn from(value: GuildFeature) -> Self {
88        match value {
89            GuildFeature::AnimatedBanner => "ANIMATED_BANNER".into(),
90            GuildFeature::AnimatedIcon => "ANIMATED_ICON".into(),
91            GuildFeature::AutoModeration => "AUTO_MODERATION".into(),
92            GuildFeature::Banner => "BANNER".into(),
93            GuildFeature::Community => "COMMUNITY".into(),
94            GuildFeature::CreatorMonetizableProvisional => "CREATOR_MONETIZABLE_PROVISIONAL".into(),
95            GuildFeature::CreatorStorePage => "CREATOR_STORE_PAGE".into(),
96            GuildFeature::DeveloperSupportServer => "DEVELOPER_SUPPORT_SERVER".into(),
97            GuildFeature::Discoverable => "DISCOVERABLE".into(),
98            GuildFeature::Featurable => "FEATURABLE".into(),
99            GuildFeature::InvitesDisabled => "INVITES_DISABLED".into(),
100            GuildFeature::InviteSplash => "INVITE_SPLASH".into(),
101            GuildFeature::MemberVerificationGateEnabled => {
102                "MEMBER_VERIFICATION_GATE_ENABLED".into()
103            }
104            GuildFeature::MoreStickers => "MORE_STICKERS".into(),
105            GuildFeature::News => "NEWS".into(),
106            GuildFeature::Partnered => "PARTNERED".into(),
107            GuildFeature::PreviewEnabled => "PREVIEW_ENABLED".into(),
108            GuildFeature::PrivateThreads => "PRIVATE_THREADS".into(),
109            GuildFeature::PruneRequiresAdmin => "PRUNE_REQUIRES_ADMIN".into(),
110            GuildFeature::RaidAlertsDisabled => "RAID_ALERTS_DISABLED".into(),
111            GuildFeature::RoleIcons => "ROLE_ICONS".into(),
112            GuildFeature::RoleSubscriptionsAvailableForPurchase => {
113                "ROLE_SUBSCRIPTIONS_AVAILABLE_FOR_PURCHASE".into()
114            }
115            GuildFeature::RoleSubscriptionsEnabled => "ROLE_SUBSCRIPTIONS_ENABLED".into(),
116            GuildFeature::TicketedEventsEnabled => "TICKETED_EVENTS_ENABLED".into(),
117            GuildFeature::VanityUrl => "VANITY_URL".into(),
118            GuildFeature::Verified => "VERIFIED".into(),
119            GuildFeature::VipRegions => "VIP_REGIONS".into(),
120            GuildFeature::WelcomeScreenEnabled => "WELCOME_SCREEN_ENABLED".into(),
121            GuildFeature::Unknown(unknown) => unknown.into(),
122        }
123    }
124}
125
126impl From<String> for GuildFeature {
127    fn from(value: String) -> Self {
128        match value.as_str() {
129            "ANIMATED_BANNER" => Self::AnimatedBanner,
130            "ANIMATED_ICON" => Self::AnimatedIcon,
131            "AUTO_MODERATION" => Self::AutoModeration,
132            "BANNER" => Self::Banner,
133            "COMMUNITY" => Self::Community,
134            "CREATOR_MONETIZABLE_PROVISIONAL" => GuildFeature::CreatorMonetizableProvisional,
135            "CREATOR_STORE_PAGE" => GuildFeature::CreatorStorePage,
136            "DEVELOPER_SUPPORT_SERVER" => Self::DeveloperSupportServer,
137            "DISCOVERABLE" => Self::Discoverable,
138            "FEATURABLE" => Self::Featurable,
139            "INVITES_DISABLED" => Self::InvitesDisabled,
140            "INVITE_SPLASH" => Self::InviteSplash,
141            "MEMBER_VERIFICATION_GATE_ENABLED" => Self::MemberVerificationGateEnabled,
142            "MORE_STICKERS" => Self::MoreStickers,
143            "NEWS" => Self::News,
144            "PARTNERED" => Self::Partnered,
145            "PREVIEW_ENABLED" => Self::PreviewEnabled,
146            "PRIVATE_THREADS" => Self::PrivateThreads,
147            "PRUNE_REQUIRES_ADMIN" => Self::PruneRequiresAdmin,
148            "RAID_ALERTS_DISABLED" => Self::RaidAlertsDisabled,
149            "ROLE_ICONS" => Self::RoleIcons,
150            "ROLE_SUBSCRIPTIONS_AVAILABLE_FOR_PURCHASE" => {
151                GuildFeature::RoleSubscriptionsAvailableForPurchase
152            }
153            "ROLE_SUBSCRIPTIONS_ENABLED" => GuildFeature::RoleSubscriptionsEnabled,
154            "TICKETED_EVENTS_ENABLED" => Self::TicketedEventsEnabled,
155            "VANITY_URL" => Self::VanityUrl,
156            "VERIFIED" => Self::Verified,
157            "VIP_REGIONS" => Self::VipRegions,
158            "WELCOME_SCREEN_ENABLED" => Self::WelcomeScreenEnabled,
159            _ => Self::Unknown(value),
160        }
161    }
162}
163
164#[cfg(test)]
165mod tests {
166    use super::GuildFeature;
167    use serde_test::Token;
168
169    #[test]
170    fn variants() {
171        serde_test::assert_tokens(
172            &GuildFeature::AnimatedBanner,
173            &[Token::Str("ANIMATED_BANNER")],
174        );
175        serde_test::assert_tokens(&GuildFeature::AnimatedIcon, &[Token::Str("ANIMATED_ICON")]);
176        serde_test::assert_tokens(
177            &GuildFeature::AutoModeration,
178            &[Token::Str("AUTO_MODERATION")],
179        );
180        serde_test::assert_tokens(&GuildFeature::Banner, &[Token::Str("BANNER")]);
181        serde_test::assert_tokens(&GuildFeature::Community, &[Token::Str("COMMUNITY")]);
182        serde_test::assert_tokens(
183            &GuildFeature::CreatorMonetizableProvisional,
184            &[Token::Str("CREATOR_MONETIZABLE_PROVISIONAL")],
185        );
186        serde_test::assert_tokens(
187            &GuildFeature::CreatorStorePage,
188            &[Token::Str("CREATOR_STORE_PAGE")],
189        );
190        serde_test::assert_tokens(
191            &GuildFeature::DeveloperSupportServer,
192            &[Token::Str("DEVELOPER_SUPPORT_SERVER")],
193        );
194        serde_test::assert_tokens(&GuildFeature::Discoverable, &[Token::Str("DISCOVERABLE")]);
195        serde_test::assert_tokens(&GuildFeature::Featurable, &[Token::Str("FEATURABLE")]);
196        serde_test::assert_tokens(
197            &GuildFeature::InvitesDisabled,
198            &[Token::Str("INVITES_DISABLED")],
199        );
200        serde_test::assert_tokens(&GuildFeature::InviteSplash, &[Token::Str("INVITE_SPLASH")]);
201        serde_test::assert_tokens(
202            &GuildFeature::MemberVerificationGateEnabled,
203            &[Token::Str("MEMBER_VERIFICATION_GATE_ENABLED")],
204        );
205        serde_test::assert_tokens(&GuildFeature::MoreStickers, &[Token::Str("MORE_STICKERS")]);
206        serde_test::assert_tokens(&GuildFeature::News, &[Token::Str("NEWS")]);
207        serde_test::assert_tokens(&GuildFeature::Partnered, &[Token::Str("PARTNERED")]);
208        serde_test::assert_tokens(
209            &GuildFeature::PreviewEnabled,
210            &[Token::Str("PREVIEW_ENABLED")],
211        );
212        serde_test::assert_tokens(
213            &GuildFeature::PrivateThreads,
214            &[Token::Str("PRIVATE_THREADS")],
215        );
216        serde_test::assert_tokens(
217            &GuildFeature::PruneRequiresAdmin,
218            &[Token::Str("PRUNE_REQUIRES_ADMIN")],
219        );
220        serde_test::assert_tokens(&GuildFeature::RoleIcons, &[Token::Str("ROLE_ICONS")]);
221        serde_test::assert_tokens(
222            &GuildFeature::RoleSubscriptionsAvailableForPurchase,
223            &[Token::Str("ROLE_SUBSCRIPTIONS_AVAILABLE_FOR_PURCHASE")],
224        );
225        serde_test::assert_tokens(
226            &GuildFeature::RoleSubscriptionsEnabled,
227            &[Token::Str("ROLE_SUBSCRIPTIONS_ENABLED")],
228        );
229        serde_test::assert_tokens(
230            &GuildFeature::TicketedEventsEnabled,
231            &[Token::Str("TICKETED_EVENTS_ENABLED")],
232        );
233        serde_test::assert_tokens(&GuildFeature::VanityUrl, &[Token::Str("VANITY_URL")]);
234        serde_test::assert_tokens(&GuildFeature::Verified, &[Token::Str("VERIFIED")]);
235        serde_test::assert_tokens(&GuildFeature::VipRegions, &[Token::Str("VIP_REGIONS")]);
236        serde_test::assert_tokens(
237            &GuildFeature::WelcomeScreenEnabled,
238            &[Token::Str("WELCOME_SCREEN_ENABLED")],
239        );
240        serde_test::assert_tokens(
241            &GuildFeature::Unknown("UNKNOWN".to_owned()),
242            &[Token::Str("UNKNOWN")],
243        );
244    }
245}