-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
148 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,96 @@ | ||
use poise::CreateReply; | ||
use scripty_bot_utils::{Context, Error}; | ||
use serenity::{ | ||
all::{Webhook, WebhookId}, | ||
builder::ExecuteWebhook, | ||
}; | ||
|
||
#[poise::command(prefix_command, hide_in_help, owners_only)] | ||
pub async fn shutdown(ctx: Context<'_>) -> Result<(), Error> { | ||
let msg = ctx.say("shutting down").await?; | ||
let cfg = scripty_config::get_config(); | ||
|
||
let webhook_builder = ExecuteWebhook::new().content(format!( | ||
"Scripty is shutting down for maintenance. If you have any questions, please feel free to \ | ||
join our support server at {}.", | ||
cfg.support_invite | ||
)); | ||
|
||
// iterate over all active voice connections and notify | ||
let songbird = scripty_audio_handler::get_songbird(); | ||
for (guild_id, call) in songbird.iter() { | ||
// leave the call | ||
call.lock().await.leave().await?; | ||
let calls = songbird.iter().map(|x| x.0).collect::<Vec<_>>(); | ||
msg.edit( | ||
ctx, | ||
CreateReply::new().content(format!( | ||
"shutting down {} voice connections\ndone: 0 (0%), succeeded 0, failed 0", | ||
calls.len() | ||
)), | ||
) | ||
.await?; | ||
let mut success = 0; | ||
let mut error = 0; | ||
let mut to_do = calls.len(); | ||
let total = calls.len(); | ||
|
||
for guild_id in calls { | ||
let f = async { | ||
// leave the call | ||
songbird.remove(guild_id).await?; | ||
|
||
// notify the guild | ||
|
||
// need to fetch from redis | ||
let webhook_id = scripty_redis::run_transaction::<u64>("GET", |f| { | ||
f.arg(format!("voice:{{{}}}:webhook_id", guild_id)); | ||
}) | ||
.await | ||
.map(WebhookId::new)?; | ||
let webhook_token = scripty_redis::run_transaction::<String>("GET", |f| { | ||
f.arg(format!("voice:{{{}}}:webhook_token", guild_id)); | ||
}) | ||
.await?; | ||
|
||
// notify the guild | ||
let hook = Webhook::from_id_with_token(ctx.http(), webhook_id, &webhook_token).await?; | ||
hook.execute(&ctx.serenity_context().http, false, webhook_builder.clone()) | ||
.await?; | ||
|
||
Result::<(), Error>::Ok(()) | ||
}; | ||
|
||
match f.await { | ||
Ok(()) => success += 1, | ||
Err(e) => { | ||
error += 1; | ||
error!("error while shutting down voice connection: {}", e); | ||
} | ||
} | ||
to_do -= 1; | ||
|
||
msg.edit( | ||
ctx, | ||
CreateReply::new().content(format!( | ||
"shutting down {} voice connections\ndone: {} ({}%), succeeded {}, failed {}", | ||
total, | ||
total - to_do, | ||
(total - to_do) * 100 / total, | ||
success, | ||
error | ||
)), | ||
) | ||
.await?; | ||
} | ||
|
||
msg.edit( | ||
ctx, | ||
CreateReply::new().content("done shutting down voice connections, shutting down bot now"), | ||
) | ||
.await?; | ||
ctx.data() | ||
.shard_manager | ||
.get() | ||
.ok_or_else(|| Error::custom("shard manager not found".to_string()))? | ||
.shutdown_all() | ||
.await; | ||
|
||
Ok(()) | ||
} |