Skip to main content

zaun_bot/
body_mentions.rs

1//! [`Reply`]'s mentions: who the message notifies. Each id is kept once; more than 50 members or
2//! 20 roles is refused (`BAD_REQUEST`). The host drops `@everyone` and roles that are not
3//! mentionable unless the bot has `MENTION_EVERYONE`, and members not in the community.
4
5use crate::Reply;
6use crate::mention::{self, push_unique};
7use crate::types::{Id, Mentions};
8
9impl Reply {
10    fn mentions_mut(&mut self) -> &mut Mentions {
11        self.0.mentions.get_or_insert_with(mention::none)
12    }
13
14    /// Notifies member `id` (write [`mention::member`] in the text to show where).
15    #[must_use]
16    pub fn mention_member(mut self, id: impl Into<Id>) -> Self {
17        push_unique(&mut self.mentions_mut().members, &id.into(), usize::MAX);
18        self
19    }
20
21    /// Notifies these members.
22    #[must_use]
23    pub fn mention_members(self, ids: impl IntoIterator<Item = Id>) -> Self {
24        ids.into_iter().fold(self, Self::mention_member)
25    }
26
27    /// Notifies the members with role `id` (write [`mention::role`] in the text).
28    #[must_use]
29    pub fn mention_role(mut self, id: impl Into<Id>) -> Self {
30        push_unique(&mut self.mentions_mut().roles, &id.into(), usize::MAX);
31        self
32    }
33
34    /// Notifies the members with these roles.
35    #[must_use]
36    pub fn mention_roles(self, ids: impl IntoIterator<Item = Id>) -> Self {
37        ids.into_iter().fold(self, Self::mention_role)
38    }
39
40    /// Notifies everyone who can see the channel (needs `MENTION_EVERYONE`).
41    #[must_use]
42    pub fn mention_everyone(mut self) -> Self {
43        self.mentions_mut().everyone = true;
44        self
45    }
46
47    /// Sets the mentions to exactly `mentions` (`mention::none()`: nobody; on an edit, clears
48    /// them).
49    #[must_use]
50    pub fn mentions(mut self, mentions: Mentions) -> Self {
51        self.0.mentions = Some(mention::none());
52        mention::merge(self.mentions_mut(), mentions);
53        self
54    }
55
56    /// Adds the mentions the content shows (`<@id>`, `<@&id>`, `@everyone`; see
57    /// [`mention::parse`]). Call it after [`content`](Self::content), and only for text the
58    /// bot wrote itself, never for text a member gave it.
59    #[must_use]
60    pub fn mentions_from_content(mut self) -> Self {
61        let found = mention::parse(self.0.content.as_deref().unwrap_or_default());
62        mention::merge(self.mentions_mut(), found);
63        self
64    }
65}