zaun_client/bots/
interaction.rs1use zaun_community_protocol::command::interaction::{
4 Followup, OriginalDelete, OriginalEdit, Replied, Respond,
5};
6use zaun_community_protocol::command::object::{
7 Interaction as Data, InteractionData, InteractionType, MessageBody, OptionValue,
8};
9use zaun_community_protocol::ids::InteractionId;
10
11use super::Response;
12use crate::error::Result;
13use crate::objects::{Channel, Community, Member};
14
15#[derive(Debug, Clone)]
19pub struct Interaction {
20 pub(crate) community: Community,
21 pub(crate) data: Box<Data>,
22}
23
24impl Interaction {
25 #[must_use]
27 pub fn id(&self) -> InteractionId {
28 self.data.interaction_id
29 }
30
31 #[must_use]
33 pub fn kind(&self) -> InteractionType {
34 self.data.kind
35 }
36
37 #[must_use]
39 pub fn data(&self) -> &Data {
40 &self.data
41 }
42
43 #[must_use]
45 pub fn channel(&self) -> Channel {
46 self.community.channel(self.data.channel_id)
47 }
48
49 #[must_use]
51 pub fn member(&self) -> Member {
52 self.community.member(self.data.member.member_id)
53 }
54
55 #[must_use]
57 pub fn command_name(&self) -> Option<&str> {
58 match &self.data.data {
59 InteractionData::Command { name, .. } => Some(name),
60 InteractionData::Component { .. } | InteractionData::Modal { .. } => None,
61 }
62 }
63
64 #[must_use]
66 pub fn options(&self) -> &[OptionValue] {
67 match &self.data.data {
68 InteractionData::Command { options, .. } => options,
69 InteractionData::Component { .. } | InteractionData::Modal { .. } => &[],
70 }
71 }
72
73 #[must_use]
75 pub fn custom_id(&self) -> Option<&str> {
76 match &self.data.data {
77 InteractionData::Component { custom_id, .. }
78 | InteractionData::Modal { custom_id, .. } => Some(custom_id),
79 InteractionData::Command { .. } => None,
80 }
81 }
82
83 #[must_use]
85 pub fn field(&self, custom_id: &str) -> Option<&str> {
86 match &self.data.data {
87 InteractionData::Modal { components, .. } => components
88 .iter()
89 .find(|c| c.custom_id == custom_id)
90 .and_then(|c| c.value.as_deref()),
91 InteractionData::Command { .. } | InteractionData::Component { .. } => None,
92 }
93 }
94
95 pub fn respond(&self, response: Response) -> Result<Replied> {
100 let body = Respond {
101 interaction_id: self.id(),
102 kind: response.kind,
103 data: response.data,
104 };
105 self.community.client.command(self.community.id, None, body)
106 }
107
108 pub fn reply(&self, content: impl Into<String>) -> Result<Replied> {
113 self.respond(Response::text(content))
114 }
115
116 pub fn reply_ephemeral(&self, content: impl Into<String>) -> Result<Replied> {
121 self.respond(Response::ephemeral(super::respond::text(content)))
122 }
123
124 pub fn followup(&self, body: MessageBody) -> Result<Replied> {
129 let body = Followup {
130 interaction_id: self.id(),
131 body,
132 };
133 self.community.client.command(self.community.id, None, body)
134 }
135
136 pub fn edit_original(&self, body: MessageBody) -> Result<()> {
141 let body = OriginalEdit {
142 interaction_id: self.id(),
143 body,
144 };
145 self.community
146 .client
147 .command(self.community.id, None, body)
148 .map(|_| ())
149 }
150
151 pub fn delete_original(&self) -> Result<()> {
156 let body = OriginalDelete {
157 interaction_id: self.id(),
158 };
159 self.community
160 .client
161 .command(self.community.id, None, body)
162 .map(|_| ())
163 }
164}