Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/discord_bot/guild_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ pub struct GuildStorage {
pub users_sent_to_support: HashSet<UserId>,
#[serde(default)]
pub counters: HashMap<String, u64>,
#[serde(default)]
pub octal_counter_channel: Option<ChannelId>,
#[serde(default)]
pub octal_counter: i32,
#[serde(default)]
pub octal_counter_latest_user: Option<UserId>,
}

impl Default for GuildStorage {
Expand All @@ -61,6 +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,
}
}
}
Expand Down
77 changes: 51 additions & 26 deletions src/discord_bot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 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
} else if new_message.author.bot {
return;
Expand Down Expand Up @@ -208,6 +212,18 @@ impl EventHandler for Handler {
MessageHandling::IncCounter(counter) => {
counter::inc_counter(counter, guild_id, ctx, &new_message).await
}
MessageHandling::OctalCounter => {
octal_counter::on_message(
guild_id,
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
}
Expand Down Expand Up @@ -238,32 +254,41 @@ impl EventHandler for Handler {
_new: Option<Message>,
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 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 {
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) {
Expand Down
41 changes: 41 additions & 0 deletions src/discord_bot/octal_counter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use serenity::client::Context;
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,
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 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(());
}

storage.octal_counter = next_counter;
storage.octal_counter_latest_user = Some(author.id);
storage.save().await;
Ok(())
}