Skip to content

Commit e767c09

Browse files
committed
Adds a new reset command for vertification codes
1 parent 99f8cf7 commit e767c09

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

trackscape-discord-bot/src/commands/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod info;
44
pub(crate) mod manually_run_wom_sync_command;
55
pub mod name_change_command;
66
pub mod reset_broadcasts_thresholds;
7+
pub mod reset_verification_code;
78
pub mod set_broadcast_channel;
89
pub mod set_clan_chat_channel;
910
pub mod set_diary_min_command;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use crate::database::BotMongoDb;
2+
use serenity::all::{CommandDataOption, CreateCommand};
3+
use serenity::client::Context;
4+
use serenity::model::prelude::Permissions;
5+
6+
pub fn register() -> CreateCommand {
7+
CreateCommand::new("reset_verification_code")
8+
.description(
9+
"Resets the verification code for the server. MUST UPDATE THE NEW CODE IN THE PLUGIN.",
10+
)
11+
.default_member_permissions(Permissions::MANAGE_GUILD)
12+
}
13+
14+
pub async fn run(
15+
_options: &[CommandDataOption],
16+
_ctx: &Context,
17+
db: &BotMongoDb,
18+
guild_id: u64,
19+
) -> Option<String> {
20+
let reset_code = db.guilds.reset_verification_code(guild_id).await;
21+
match reset_code {
22+
Ok(new_code) => Some(format!("The new verification code is: {}", new_code)),
23+
Err(e) => {
24+
eprintln!("Failed to reset verification code: {:?}", e);
25+
Some("Failed to reset verification code.".to_string())
26+
}
27+
}
28+
}

trackscape-discord-bot/src/main.rs

+10
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,15 @@ impl EventHandler for Bot {
307307
)
308308
.await
309309
}
310+
"reset_verification_code" => {
311+
commands::reset_verification_code::run(
312+
&command.data.options,
313+
&ctx,
314+
&self.mongo_db,
315+
command.guild_id.unwrap().get(),
316+
)
317+
.await
318+
}
310319
_ => {
311320
info!("not implemented :(");
312321
None
@@ -349,6 +358,7 @@ fn get_commands() -> Vec<CreateCommand> {
349358
commands.push(commands::manually_run_wom_sync_command::register());
350359
commands.push(commands::set_leagues_broadcast_channel::register());
351360
commands.push(commands::stop_leagues_notifications::register());
361+
commands.push(commands::reset_verification_code::register());
352362
commands
353363
}
354364
pub async fn create_commands_for_guild(guild_id: &GuildId, ctx: Context) {

trackscape-discord-shared/src/database/guilds_db.rs

+28
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,34 @@ impl GuildsDb {
134134
}
135135
}
136136

137+
pub async fn reset_verification_code(&self, guild_id: u64) -> Result<String, anyhow::Error> {
138+
let saved_guild_query = self.get_by_guild_id(guild_id).await;
139+
match saved_guild_query {
140+
Ok(saved_guild) => match saved_guild {
141+
Some(mut guild) => {
142+
let check_for_unique_code = self
143+
.recursive_check_for_unique_code(guild.verification_code.clone())
144+
.await;
145+
match check_for_unique_code {
146+
Ok(new_code) => {
147+
guild.verification_code = new_code.clone();
148+
guild.hashed_verification_code = hash_string(new_code.clone());
149+
self.update_guild(guild).await;
150+
Ok(new_code)
151+
}
152+
Err(_) => Err(anyhow::Error::msg("Failed to reset verification code.")),
153+
}
154+
}
155+
None => Err(anyhow::Error::msg(
156+
"Could not find a clan with that guild id.",
157+
)),
158+
},
159+
Err(_) => Err(anyhow::Error::msg(
160+
"Failed to get the saved guild to reset verification code.",
161+
)),
162+
}
163+
}
164+
137165
pub async fn get_guild_by_code_and_clan_name(
138166
&self,
139167
code: String,

0 commit comments

Comments
 (0)