2023-12-04 00:35:42 -05:00
|
|
|
mod commands;
|
2024-03-15 18:47:21 -04:00
|
|
|
mod internals;
|
2023-12-04 00:35:42 -05:00
|
|
|
|
2024-01-11 21:59:46 -05:00
|
|
|
use poise::serenity_prelude::{self as serenity};
|
2024-03-13 05:27:16 -04:00
|
|
|
use std::{
|
|
|
|
env::var,
|
|
|
|
error
|
|
|
|
};
|
2024-01-11 21:59:46 -05:00
|
|
|
use serenity::{
|
|
|
|
builder::{
|
|
|
|
CreateMessage,
|
|
|
|
CreateEmbed,
|
|
|
|
CreateEmbedAuthor
|
|
|
|
},
|
2024-03-10 19:06:40 -04:00
|
|
|
Context,
|
|
|
|
Ready,
|
2024-01-11 21:59:46 -05:00
|
|
|
ClientBuilder,
|
|
|
|
GatewayIntents
|
|
|
|
};
|
2023-12-04 00:35:42 -05:00
|
|
|
|
2024-03-13 05:27:16 -04:00
|
|
|
type Error = Box<dyn error::Error + Send + Sync>;
|
2023-12-04 00:35:42 -05:00
|
|
|
|
2024-01-07 11:22:08 -05:00
|
|
|
static BOT_READY_NOTIFY: u64 = 865673694184996888;
|
2023-12-06 19:22:16 -05:00
|
|
|
|
2023-12-04 00:35:42 -05:00
|
|
|
async fn on_ready(
|
2024-03-10 19:06:40 -04:00
|
|
|
ctx: &Context,
|
|
|
|
ready: &Ready,
|
2023-12-04 00:35:42 -05:00
|
|
|
framework: &poise::Framework<(), Error>
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
println!("Connected to API as {}", ready.user.name);
|
|
|
|
|
2024-01-11 21:59:46 -05:00
|
|
|
let message = CreateMessage::new();
|
|
|
|
let ready_embed = CreateEmbed::new()
|
2024-03-15 18:47:21 -04:00
|
|
|
.color(internals::utils::EMBED_COLOR)
|
2024-01-11 21:59:46 -05:00
|
|
|
.thumbnail(ready.user.avatar_url().unwrap_or_default())
|
|
|
|
.author(CreateEmbedAuthor::new(format!("{} is ready!", ready.user.name)).clone());
|
|
|
|
|
|
|
|
serenity::ChannelId::new(BOT_READY_NOTIFY).send_message(&ctx.http, message.add_embed(ready_embed)).await?;
|
2023-12-05 16:38:19 -05:00
|
|
|
|
2023-12-31 00:03:49 -05:00
|
|
|
let register_commands = var("REGISTER_CMDS").unwrap_or_else(|_| String::from("true")).parse::<bool>().unwrap_or(true);
|
2023-12-04 22:25:20 -05:00
|
|
|
|
|
|
|
if register_commands {
|
|
|
|
let builder = poise::builtins::create_application_commands(&framework.options().commands);
|
2024-01-11 21:59:46 -05:00
|
|
|
let commands = serenity::Command::set_global_commands(&ctx.http, builder).await;
|
2023-12-04 22:25:20 -05:00
|
|
|
|
|
|
|
match commands {
|
2023-12-31 00:03:49 -05:00
|
|
|
Ok(cmdmap) => for command in cmdmap.iter() {
|
2024-03-10 19:06:40 -04:00
|
|
|
println!("Registered command globally: {}", command.name);
|
|
|
|
},
|
2023-12-04 22:25:20 -05:00
|
|
|
Err(why) => println!("Error registering commands: {:?}", why)
|
|
|
|
}
|
2023-12-04 21:11:45 -05:00
|
|
|
}
|
2023-12-04 00:35:42 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2023-12-31 00:03:49 -05:00
|
|
|
let token = var("DISCORD_TOKEN").expect("Expected a \"DISCORD_TOKEN\" in the envvar but none was found");
|
2023-12-04 00:35:42 -05:00
|
|
|
|
2024-01-11 21:59:46 -05:00
|
|
|
let framework = poise::Framework::builder()
|
2023-12-04 00:35:42 -05:00
|
|
|
.options(poise::FrameworkOptions {
|
|
|
|
commands: vec![
|
2023-12-04 22:18:30 -05:00
|
|
|
commands::ping::ping(),
|
2023-12-06 05:49:55 -05:00
|
|
|
commands::eval::eval(),
|
2024-01-06 22:43:10 -05:00
|
|
|
commands::uptime::uptime()
|
2023-12-04 00:35:42 -05:00
|
|
|
],
|
2023-12-07 20:34:57 -05:00
|
|
|
pre_command: |ctx| Box::pin(async move {
|
2023-12-10 05:03:52 -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)
|
|
|
|
}),
|
2024-03-10 19:06:40 -04:00
|
|
|
on_error: |error| Box::pin(async move {
|
|
|
|
match error {
|
|
|
|
poise::FrameworkError::Command { error, ctx, .. } => {
|
|
|
|
println!("PoiseCommandError({}): {}", ctx.command().qualified_name, error);
|
|
|
|
}
|
|
|
|
other => println!("PoiseOtherError: {:?}", other)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
initialize_owners: true,
|
2023-12-04 00:35:42 -05:00
|
|
|
..Default::default()
|
2023-12-10 05:03:52 -05:00
|
|
|
})
|
|
|
|
.setup(|ctx, ready, framework| Box::pin(on_ready(ctx, ready, framework)))
|
2024-01-11 21:59:46 -05:00
|
|
|
.build();
|
|
|
|
|
|
|
|
let mut client = ClientBuilder::new(token, GatewayIntents::GUILDS).framework(framework).await.expect("Error creating client");
|
2023-12-04 00:35:42 -05:00
|
|
|
|
|
|
|
if let Err(why) = client.start().await {
|
|
|
|
println!("Client error: {:?}", why);
|
|
|
|
}
|
|
|
|
}
|