Kon/src/main.rs

73 lines
2.2 KiB
Rust
Raw Normal View History

2023-12-07 22:01:13 -05:00
mod commands;
use poise::serenity_prelude::{self as serenity};
use std::env::var;
2023-12-07 22:01:13 -05:00
type Error = Box<dyn std::error::Error + Send + Sync>;
pub static COLOR: i32 = 0x5a99c7;
async fn on_ready(
ctx: &serenity::Context,
ready: &serenity::Ready,
framework: &poise::Framework<(), Error>
) -> Result<(), Error> {
println!("Connected to API as {}", ready.user.name);
2023-12-08 06:44:27 -05:00
serenity::ChannelId(865673694184996888).send_message(&ctx.http, |m| m.embed(|e|
e.color(COLOR)
.thumbnail(ready.user.avatar_url().unwrap_or_default())
.author(|a|
a.name(format!("{} is ready!", ready.user.name))
)
)).await?;
2023-12-07 22:01:13 -05:00
let register_commands = var("REGISTER_CMDS").unwrap_or_else(|_| String::from("true")).parse::<bool>().unwrap_or(true);
2023-12-07 22:01:13 -05:00
if register_commands {
let builder = poise::builtins::create_application_commands(&framework.options().commands);
let commands = serenity::Command::set_global_application_commands(&ctx.http, |commands| {
*commands = builder.clone();
commands
}).await;
match commands {
Ok(cmdmap) => for command in cmdmap.iter() {
2023-12-07 22:01:13 -05:00
println!("Registered command globally: {}", command.name);
},
2023-12-07 22:01:13 -05:00
Err(why) => println!("Error registering commands: {:?}", why)
}
}
Ok(())
}
#[tokio::main]
async fn main() {
let token = var("DISCORD_TOKEN").expect("Expected a \"DISCORD_TOKEN\" in the envvar but none was found");
2023-12-07 22:01:13 -05:00
let client = poise::Framework::builder().token(token)
.intents(serenity::GatewayIntents::GUILDS)
2023-12-07 22:01:13 -05:00
.options(poise::FrameworkOptions {
commands: vec![
commands::ping::ping(),
2024-01-06 22:38:37 -05:00
commands::uptime::uptime(),
commands::status::status()
2023-12-07 22:01:13 -05:00
],
pre_command: |ctx| Box::pin(async move {
2023-12-09 20:53:10 -05:00
let get_guild_name = match ctx.guild() {
Some(guild) => guild.name.clone(),
None => String::from("DM")
};
println!("[{}] {} ran /{}", get_guild_name, ctx.author().name, ctx.command().qualified_name)
2023-12-07 22:01:13 -05:00
}),
..Default::default()
2023-12-10 04:56:25 -05:00
})
.setup(|ctx, ready, framework| Box::pin(on_ready(ctx, ready, framework)))
2023-12-07 22:01:13 -05:00
.build().await.expect("Error while building the client");
if let Err(why) = client.start().await {
println!("Client error: {:?}", why);
}
}