Make any errors not crash the entire application
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julius 2022-06-24 22:43:08 +02:00
parent 1ab7e4bffe
commit fca4872a73
Signed by: j00lz
GPG key ID: AF241B0AA237BBA2

View file

@ -2,7 +2,8 @@ use serenity::{
client::{Context, EventHandler}, client::{Context, EventHandler},
model::{ model::{
channel::Reaction, channel::Reaction,
gateway::{Activity, Ready}, id::{ChannelId, MessageId}, gateway::{Activity, Ready},
id::{ChannelId, MessageId},
}, },
prelude::GatewayIntents, prelude::GatewayIntents,
Client, Client,
@ -20,48 +21,50 @@ impl EventHandler for Handler {
} }
async fn reaction_add(&self, c: Context, r: Reaction) { async fn reaction_add(&self, c: Context, r: Reaction) {
if let Err(e) = handle_react(c, r).await {
println!("Error: {:?}", e);
}
}
async fn reaction_remove(&self, c: Context, r: Reaction) {
if let Err(e) = handle_react(c, r).await {
println!("Error: {:?}", e);
}
}
async fn reaction_remove_all(&self, c: Context, c_id: ChannelId, m_id: MessageId) {
let msg = match c.http.get_message(c_id.0, m_id.0).await {
Ok(m) => m,
Err(e) => {
println!("Error: {:?}", e);
return;
}
};
if msg.pinned {
if let Err(e) = msg.unpin(&c.http).await {
println!("Error: {:?}", e);
}
}
}
}
async fn handle_react(c: Context, r: Reaction) -> Result<(), serenity::Error> {
let emoji = r.emoji.as_data(); let emoji = r.emoji.as_data();
if emoji == "📌" { if emoji == "📌" {
let msg = r.message(c.http.clone()).await.expect("Could not find message"); let msg = r.message(c.http.clone()).await?;
for r in &msg.reactions { for r in &msg.reactions {
let emoji = r.reaction_type.as_data(); let emoji = r.reaction_type.as_data();
if emoji == "📌" { if emoji == "📌" {
let count = r.count; let count = r.count;
if count >= 5 { if count >= 5 {
msg.pin(c.http.clone()).await.expect("Could not pin message"); msg.pin(c.http.clone()).await?;
} else if count < 3 {
msg.unpin(&c.http).await?;
} }
} }
} }
} }
} Ok(())
async fn reaction_remove(&self, c: Context, r: Reaction) {
let emoji = r.emoji.as_data();
if emoji == "📌" {
let msg = r.message(&c.http).await.expect("Could not find message");
for r in &msg.reactions {
let emoji = r.reaction_type.as_data();
if emoji == "📌" {
let count = r.count;
if count < 3 {
msg.unpin(&c.http).await.expect("Could not unpin message");
}
}
}
}
}
async fn reaction_remove_all(
&self,
c: Context,
c_id: ChannelId,
m_id: MessageId,
) {
let msg = c.http.get_message(c_id.0, m_id.0).await.expect("Could not find message");
if msg.pinned {
msg.unpin(&c.http).await.expect("Could not unpin message");
}
}
} }
#[tokio::main] #[tokio::main]