From 65032e8bf56639130d4c9006b9492f78d65a294c Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Thu, 26 Oct 2023 00:29:49 +0800 Subject: [PATCH] feat(presence): allow more types and default to custom --- src/commands/presence.rs | 44 +++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/commands/presence.rs b/src/commands/presence.rs index f1e9ed6..fe461a0 100644 --- a/src/commands/presence.rs +++ b/src/commands/presence.rs @@ -5,9 +5,29 @@ use crate::Context; #[derive(poise::ChoiceParameter)] pub enum PresenceChoice { + Custom, Playing, Watching, - Custom, + Listening, + Competing, +} + +impl PresenceChoice { + fn make_activity(&self, content: &str) -> serenity::ActivityData { + match self { + Self::Custom => serenity::ActivityData::custom(content), + Self::Playing => serenity::ActivityData::playing(content), + Self::Watching => serenity::ActivityData::watching(content), + Self::Listening => serenity::ActivityData::listening(content), + Self::Competing => serenity::ActivityData::competing(content), + } + } +} + +impl Default for PresenceChoice { + fn default() -> Self { + Self::Custom + } } impl std::fmt::Display for PresenceChoice { @@ -16,22 +36,16 @@ impl std::fmt::Display for PresenceChoice { f, "{}", match self { - PresenceChoice::Playing => "Playing".to_owned(), - PresenceChoice::Watching => "Watching".to_owned(), - PresenceChoice::Custom => "Custom".to_owned(), + Self::Custom => "Custom".to_owned(), + Self::Playing => "Playing".to_owned(), + Self::Watching => "Watching".to_owned(), + Self::Listening => "Listening".to_owned(), + Self::Competing => "Competing".to_owned(), } ) } } -fn make_activity(content: &str, presence_type: &PresenceChoice) -> serenity::ActivityData { - match presence_type { - PresenceChoice::Playing => serenity::ActivityData::playing(content), - PresenceChoice::Watching => serenity::ActivityData::watching(content), - PresenceChoice::Custom => serenity::ActivityData::custom(content), - } -} - /// Modify the Discord presence shown by the bot #[poise::command( slash_command, @@ -45,10 +59,12 @@ pub async fn presence( #[rename = "type"] #[description = "Type of presence"] - type_: PresenceChoice, + type_: Option, ) -> Result<()> { + let type_ = type_.unwrap_or_default(); + ctx.serenity_context().set_presence( - Some(make_activity(&content, &type_)), + Some(type_.make_activity(&content)), serenity::OnlineStatus::Online, );