Skip to content

Commit 9da696b

Browse files
authored
Merge pull request #305 from Rustmail/298-change-bot-status-with-a-command
feat(status): add command to change bot status and maintenance mode
2 parents 006b0bf + eac6ee3 commit 9da696b

26 files changed

Lines changed: 787 additions & 17 deletions

File tree

Cargo.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rustmail/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ sha2 = "0.10.9"
2929
hex = "0.4.3"
3030
moka = { version = "0.12.11", features = ["future"] }
3131
tower-http = { version = "0.6.8", features = ["compression-gzip", "compression-br"] }
32+
strum = { version = "0.27.2", features = ["derive"] }
3233

3334
[dependencies.uuid]
3435
version = "1.19.0"
3536
features = ["v4"]
3637

3738
[dev-dependencies]
38-
cargo-husky = { version = "1.5.0", default-features = false, features = ["run-for-all", "run-cargo-fmt", "precommit-hook", "run-cargo-test", "run-cargo-check"] }
39+
cargo-husky = { version = "1.5.0", default-features = false, features = ["run-for-all", "run-cargo-fmt", "precommit-hook", "run-cargo-test", "run-cargo-check"] }

rustmail/src/api/handler/externals/tickets/create.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub async fn handle_external_ticket_create(
134134
));
135135
}
136136

137-
if thread_exists(user_id, &db_pool).await {
137+
if thread_exists_by_user(user_id, &db_pool).await {
138138
return if let Some(channel_id_str) = get_thread_channel_by_user_id(user_id, &db_pool).await
139139
{
140140
Err((

rustmail/src/bot.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use serenity::prelude::TypeMapKey;
1111
use std::collections::HashMap;
1212
use std::process;
1313
use std::sync::Arc;
14+
use std::sync::atomic::AtomicBool;
1415
use std::time::Duration;
1516
use tokio::sync::Mutex;
1617
use tokio::{select, spawn};
@@ -36,6 +37,7 @@ pub async fn init_bot_state() -> Arc<Mutex<BotState>> {
3637
command_tx: command_tx.clone(),
3738
bot_http: None,
3839
bot_context: Arc::new(tokio::sync::RwLock::new(None)),
40+
maintenance_mode: Arc::new(AtomicBool::new(false)),
3941
};
4042

4143
Arc::new(Mutex::new(bot_state))
@@ -90,12 +92,15 @@ pub async fn run_bot(
9092
state_lock.db_pool.clone().expect("Database pool not set")
9193
};
9294

93-
let mut config = {
95+
let (mut config, maintenance_mode) = {
9496
let state_lock = bot_state.lock().await;
9597
if state_lock.config.is_none() {
9698
panic!("Config not set before starting rustmail!");
9799
}
98-
state_lock.config.clone().expect("Config not set")
100+
(
101+
state_lock.config.clone().expect("Config not set"),
102+
state_lock.maintenance_mode.clone(),
103+
)
99104
};
100105

101106
let pagination = Arc::new(Mutex::new(HashMap::<String, PaginationContext>::new()));
@@ -139,6 +144,7 @@ pub async fn run_bot(
139144
registry.register_command(ReleaseCommand);
140145
registry.register_command(PingCommand);
141146
registry.register_command(SnippetCommand);
147+
registry.register_command(StatusCommand);
142148

143149
let registry = Arc::new(registry);
144150

@@ -156,6 +162,7 @@ pub async fn run_bot(
156162
registry.clone(),
157163
shutdown_rx.clone(),
158164
pagination.clone(),
165+
maintenance_mode.clone(),
159166
)
160167
.await,
161168
)
@@ -168,6 +175,7 @@ pub async fn run_bot(
168175
registry.clone(),
169176
shutdown_rx,
170177
pagination,
178+
maintenance_mode,
171179
))
172180
.event_handler(GuildHandler::new(&config))
173181
.await

rustmail/src/commands/add_staff/slash_command/add_staff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl RegistrableCommand for AddStaffCommand {
105105
}
106106
};
107107

108-
if thread_exists(command.user.id, pool).await {
108+
if thread_exists_by_channel(command.channel_id, pool).await {
109109
match add_user_to_channel(&ctx, command.channel_id, user_id).await {
110110
Ok(_) => {
111111
let mut params = HashMap::new();

rustmail/src/commands/add_staff/text_command/add_staff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub async fn add_staff(
3030
Err(_) => return Err(ModmailError::Command(CommandError::InvalidFormat)),
3131
};
3232

33-
if thread_exists(msg.author.id, pool).await {
33+
if thread_exists_by_channel(msg.channel_id, pool).await {
3434
match add_user_to_channel(&ctx, msg.channel_id, user_id).await {
3535
Ok(_) => {
3636
let mut params = HashMap::new();

rustmail/src/commands/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub mod remove_reminder;
2929
pub mod remove_staff;
3030
pub mod reply;
3131
pub mod snippet;
32+
pub mod status;
3233
pub mod take;
3334

3435
pub use add_reminder::*;
@@ -51,6 +52,7 @@ pub use remove_reminder::*;
5152
pub use remove_staff::*;
5253
pub use reply::*;
5354
pub use snippet::*;
55+
pub use status::*;
5456
pub use take::*;
5557

5658
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

rustmail/src/commands/new_thread/slash_command/new_thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl RegistrableCommand for NewThreadCommand {
121121
return Err(ModmailError::Discord(DiscordError::UserIsABot));
122122
}
123123

124-
if thread_exists(user_id, pool).await {
124+
if thread_exists_by_user(user_id, pool).await {
125125
return if let Some(channel_id_str) =
126126
get_thread_channel_by_user_id(user_id, pool).await
127127
{

rustmail/src/commands/new_thread/text_command/new_thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub async fn new_thread(
3737
return Err(ModmailError::Discord(DiscordError::UserIsABot));
3838
}
3939

40-
if thread_exists(user_id, pool).await {
40+
if thread_exists_by_user(user_id, pool).await {
4141
if let Some(channel_id_str) = get_thread_channel_by_user_id(user_id, pool).await {
4242
let mut params = HashMap::new();
4343
params.insert("user".to_string(), user.name.clone());

rustmail/src/commands/remove_staff/slash_command/remove_staff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl RegistrableCommand for RemoveStaffCommand {
107107
}
108108
};
109109

110-
if thread_exists(command.user.id, pool).await {
110+
if thread_exists_by_channel(command.channel_id, pool).await {
111111
match remove_user_from_channel(&ctx, command.channel_id, user_id).await {
112112
Ok(_) => {
113113
let mut params = HashMap::new();

0 commit comments

Comments
 (0)