From 5f92ea154525f988de6b533b2d08d2c9970bb2d4 Mon Sep 17 00:00:00 2001 From: xpple Date: Mon, 5 Jun 2023 17:07:55 +0200 Subject: [PATCH 1/7] Add octal counter channel game --- src/config.rs | 1 + src/discord_bot/mod.rs | 76 +++++++++++++++++++++----------- src/discord_bot/octal_counter.rs | 39 ++++++++++++++++ 3 files changed, 90 insertions(+), 26 deletions(-) create mode 100644 src/discord_bot/octal_counter.rs diff --git a/src/config.rs b/src/config.rs index 6767692..7560139 100644 --- a/src/config.rs +++ b/src/config.rs @@ -27,6 +27,7 @@ pub struct Config { pub use_https: bool, pub application_channel: ChannelId, pub application_token: String, + pub octal_counter_channel: Option, pub pterodactyl_domain: String, pub pterodactyl_server_ids: Vec, pub pterodactyl_api_key: String, diff --git a/src/discord_bot/mod.rs b/src/discord_bot/mod.rs index e66b665..0d1330b 100644 --- a/src/discord_bot/mod.rs +++ b/src/discord_bot/mod.rs @@ -4,6 +4,7 @@ mod commands; mod counter; mod guild_storage; mod mood; +mod octal_counter; mod permanent_latest; mod reaction_role_toggle; mod role; @@ -167,12 +168,15 @@ impl EventHandler for Handler { enum MessageHandling<'a> { Command(&'a str), IncCounter(&'a str), + OctalCounter, PermanentLatest, SimpleWords, } let message_handling = { - if config::get().simple_words_channel == Some(new_message.channel_id) { + if config::get().octal_counter_channel == Some(new_message.channel_id) { + MessageHandling::OctalCounter + } else if config::get().simple_words_channel == Some(new_message.channel_id) { MessageHandling::SimpleWords } else if new_message.author.bot { return; @@ -208,6 +212,17 @@ impl EventHandler for Handler { MessageHandling::IncCounter(counter) => { counter::inc_counter(counter, guild_id, ctx, &new_message).await } + MessageHandling::OctalCounter => { + octal_counter::on_message( + ctx, + !new_message.attachments.is_empty(), + &new_message.content, + &new_message.author, + new_message.channel_id, + new_message.id, + ) + .await + } MessageHandling::PermanentLatest => { permanent_latest::on_message(guild_id, ctx, &new_message).await } @@ -238,32 +253,41 @@ impl EventHandler for Handler { _new: Option, event: MessageUpdateEvent, ) { - if config::get().simple_words_channel != Some(event.channel_id) { - return; + if config::get().simple_words_channel == Some(event.channel_id) { + let Some(content) = event.content else { return; }; + let Some(author) = event.author else { return; }; + tokio::runtime::Handle::current().spawn(async move { + if let Err(err) = simple_words::on_message( + ctx, + event + .attachments + .as_ref() + .map(|attachments| attachments.is_empty()) + == Some(false), + &content, + &author, + event.channel_id, + event.id, + ) + .await + { + warn!( + "Error processing message edit from \"{}\" (ID {}): {}", + author.name, author.id, err + ); + } + }); + } else if config::get().octal_counter_channel == Some(event.channel_id) { + let Some(author) = event.author else { return; }; + tokio::runtime::Handle::current().spawn(async move { + if let Err(err) = event.channel_id.delete_message(&ctx, event.id).await { + warn!( + "Error processing message edit from \"{}\" (ID {}): {}", + author.name, author.id, err + ); + } + }); } - let Some(content) = event.content else { return; }; - let Some(author) = event.author else { return; }; - tokio::runtime::Handle::current().spawn(async move { - if let Err(err) = simple_words::on_message( - ctx, - event - .attachments - .as_ref() - .map(|attachments| attachments.is_empty()) - == Some(false), - &content, - &author, - event.channel_id, - event.id, - ) - .await - { - warn!( - "Error processing message edit from \"{}\" (ID {}): {}", - author.name, author.id, err - ); - } - }); } async fn reaction_add(&self, ctx: Context, reaction: Reaction) { diff --git a/src/discord_bot/octal_counter.rs b/src/discord_bot/octal_counter.rs new file mode 100644 index 0000000..181bf5a --- /dev/null +++ b/src/discord_bot/octal_counter.rs @@ -0,0 +1,39 @@ +use futures::StreamExt; +use serenity::client::Context; +use serenity::model::id::{ChannelId, MessageId}; +use serenity::model::user::User; + +pub(crate) async fn on_message( + ctx: Context, + has_attachments: bool, + content: &str, + author: &User, + channel_id: ChannelId, + message_id: MessageId, +) -> Result<(), crate::Error> { + if has_attachments { + channel_id.delete_message(&ctx, message_id).await?; + return Ok(()); + } + + let Ok(next_counter) = i32::from_str_radix(content, 8) else { + channel_id.delete_message(&ctx, message_id).await?; + return Ok(()); + }; + + let Some(Ok(latest_message)) = channel_id.messages_iter(&ctx).boxed().next().await else { + return Ok(()); + }; + if *author == latest_message.author { + channel_id.delete_message(&ctx, message_id).await?; + return Ok(()); + } + let previous_counter = i32::from_str_radix(&latest_message.content, 8).unwrap(); + + if next_counter != previous_counter + 1 { + channel_id.delete_message(&ctx, message_id).await?; + return Ok(()); + } + + Ok(()) +} From ad716a8c458e4975d02998e797a2ddbd7ec55d87 Mon Sep 17 00:00:00 2001 From: xpple Date: Mon, 5 Jun 2023 19:31:15 +0200 Subject: [PATCH 2/7] Store octal counter data in the guild storage --- src/discord_bot/guild_storage.rs | 6 ++++++ src/discord_bot/mod.rs | 3 ++- src/discord_bot/octal_counter.rs | 17 ++++++++--------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/discord_bot/guild_storage.rs b/src/discord_bot/guild_storage.rs index 7461012..5aedaa2 100644 --- a/src/discord_bot/guild_storage.rs +++ b/src/discord_bot/guild_storage.rs @@ -44,6 +44,10 @@ pub struct GuildStorage { pub users_sent_to_support: HashSet, #[serde(default)] pub counters: HashMap, + #[serde(default)] + pub octal_counter: i32, + #[serde(default)] + pub octal_counter_latest_user: Option } impl Default for GuildStorage { @@ -61,6 +65,8 @@ impl Default for GuildStorage { send_to_support_leaderboard: HashMap::new(), users_sent_to_support: HashSet::new(), counters: HashMap::new(), + octal_counter: 0, + octal_counter_latest_user: None } } } diff --git a/src/discord_bot/mod.rs b/src/discord_bot/mod.rs index 0d1330b..8c6945f 100644 --- a/src/discord_bot/mod.rs +++ b/src/discord_bot/mod.rs @@ -214,6 +214,7 @@ impl EventHandler for Handler { } MessageHandling::OctalCounter => { octal_counter::on_message( + guild_id, ctx, !new_message.attachments.is_empty(), &new_message.content, @@ -269,7 +270,7 @@ impl EventHandler for Handler { event.channel_id, event.id, ) - .await + .await { warn!( "Error processing message edit from \"{}\" (ID {}): {}", diff --git a/src/discord_bot/octal_counter.rs b/src/discord_bot/octal_counter.rs index 181bf5a..a714799 100644 --- a/src/discord_bot/octal_counter.rs +++ b/src/discord_bot/octal_counter.rs @@ -1,9 +1,10 @@ -use futures::StreamExt; use serenity::client::Context; -use serenity::model::id::{ChannelId, MessageId}; +use serenity::model::id::{ChannelId, GuildId, MessageId}; use serenity::model::user::User; +use crate::discord_bot::guild_storage::GuildStorage; pub(crate) async fn on_message( + guild_id: GuildId, ctx: Context, has_attachments: bool, content: &str, @@ -21,19 +22,17 @@ pub(crate) async fn on_message( return Ok(()); }; - let Some(Ok(latest_message)) = channel_id.messages_iter(&ctx).boxed().next().await else { - return Ok(()); - }; - if *author == latest_message.author { + let mut storage = GuildStorage::get_mut(guild_id).await; + if storage.octal_counter_latest_user == Some(author.id) { channel_id.delete_message(&ctx, message_id).await?; return Ok(()); } - let previous_counter = i32::from_str_radix(&latest_message.content, 8).unwrap(); - - if next_counter != previous_counter + 1 { + if next_counter != storage.octal_counter + 1 { channel_id.delete_message(&ctx, message_id).await?; return Ok(()); } + storage.octal_counter = next_counter; + storage.save().await; Ok(()) } From ee05924d1677f5d40b85ff76d91348d40d5b80c0 Mon Sep 17 00:00:00 2001 From: xpple Date: Mon, 5 Jun 2023 19:34:55 +0200 Subject: [PATCH 3/7] Update user --- src/discord_bot/octal_counter.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/discord_bot/octal_counter.rs b/src/discord_bot/octal_counter.rs index a714799..1879ace 100644 --- a/src/discord_bot/octal_counter.rs +++ b/src/discord_bot/octal_counter.rs @@ -33,6 +33,7 @@ pub(crate) async fn on_message( } storage.octal_counter = next_counter; + storage.octal_counter_latest_user = Some(author.id); storage.save().await; Ok(()) } From a1eb2aecfb52a91b47bcf2bb27c7e16014609b8f Mon Sep 17 00:00:00 2001 From: xpple Date: Mon, 5 Jun 2023 21:40:51 +0200 Subject: [PATCH 4/7] Make the octal counting channel guild dependent --- src/config.rs | 1 - src/discord_bot/guild_storage.rs | 7 +++++-- src/discord_bot/mod.rs | 4 ++-- src/discord_bot/octal_counter.rs | 2 ++ 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index 7560139..6767692 100644 --- a/src/config.rs +++ b/src/config.rs @@ -27,7 +27,6 @@ pub struct Config { pub use_https: bool, pub application_channel: ChannelId, pub application_token: String, - pub octal_counter_channel: Option, pub pterodactyl_domain: String, pub pterodactyl_server_ids: Vec, pub pterodactyl_api_key: String, diff --git a/src/discord_bot/guild_storage.rs b/src/discord_bot/guild_storage.rs index 5aedaa2..9db942a 100644 --- a/src/discord_bot/guild_storage.rs +++ b/src/discord_bot/guild_storage.rs @@ -45,9 +45,11 @@ pub struct GuildStorage { #[serde(default)] pub counters: HashMap, #[serde(default)] + pub octal_counter_channel: Option, + #[serde(default)] pub octal_counter: i32, #[serde(default)] - pub octal_counter_latest_user: Option + pub octal_counter_latest_user: Option, } impl Default for GuildStorage { @@ -65,8 +67,9 @@ impl Default for GuildStorage { send_to_support_leaderboard: HashMap::new(), users_sent_to_support: HashSet::new(), counters: HashMap::new(), + octal_counter_channel: None, octal_counter: 0, - octal_counter_latest_user: None + octal_counter_latest_user: None, } } } diff --git a/src/discord_bot/mod.rs b/src/discord_bot/mod.rs index 8c6945f..50793f5 100644 --- a/src/discord_bot/mod.rs +++ b/src/discord_bot/mod.rs @@ -174,7 +174,7 @@ impl EventHandler for Handler { } let message_handling = { - if config::get().octal_counter_channel == Some(new_message.channel_id) { + if GuildStorage::get(guild_id).await.octal_counter_channel == Some(new_message.channel_id) { MessageHandling::OctalCounter } else if config::get().simple_words_channel == Some(new_message.channel_id) { MessageHandling::SimpleWords @@ -278,7 +278,7 @@ impl EventHandler for Handler { ); } }); - } else if config::get().octal_counter_channel == Some(event.channel_id) { + } else if GuildStorage::get(match event.guild_id { Some(i) => i, None => return }).await.octal_counter_channel == Some(event.channel_id) { let Some(author) = event.author else { return; }; tokio::runtime::Handle::current().spawn(async move { if let Err(err) = event.channel_id.delete_message(&ctx, event.id).await { diff --git a/src/discord_bot/octal_counter.rs b/src/discord_bot/octal_counter.rs index 1879ace..53ece6f 100644 --- a/src/discord_bot/octal_counter.rs +++ b/src/discord_bot/octal_counter.rs @@ -25,10 +25,12 @@ pub(crate) async fn on_message( let mut storage = GuildStorage::get_mut(guild_id).await; if storage.octal_counter_latest_user == Some(author.id) { channel_id.delete_message(&ctx, message_id).await?; + storage.discard(); return Ok(()); } if next_counter != storage.octal_counter + 1 { channel_id.delete_message(&ctx, message_id).await?; + storage.discard(); return Ok(()); } From af82ed2f349230de7cbc31aed2689849b4b5128c Mon Sep 17 00:00:00 2001 From: xpple Date: Tue, 6 Jun 2023 16:53:38 +0200 Subject: [PATCH 5/7] Put octal counter data inside struct --- src/discord_bot/guild_storage.rs | 11 +++-------- src/discord_bot/mod.rs | 4 ++-- src/discord_bot/octal_counter.rs | 18 +++++++++++++----- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/discord_bot/guild_storage.rs b/src/discord_bot/guild_storage.rs index 9db942a..a866001 100644 --- a/src/discord_bot/guild_storage.rs +++ b/src/discord_bot/guild_storage.rs @@ -1,4 +1,5 @@ use crate::discord_bot::chess::ChessState; +use crate::discord_bot::octal_counter::OctalCounterState; use crate::discord_bot::permanent_latest::PermanentLatestInfo; use crate::discord_bot::role::RoleData; use crate::discord_bot::roletoggle::RoleToggleInfo; @@ -45,11 +46,7 @@ pub struct GuildStorage { #[serde(default)] pub counters: HashMap, #[serde(default)] - pub octal_counter_channel: Option, - #[serde(default)] - pub octal_counter: i32, - #[serde(default)] - pub octal_counter_latest_user: Option, + pub octal_counter_state: OctalCounterState, } impl Default for GuildStorage { @@ -67,9 +64,7 @@ impl Default for GuildStorage { send_to_support_leaderboard: HashMap::new(), users_sent_to_support: HashSet::new(), counters: HashMap::new(), - octal_counter_channel: None, - octal_counter: 0, - octal_counter_latest_user: None, + octal_counter_state: OctalCounterState::default(), } } } diff --git a/src/discord_bot/mod.rs b/src/discord_bot/mod.rs index 50793f5..1ab5f29 100644 --- a/src/discord_bot/mod.rs +++ b/src/discord_bot/mod.rs @@ -174,7 +174,7 @@ impl EventHandler for Handler { } let message_handling = { - if GuildStorage::get(guild_id).await.octal_counter_channel == Some(new_message.channel_id) { + if GuildStorage::get(guild_id).await.octal_counter_state.octal_counter_channel == Some(new_message.channel_id) { MessageHandling::OctalCounter } else if config::get().simple_words_channel == Some(new_message.channel_id) { MessageHandling::SimpleWords @@ -278,7 +278,7 @@ impl EventHandler for Handler { ); } }); - } else if GuildStorage::get(match event.guild_id { Some(i) => i, None => return }).await.octal_counter_channel == Some(event.channel_id) { + } else if GuildStorage::get(match event.guild_id { Some(i) => i, None => return }).await.octal_counter_state.octal_counter_channel == Some(event.channel_id) { let Some(author) = event.author else { return; }; tokio::runtime::Handle::current().spawn(async move { if let Err(err) = event.channel_id.delete_message(&ctx, event.id).await { diff --git a/src/discord_bot/octal_counter.rs b/src/discord_bot/octal_counter.rs index 53ece6f..69b9782 100644 --- a/src/discord_bot/octal_counter.rs +++ b/src/discord_bot/octal_counter.rs @@ -1,5 +1,6 @@ +use serde::{Deserialize, Serialize}; use serenity::client::Context; -use serenity::model::id::{ChannelId, GuildId, MessageId}; +use serenity::model::id::{ChannelId, GuildId, MessageId, UserId}; use serenity::model::user::User; use crate::discord_bot::guild_storage::GuildStorage; @@ -23,19 +24,26 @@ pub(crate) async fn on_message( }; let mut storage = GuildStorage::get_mut(guild_id).await; - if storage.octal_counter_latest_user == Some(author.id) { + if storage.octal_counter_state.octal_counter_latest_user == Some(author.id) { channel_id.delete_message(&ctx, message_id).await?; storage.discard(); return Ok(()); } - if next_counter != storage.octal_counter + 1 { + if next_counter != storage.octal_counter_state.octal_counter + 1 { channel_id.delete_message(&ctx, message_id).await?; storage.discard(); return Ok(()); } - storage.octal_counter = next_counter; - storage.octal_counter_latest_user = Some(author.id); + storage.octal_counter_state.octal_counter = next_counter; + storage.octal_counter_state.octal_counter_latest_user = Some(author.id); storage.save().await; Ok(()) } + +#[derive(Debug, Default, Deserialize, Serialize)] +pub struct OctalCounterState { + pub octal_counter_channel: Option, + pub octal_counter: i32, + pub octal_counter_latest_user: Option, +} From a21889a935c57496f63649daa12719f83e5a5a83 Mon Sep 17 00:00:00 2001 From: xpple Date: Tue, 6 Jun 2023 16:55:34 +0200 Subject: [PATCH 6/7] Move discards --- src/discord_bot/octal_counter.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/discord_bot/octal_counter.rs b/src/discord_bot/octal_counter.rs index 69b9782..209a29d 100644 --- a/src/discord_bot/octal_counter.rs +++ b/src/discord_bot/octal_counter.rs @@ -25,13 +25,13 @@ pub(crate) async fn on_message( let mut storage = GuildStorage::get_mut(guild_id).await; if storage.octal_counter_state.octal_counter_latest_user == Some(author.id) { - channel_id.delete_message(&ctx, message_id).await?; storage.discard(); + channel_id.delete_message(&ctx, message_id).await?; return Ok(()); } if next_counter != storage.octal_counter_state.octal_counter + 1 { - channel_id.delete_message(&ctx, message_id).await?; storage.discard(); + channel_id.delete_message(&ctx, message_id).await?; return Ok(()); } From 3e89a2cb26472216b804f99fd083f6235dec10ef Mon Sep 17 00:00:00 2001 From: xpple Date: Thu, 8 Jun 2023 22:01:18 +0200 Subject: [PATCH 7/7] Apply rustfmt --- src/discord_bot/mod.rs | 17 +++++++++++++++-- src/discord_bot/octal_counter.rs | 4 ++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/discord_bot/mod.rs b/src/discord_bot/mod.rs index 1ab5f29..59e5d73 100644 --- a/src/discord_bot/mod.rs +++ b/src/discord_bot/mod.rs @@ -174,7 +174,12 @@ impl EventHandler for Handler { } let message_handling = { - if GuildStorage::get(guild_id).await.octal_counter_state.octal_counter_channel == Some(new_message.channel_id) { + if GuildStorage::get(guild_id) + .await + .octal_counter_state + .octal_counter_channel + == Some(new_message.channel_id) + { MessageHandling::OctalCounter } else if config::get().simple_words_channel == Some(new_message.channel_id) { MessageHandling::SimpleWords @@ -278,7 +283,15 @@ impl EventHandler for Handler { ); } }); - } else if GuildStorage::get(match event.guild_id { Some(i) => i, None => return }).await.octal_counter_state.octal_counter_channel == Some(event.channel_id) { + } else if GuildStorage::get(match event.guild_id { + Some(i) => i, + None => return, + }) + .await + .octal_counter_state + .octal_counter_channel + == Some(event.channel_id) + { let Some(author) = event.author else { return; }; tokio::runtime::Handle::current().spawn(async move { if let Err(err) = event.channel_id.delete_message(&ctx, event.id).await { diff --git a/src/discord_bot/octal_counter.rs b/src/discord_bot/octal_counter.rs index 209a29d..101b165 100644 --- a/src/discord_bot/octal_counter.rs +++ b/src/discord_bot/octal_counter.rs @@ -1,8 +1,8 @@ +use crate::discord_bot::guild_storage::GuildStorage; use serde::{Deserialize, Serialize}; use serenity::client::Context; use serenity::model::id::{ChannelId, GuildId, MessageId, UserId}; use serenity::model::user::User; -use crate::discord_bot::guild_storage::GuildStorage; pub(crate) async fn on_message( guild_id: GuildId, @@ -24,7 +24,7 @@ pub(crate) async fn on_message( }; let mut storage = GuildStorage::get_mut(guild_id).await; - if storage.octal_counter_state.octal_counter_latest_user == Some(author.id) { + if storage.octal_counter_state.octal_counter_latest_user == Some(author.id) { storage.discard(); channel_id.delete_message(&ctx, message_id).await?; return Ok(());