Skip to content

Commit

Permalink
Generate and print command list on deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinegb committed Sep 20, 2024
1 parent 508e9aa commit 788b508
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/commands/anon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use crate::{
/// Sends a message anonymously
#[command(
slash_command,
category = "Other",
install_context = "Guild",
interaction_context = "Guild",
required_bot_permissions = "MANAGE_WEBHOOKS|SEND_MESSAGES|USE_EXTERNAL_EMOJIS",
Expand Down
1 change: 1 addition & 0 deletions src/commands/rock_paper_scissors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::{emoji::*, Context, Error};
/// Challenge someone to a game of Rock Paper Scissors
#[command(
slash_command,
category = "Other",
install_context = "Guild|User",
interaction_context = "Guild|BotDm|PrivateChannel",
required_bot_permissions = "USE_EXTERNAL_EMOJIS"
Expand Down
1 change: 1 addition & 0 deletions src/commands/silly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ macro_rules! silly_command {
#[$doc]
#[command(
slash_command,
category = "Silly",
install_context = "Guild|User",
interaction_context = "Guild|BotDm|PrivateChannel",
required_bot_permissions = "USE_EXTERNAL_EMOJIS"
Expand Down
1 change: 1 addition & 0 deletions src/commands/sponsors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::{
/// Lists current GitHub sponsors ❤️
#[command(
slash_command,
category = "Other",
install_context = "Guild|User",
interaction_context = "Guild|BotDm|PrivateChannel",
required_bot_permissions = "USE_EXTERNAL_EMOJIS",
Expand Down
1 change: 1 addition & 0 deletions src/commands/strike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ fn pre_strike_command(ctx: Context<'_>) -> Result<Option<ChannelId>, Error> {
#[command(
slash_command,
subcommands("give", "history", "repeal"),
category = "Strikes",
install_context = "Guild",
interaction_context = "Guild",
required_bot_permissions = "USE_EXTERNAL_EMOJIS"
Expand Down
1 change: 1 addition & 0 deletions src/commands/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl From<FormattedTimestampStyleChoice> for FormattedTimestampStyle {
/// Generates a Unix timestamp for your use in Discord styled messages
#[command(
slash_command,
category = "Other",
install_context = "Guild|User",
interaction_context = "Guild|BotDm|PrivateChannel",
ephemeral
Expand Down
1 change: 1 addition & 0 deletions src/commands/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::{Context, Error};
/// Lists the 10 most recent Goober Bot changes
#[command(
slash_command,
category = "Other",
install_context = "Guild|User",
interaction_context = "Guild|BotDm|PrivateChannel",
ephemeral
Expand Down
1 change: 1 addition & 0 deletions src/commands/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::{emoji::*, Context, Error};
/// Vote for Goober Bot on Top.gg!
#[command(
slash_command,
category = "Other",
install_context = "Guild|User",
interaction_context = "Guild|BotDm|PrivateChannel",
ephemeral
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub(crate) fn get_config_key(ctx: Context<'_>) -> Result<String, Error> {
#[command(
slash_command,
subcommands("list", "get", "set"),
category = "Config",
install_context = "Guild",
interaction_context = "Guild",
required_bot_permissions = "USE_EXTERNAL_EMOJIS",
Expand Down
82 changes: 81 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ pub(crate) use crate::error::Error;

#[cfg(not(debug_assertions))]
use std::time::Duration;
use std::{collections::HashSet, fmt::Debug};
use std::{
collections::{BTreeMap, HashSet},
fmt::Debug,
};

use analytics::analytics;
use anyhow::Context as _;
use chrono::Utc;
use config::config;
use octocrab::Octocrab;
use poise::{
Expand Down Expand Up @@ -64,6 +68,80 @@ struct Data {

type Context<'a> = poise::Context<'a, Data, Error>;

fn print_commands<U, E>(commands: &[poise::Command<U, E>]) {
fn print_command<U, E>(command: &poise::Command<U, E>) {
print!("- /{}", command.qualified_name);

for parameter in &command.parameters {
print!(" ");

if parameter.required {
print!("<");
} else {
print!("[");
}

print!("{}", parameter.name);

if parameter.required {
print!(">");
} else {
print!("]");
}
}

if command.name == "sponsors" {
print!(" 💖");
} else if command.name == "vote" {
print!(" ❤️");
}

println!();
}

let mut category_keys = Vec::new();
let mut categories: BTreeMap<&String, Vec<&poise::Command<U, E>>> = BTreeMap::new();

for command in commands {
if let Some(category) = &command.category {
if !category_keys.contains(&category) {
category_keys.push(category);
}

let category_commands = categories.entry(category).or_default();

category_commands.push(command);
}
}

category_keys.sort_by(|a, b| categories[b].len().cmp(&categories[a].len()));

let other_category_key_index = category_keys
.binary_search(&&String::from("Other"))
.expect("there should be a command category called \"Other\"");
let other_category_key = category_keys.remove(other_category_key_index);

category_keys.push(other_category_key);
println!("## Commands\n");
println!("*Last updated {}*", Utc::now().format("%b %e, %Y"));

for category in category_keys {
let category_commands = &categories[category];

println!("\n### {category}\n");

for command in category_commands {
for subcommand in &command.subcommands {
print_command(subcommand);
}

if command.subcommands.is_empty() {
print_command(command);
}
}
}
}

#[shuttle_runtime::main]
async fn main(
#[shuttle_runtime::Secrets] secret_store: SecretStore,
Expand Down Expand Up @@ -147,6 +225,8 @@ async fn main(
Box::pin(async move {
start_activity_loop(ctx.clone());
info!("Activity loop started");
// Omit `category` argument on a command to hide from list
print_commands(&framework.options().commands);
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
info!("Commands registered");
octocrab::initialise(Octocrab::builder().personal_token(github_pat).build()?);
Expand Down

0 comments on commit 788b508

Please sign in to comment.