Skip to main content

zaun_bot/
presence.rs

1//! The bot's presence and custom status, shown to every member next to its name:
2//! "Listening to lofi radio", "🎵 Type /play".
3//!
4//! ```no_run
5//! use zaun_bot::presence::{self, Activity, Status};
6//!
7//! presence::set(Status::Online, Activity::listening("lofi radio").state("ChilledCow"));
8//! presence::set_status_text("Type /play").ok();
9//! ```
10//!
11//! The host shows a change at most every 3 s; a later call within that time replaces an
12//! earlier one that has not gone out yet. A bot is `online`, without an activity, when it
13//! starts.
14
15use crate::bindings::zaun::bot::presence as wit;
16use crate::types::Activity as WitActivity;
17use crate::types::HostError;
18
19pub use crate::types::ActivityType;
20pub use wit::Status;
21
22/// What the bot is doing: build one with [`Activity::playing`], [`Activity::listening`], …
23#[derive(Debug, Clone)]
24pub struct Activity(WitActivity);
25
26impl Activity {
27    fn new(kind: ActivityType, name: impl Into<String>) -> Self {
28        Self(WitActivity {
29            kind,
30            name: name.into(),
31            details: None,
32            state: None,
33            url: None,
34            started_at: None,
35            emoji: None,
36        })
37    }
38
39    /// "Playing {name}".
40    pub fn playing(name: impl Into<String>) -> Self {
41        Self::new(ActivityType::Playing, name)
42    }
43
44    /// "Streaming {name}", with an `https://` link members may open (after confirming).
45    pub fn streaming(name: impl Into<String>, url: impl Into<String>) -> Self {
46        let mut a = Self::new(ActivityType::Streaming, name);
47        a.0.url = Some(url.into());
48        a
49    }
50
51    /// "Listening to {name}".
52    pub fn listening(name: impl Into<String>) -> Self {
53        Self::new(ActivityType::Listening, name)
54    }
55
56    /// "Watching {name}".
57    pub fn watching(name: impl Into<String>) -> Self {
58        Self::new(ActivityType::Watching, name)
59    }
60
61    /// "Competing in {name}".
62    pub fn competing(name: impl Into<String>) -> Self {
63        Self::new(ActivityType::Competing, name)
64    }
65
66    /// Free text, with an optional emoji ([`Activity::emoji`]).
67    pub fn custom(text: impl Into<String>) -> Self {
68        Self::new(ActivityType::Custom, text)
69    }
70
71    /// A second line, e.g. the track (≤ 128 characters).
72    #[must_use]
73    pub fn details(mut self, details: impl Into<String>) -> Self {
74        self.0.details = Some(details.into());
75        self
76    }
77
78    /// A third line, e.g. the artist (≤ 128 characters).
79    #[must_use]
80    pub fn state(mut self, state: impl Into<String>) -> Self {
81        self.0.state = Some(state.into());
82        self
83    }
84
85    /// When it started, ms since the epoch (clients show the elapsed time).
86    #[must_use]
87    pub fn started_at(mut self, ms: u64) -> Self {
88        self.0.started_at = Some(ms);
89        self
90    }
91
92    /// A Unicode emoji, for [`Activity::custom`] only.
93    #[must_use]
94    pub fn emoji(mut self, emoji: impl Into<String>) -> Self {
95        self.0.emoji = Some(emoji.into());
96        self
97    }
98}
99
100/// Sets the bot's status and activity (`None`: no activity).
101pub fn set(status: Status, activity: impl Into<Option<Activity>>) {
102    wit::set(status, activity.into().as_ref().map(|a| &a.0));
103}
104
105/// Sets the custom status text, kept until changed.
106///
107/// # Errors
108/// The host's refusal (too long, rate limited).
109pub fn set_status_text(text: impl Into<String>) -> Result<(), HostError> {
110    wit::set_status_text(Some(&text.into()), None, None)
111}
112
113/// Sets the custom status text and emoji, cleared by the host at `expires_at` (ms) if given.
114///
115/// # Errors
116/// The host's refusal.
117pub fn set_status(
118    text: Option<&str>,
119    emoji: Option<&str>,
120    expires_at: Option<u64>,
121) -> Result<(), HostError> {
122    wit::set_status_text(text, emoji, expires_at)
123}
124
125/// Clears the custom status.
126///
127/// # Errors
128/// The host's refusal.
129pub fn clear_status() -> Result<(), HostError> {
130    wit::set_status_text(None, None, None)
131}