1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use nanoserde::{DeJson, SerJson};
use crate::utils;
use super::{channel::Channel, emoji::Emoji, guild::Member, message_response::Message, user::User};
/// Represents a response for a reaction.
#[derive(DeJson, SerJson, Clone, Debug)]
pub struct ReactionResponse {
/// The reaction data.
#[nserde(rename = "d")]
pub data: Reaction,
}
/// Represents a reaction in Discord.
#[derive(DeJson, SerJson, Debug, Clone)]
pub struct Reaction {
/// The ID of the user who reacted.
pub user_id: String,
/// The ID of the message that was reacted to.
pub message_id: String,
/// The member who reacted.
#[nserde(default)]
pub member: Option<Member>,
/// The emoji used for the reaction.
pub emoji: Emoji,
/// The ID of the channel where the reaction occurred.
pub channel_id: String,
/// Whether the reaction is a burst reaction.
pub burst: bool,
/// The ID of the guild where the reaction occurred.
#[nserde(default)]
pub guild_id: Option<String>,
}
impl Reaction {
/// Fetches the channel where the reaction occurred.
///
/// # Examples
///
/// ```
/// let channel = reaction.get_channel().await?;
/// ```
pub async fn get_channel(&self) -> Result<Channel, Box<dyn std::error::Error>> {
utils::fetch_channel(&self.channel_id).await
}
/// Fetches the user who reacted.
///
/// # Examples
///
/// ```
/// let user = reaction.get_user().await?;
/// ```
pub async fn get_user(&self) -> Result<User, Box<dyn std::error::Error>> {
utils::fetch_user(&self.user_id).await
}
/// Fetches the message that was reacted to.
///
/// # Examples
///
/// ```
/// let message = reaction.get_message().await?;
/// ```
pub async fn get_message(&self) -> Result<Message, Box<dyn std::error::Error>> {
utils::fetch_message(&self.channel_id, &self.message_id).await
}
/// Removes the reaction.
///
/// # Examples
///
/// ```
/// reaction.remove_reaction().await;
/// ```
pub async fn remove_reaction(&self) {
utils::remove_reaction(
&self.channel_id,
&self.message_id,
&self.user_id,
&if let Some(ref id) = self.emoji.id {
format!("{name}:{id}", name = self.emoji.name)
} else {
self.emoji.name.clone()
},
)
.await;
}
}