Add default_register command

This will make registering the default setup easier
This commit is contained in:
Tom 2023-04-08 20:40:09 +02:00
parent 86cc799988
commit 8d8662be75

View file

@ -122,6 +122,87 @@ async fn register(
Ok(())
}
/// Use the default configuration for setting up commands.
#[poise::command(
prefix_command,
hide_in_help,
// required_permissions = "MANAGE_MESSAGES | ADMINISTRATOR",
owners_only=true,
)]
async fn default_register(ctx: Context<'_>) -> Result<(), Error> {
// poise::builtins::register_application_commands(ctx, global).await?;
let is_bot_owner = ctx.framework().options().owners.contains(&ctx.author().id);
let mut commands_builder_guild = serenity::CreateApplicationCommands::default();
let commands = &ctx.framework().options().commands;
ctx.say(format!("Commands: {}", commands.len())).await?;
fn globals_filter(name: &String) -> bool {
name.starts_with("invite") or name.starts_with("boop") or name.starts_with("fix")
}
// If the user is the bot owner, also correct the global commands.
if is_bot_owner {
let mut commands_builder_global = serenity::CreateApplicationCommands::default();
// let global_commands = commands; //.iter().filter(|com| com.name.starts_with("invite")).collect();
let mut global_count = 0;
for command in commands {
if globals_filter(&command.name) {
global_count += 1;
if let Some(slash_command) = command.create_as_slash_command() {
commands_builder_global.add_application_command(slash_command);
}
if let Some(context_menu_command) = command.create_as_context_menu_command() {
commands_builder_global.add_application_command(context_menu_command);
}
}
}
let commands_build_global = serenity::json::Value::Array(commands_builder_global.0);
ctx.serenity_context()
.http
.create_global_application_commands(&commands_build_global)
.await?;
ctx.say(format!("Registered {} as global commands", global_count))
.await?;
}
let mut command_count: u8 = 0;
for command in commands {
if !globals_filter(&command.name) {
command_count += 1;
if let Some(slash_command) = command.create_as_slash_command() {
commands_builder_guild.add_application_command(slash_command);
}
if let Some(context_menu_command) = command.create_as_context_menu_command() {
commands_builder_guild.add_application_command(context_menu_command);
}
}
}
let commands_builder = serenity::json::Value::Array(commands_builder_guild.0);
let guild = match ctx.guild() {
Some(x) => x,
None => {
return Ok(());
}
};
let is_guild_owner = ctx.author().id == guild.owner_id;
if !is_guild_owner && !is_bot_owner {
ctx.say("Can only be used by server owner").await?;
return Ok(());
}
ctx.say(format!(
"Registering {} commands out of {}...",
command_count,
commands.len()
))
.await?;
ctx.serenity_context()
.http
.create_guild_application_commands(guild.id.0, &commands_builder)
.await?;
ctx.say("Done!").await?;
Ok(())
}
async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
// This is our custom error handler
// They are many errors that can occur, so we only handle the ones we want to customize
@ -180,6 +261,7 @@ async fn app() -> Result<(), Error> {
let commands = vec![
help(),
register(),
default_register(),
commands::invites::invites(),
commands::schedule::schedule(),
commands::roles::roles(),