diff options
author | Cara Salter <cara@devcara.com> | 2022-01-07 13:33:31 -0500 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-01-07 13:33:31 -0500 |
commit | 3f9376a46efeb53b494a8b5272fc15be6ca5869a (patch) | |
tree | c3dc6e9a1b8362479718e8c8ea7d90ea6c3b6a75 /src/main.rs | |
download | glitch-ng-3f9376a46efeb53b494a8b5272fc15be6ca5869a.tar.gz glitch-ng-3f9376a46efeb53b494a8b5272fc15be6ca5869a.zip |
Initial Commit
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..bb686d2 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,103 @@ +use std::time::Duration; + +use dotenv::dotenv; + +type Error = Box<dyn std::error::Error + Send + Sync>; +type Context<'a> = poise::Context<'a, Data, Error>; + +mod commands; + +pub struct Data { + +} + +/// Show help menu +#[poise::command(prefix_command, slash_command)] +async fn help(ctx: Context<'_>, + #[description = "Command to get help for"] command: Option<String>, + ) -> Result<(), Error> { + poise::builtins::help(ctx, command.as_deref(), poise::builtins::HelpConfiguration::default()).await?; + Ok(()) +} + +async fn on_error(error: poise::FrameworkError<'_, Data, Error>) { + match error { + poise::FrameworkError::Setup { error } => panic!("Failed to start bot: {:?}", error), + poise::FrameworkError::Command {error, ctx} => { + println!("Error in command {}: {:?}", ctx.command().name, error); + }, + error => { + if let Err(e) = poise::builtins::on_error(error).await { + println!("Error handling error: {}", e); + } + } + } +} + +/// Register application commands in this guild or globally +/// +/// Run with no arguments to register in guild, run with argument "global" to register globally. +#[poise::command(prefix_command, hide_in_help)] +async fn register(ctx: Context<'_>, #[flag] global: bool) -> Result<(), Error> { + poise::builtins::register_application_commands(ctx, global).await?; + + Ok(()) +} + +#[tokio::main] +async fn main() { + dotenv().unwrap(); + let options = poise::FrameworkOptions { + commands: vec![ + help(), + register(), + commands::meta::ping(), + commands::meta::about(), + commands::meta::userinfo(), + + commands::actions::boop(), + commands::actions::hug(), + + commands::pony::randpony(), + commands::pony::tpony(), + + commands::osu::osup(), + commands::osu::osubm(), + ], + on_error: |error| Box::pin(on_error(error)), + pre_command: |ctx| { + Box::pin(async move { + println!("Executing command {}...", ctx.command().name); + }) + }, + post_command: |ctx| { + Box::pin(async move { + println!("Done executing command {}!", ctx.command().name); + }) + }, + + prefix_options: poise::PrefixFrameworkOptions { + prefix: Some("~".into()), + edit_tracker: Some(poise::EditTracker::for_timespan(Duration::from_secs(3600))), + additional_prefixes: vec![ + poise::Prefix::Literal("hey glitch"), + poise::Prefix::Literal("hey glitch,"), + ], + ..Default::default() + }, + ..Default::default() + }; + + poise::Framework::build() + .token(std::env::var("DISCORD_TOKEN").unwrap_or("BAD-TOKEN".into())) + .user_data_setup(move |_ctx, _ready, _framework| { + Box::pin(async move { + Ok(Data {}) + }) + }) + .options(options) + .run() + .await + .unwrap(); +} + |