Skip to content

Commit 5241262

Browse files
committed
Move April fools channels to guild storage
1 parent 9793aab commit 5241262

4 files changed

Lines changed: 80 additions & 43 deletions

File tree

src/config.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ impl Config {
107107
pub struct SpecialChannels {
108108
pub applications: ChannelId,
109109
pub support: ChannelId,
110-
#[serde(default)]
111-
pub simple_words: Option<ChannelId>,
112-
#[serde(default)]
113-
pub haiku: Option<ChannelId>,
114110
}
115111

116112
#[derive(Deserialize)]

src/discord_bot/april_fools_channel/mod.rs

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
1-
use crate::config;
2-
use serenity::all::{ChannelId, Context, CreateMessage, MessageFlags, MessageId, User};
1+
use crate::discord_bot::guild_storage::GuildStorage;
2+
use serde::{Deserialize, Serialize};
3+
use serenity::all::{ChannelId, Context, CreateMessage, GuildId, MessageFlags, MessageId, User};
34

45
mod haiku;
56
mod simple_words;
67

7-
pub(crate) fn get_april_fools_channel(
8+
#[derive(Debug)]
9+
pub(crate) struct AprilFoolsMessageContext<'a> {
10+
pub context: Context,
11+
pub has_attachments: bool,
12+
pub content: &'a str,
13+
pub author: &'a User,
14+
pub guild_id: GuildId,
15+
pub channel_id: ChannelId,
16+
pub message_id: MessageId,
17+
}
18+
19+
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
20+
pub(crate) struct AprilFoolsChannels {
21+
#[serde(default)]
22+
pub simple_words: Option<ChannelId>,
23+
#[serde(default)]
24+
pub haiku: Option<ChannelId>,
25+
}
26+
27+
pub(crate) async fn get_april_fools_channel(
28+
guild_id: GuildId,
829
channel_id: ChannelId,
930
) -> Option<&'static dyn AprilFoolsChannel> {
10-
let config = config::get();
11-
let channels = &config.special_channels;
31+
let storage = GuildStorage::get(guild_id).await;
32+
let channels = &storage.april_fools_channels;
1233
if channels.simple_words == Some(channel_id) {
1334
Some(&simple_words::CHANNEL)
1435
} else if channels.haiku == Some(channel_id) {
@@ -19,37 +40,38 @@ pub(crate) fn get_april_fools_channel(
1940
}
2041

2142
pub(crate) async fn on_message(
22-
ctx: Context,
2343
april_fools: &(impl AprilFoolsChannel + ?Sized),
24-
has_attachments: bool,
25-
content: &str,
26-
author: &User,
27-
channel_id: ChannelId,
28-
message_id: MessageId,
44+
context: AprilFoolsMessageContext<'_>,
2945
) -> crate::Result<()> {
3046
// find words in message
31-
let error_message = if has_attachments {
47+
let error_message = if context.has_attachments {
3248
Some(april_fools.has_attachment_message().to_owned())
3349
} else {
34-
april_fools.get_error(content)
50+
april_fools.get_error(context.content)
3551
};
3652
if let Some(mut error_message) = error_message {
37-
channel_id.delete_message(&ctx, message_id).await?;
38-
if !author.bot {
39-
let dm_channel = author.create_dm_channel(&ctx).await?;
53+
context
54+
.channel_id
55+
.delete_message(&context.context, context.message_id)
56+
.await?;
57+
if !context.author.bot {
58+
let dm_channel = context.author.create_dm_channel(&context.context).await?;
4059
if let Some(your_original_message_was) = april_fools.your_original_message_was() {
4160
error_message.push(' ');
4261
error_message.push_str(your_original_message_was);
4362
}
4463
dm_channel
45-
.send_message(&ctx, CreateMessage::new().content(error_message))
64+
.send_message(
65+
&context.context,
66+
CreateMessage::new().content(error_message),
67+
)
4668
.await?;
47-
if !content.is_empty() {
69+
if !context.content.is_empty() {
4870
dm_channel
4971
.send_message(
50-
ctx,
72+
context.context,
5173
CreateMessage::new()
52-
.content(content)
74+
.content(context.content)
5375
.flags(MessageFlags::SUPPRESS_NOTIFICATIONS),
5476
)
5577
.await?;

src/discord_bot/guild_storage.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::discord_bot::april_fools_channel::AprilFoolsChannels;
12
use crate::discord_bot::chess::ChessState;
23
use crate::discord_bot::permanent_latest::PermanentLatestInfo;
34
use crate::discord_bot::role::RoleData;
@@ -50,6 +51,8 @@ pub struct GuildStorage {
5051
pub counters: HashMap<String, u64>,
5152
#[serde(default)]
5253
pub social_credit: HashMap<UserId, i32>,
54+
#[serde(default)]
55+
pub april_fools_channels: AprilFoolsChannels,
5356
}
5457

5558
impl Default for GuildStorage {
@@ -69,6 +72,7 @@ impl Default for GuildStorage {
6972
users_sent_to_support: HashSet::new(),
7073
counters: HashMap::new(),
7174
social_credit: HashMap::new(),
75+
april_fools_channels: AprilFoolsChannels::default(),
7276
}
7377
}
7478
}

src/discord_bot/mod.rs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ mod update_copy;
1616
mod welcome_message;
1717

1818
use crate::config;
19-
use crate::discord_bot::april_fools_channel::{get_april_fools_channel, AprilFoolsChannel};
19+
use crate::discord_bot::april_fools_channel::{
20+
get_april_fools_channel, AprilFoolsChannel, AprilFoolsMessageContext,
21+
};
2022
use crate::discord_bot::guild_storage::GuildStorage;
2123
use crate::pterodactyl::{tellraw, PterodactylChatBridge, PterodactylServer};
2224
use async_trait::async_trait;
@@ -318,7 +320,9 @@ impl EventHandler for Handler {
318320
let config = config::get();
319321

320322
let message_handling = {
321-
if let Some(april_fools_channel) = get_april_fools_channel(new_message.channel_id) {
323+
if let Some(april_fools_channel) =
324+
get_april_fools_channel(guild_id, new_message.channel_id).await
325+
{
322326
MessageHandling::AprilFools(april_fools_channel)
323327
} else if new_message.author.bot {
324328
return;
@@ -373,13 +377,16 @@ impl EventHandler for Handler {
373377
}
374378
MessageHandling::AprilFools(april_fools) => {
375379
april_fools_channel::on_message(
376-
ctx,
377380
april_fools,
378-
!new_message.attachments.is_empty(),
379-
&new_message.content,
380-
&new_message.author,
381-
new_message.channel_id,
382-
new_message.id,
381+
AprilFoolsMessageContext {
382+
context: ctx,
383+
has_attachments: !new_message.attachments.is_empty(),
384+
content: &new_message.content,
385+
author: &new_message.author,
386+
guild_id,
387+
channel_id: new_message.channel_id,
388+
message_id: new_message.id,
389+
},
383390
)
384391
.await
385392
}
@@ -399,10 +406,15 @@ impl EventHandler for Handler {
399406
_new: Option<Message>,
400407
event: MessageUpdateEvent,
401408
) {
409+
let Some(guild_id) = event.guild_id else {
410+
return;
411+
};
412+
402413
enum MessageEditHandling {
403414
AprilFools(&'static dyn AprilFoolsChannel),
404415
}
405-
let handling = if let Some(april_fools_channel) = get_april_fools_channel(event.channel_id)
416+
let handling = if let Some(april_fools_channel) =
417+
get_april_fools_channel(guild_id, event.channel_id).await
406418
{
407419
MessageEditHandling::AprilFools(april_fools_channel)
408420
} else {
@@ -418,17 +430,20 @@ impl EventHandler for Handler {
418430
if let Err(err) = match handling {
419431
MessageEditHandling::AprilFools(april_fools) => {
420432
april_fools_channel::on_message(
421-
ctx,
422433
april_fools,
423-
event
424-
.attachments
425-
.as_ref()
426-
.map(|attachments| attachments.is_empty())
427-
== Some(false),
428-
&content,
429-
&author,
430-
event.channel_id,
431-
event.id,
434+
AprilFoolsMessageContext {
435+
context: ctx,
436+
has_attachments: event
437+
.attachments
438+
.as_ref()
439+
.map(|attachments| attachments.is_empty())
440+
== Some(false),
441+
content: &content,
442+
author: &author,
443+
guild_id,
444+
channel_id: event.channel_id,
445+
message_id: event.id,
446+
},
432447
)
433448
.await
434449
}

0 commit comments

Comments
 (0)