Kon/src/main.rs

151 lines
4.7 KiB
Rust
Raw Normal View History

2023-12-07 22:01:13 -05:00
mod commands;
2024-01-30 07:17:59 -05:00
mod controllers;
mod internals;
2024-07-19 19:56:58 -04:00
// https://cdn.toast-server.net/RustFSHiearchy.png
// Using the new filesystem hierarchy
2023-12-07 22:01:13 -05:00
2024-07-19 19:56:58 -04:00
use crate::{
internals::{
utils::token_path,
config::BINARY_PROPERTIES
},
// controllers::database::DatabaseController
2024-01-30 07:17:59 -05:00
};
2024-07-19 19:56:58 -04:00
use std::error;
2024-03-20 17:55:48 -04:00
use poise::serenity_prelude::{
2024-01-11 21:50:38 -05:00
builder::{
CreateMessage,
CreateEmbed,
CreateEmbedAuthor
},
2024-01-30 07:17:59 -05:00
Ready,
2024-07-19 19:56:58 -04:00
Context,
FullEvent,
2024-01-11 21:50:38 -05:00
ClientBuilder,
2024-03-20 17:55:48 -04:00
ChannelId,
Command,
2024-07-19 19:56:58 -04:00
UserId,
2024-01-11 21:50:38 -05:00
GatewayIntents
};
2023-12-07 22:01:13 -05:00
2024-01-30 07:17:59 -05:00
type Error = Box<dyn error::Error + Send + Sync>;
2023-12-07 22:01:13 -05:00
async fn on_ready(
2024-01-30 07:17:59 -05:00
ctx: &Context,
ready: &Ready,
2023-12-07 22:01:13 -05:00
framework: &poise::Framework<(), Error>
) -> Result<(), Error> {
2024-07-19 19:56:58 -04:00
#[cfg(not(feature = "production"))]
{
println!("Event[Ready][Notice]: Detected a non-production environment!");
let gateway = ctx.http.get_bot_gateway().await?;
let session = gateway.session_start_limit;
println!("Event[Ready][Notice]: Session limit: {}/{}", session.remaining, session.total);
}
println!("Event[Ready]: Connected to API as {}", ready.user.name);
2023-12-07 22:01:13 -05:00
let message = CreateMessage::new();
let ready_embed = CreateEmbed::new()
2024-07-19 19:56:58 -04:00
.color(BINARY_PROPERTIES.embed_color)
.thumbnail(ready.user.avatar_url().unwrap_or_default())
2024-07-19 19:56:58 -04:00
.author(CreateEmbedAuthor::new(format!("{} is ready!", ready.user.name)));
2023-12-07 22:01:13 -05:00
2024-07-19 19:56:58 -04:00
ChannelId::new(BINARY_PROPERTIES.ready_notify).send_message(&ctx.http, message.add_embed(ready_embed)).await?;
2023-12-07 22:01:13 -05:00
2024-07-19 19:56:58 -04:00
if BINARY_PROPERTIES.deploy_commands {
2023-12-07 22:01:13 -05:00
let builder = poise::builtins::create_application_commands(&framework.options().commands);
2024-03-20 17:55:48 -04:00
let commands = Command::set_global_commands(&ctx.http, builder).await;
2024-07-19 19:56:58 -04:00
let mut commands_deployed = std::collections::HashSet::new();
2023-12-07 22:01:13 -05:00
match commands {
Ok(cmdmap) => for command in cmdmap.iter() {
2024-07-19 19:56:58 -04:00
commands_deployed.insert(command.name.clone());
},
2024-07-19 19:56:58 -04:00
Err(y) => eprintln!("Error registering commands: {:?}", y)
}
if commands_deployed.len() > 0 {
println!("Event[Ready]: Deployed the commands globally:\n- {}", commands_deployed.into_iter().collect::<Vec<_>>().join("\n- "));
2023-12-07 22:01:13 -05:00
}
}
Ok(())
}
2024-07-19 19:56:58 -04:00
async fn event_processor(
_ctx: &Context,
event: &FullEvent,
_framework: poise::FrameworkContext<'_, (), Error>
) -> Result<(), Error> {
match event {
FullEvent::Ratelimit { data } => {
println!("Event[Ratelimit]: {:#?}", data);
}
_ => {}
}
Ok(())
}
2023-12-07 22:01:13 -05:00
#[tokio::main]
async fn main() {
2024-07-19 19:56:58 -04:00
// DatabaseController::new().await.expect("Error initializing database");
2023-12-07 22:01:13 -05:00
let framework = poise::Framework::builder()
2023-12-07 22:01:13 -05:00
.options(poise::FrameworkOptions {
commands: vec![
commands::ping::ping(),
2024-01-30 13:22:08 -05:00
commands::status::status(),
2024-07-19 19:56:58 -04:00
commands::uptime::uptime()
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(),
2024-07-19 19:56:58 -04:00
None => String::from("Direct Message")
2023-12-09 20:53:10 -05:00
};
2024-07-19 19:56:58 -04:00
println!("Discord[{}] {} ran /{}", get_guild_name, ctx.author().name, ctx.command().qualified_name);
2023-12-07 22:01:13 -05:00
}),
on_error: |error| Box::pin(async move {
match error {
poise::FrameworkError::Command { error, ctx, .. } => {
println!("PoiseCommandError({}): {}", ctx.command().qualified_name, error);
2024-07-19 19:56:58 -04:00
ctx.reply(format!(
"Encountered an error during command execution, ask **{}** to check console for more details!",
UserId::new(BINARY_PROPERTIES.developers[0])
.to_user(&ctx.http())
.await.expect("Error getting user")
.nick_in(&ctx.http(), BINARY_PROPERTIES.guild_id)
.await.expect("Error getting nickname")
)).await.expect("Error sending message");
},
poise::FrameworkError::EventHandler { error, event, .. } => println!("PoiseEventHandlerError({}): {}", event.snake_case_name(), error),
poise::FrameworkError::Setup { error, .. } => println!("PoiseSetupError: {}", error),
poise::FrameworkError::UnknownInteraction { interaction, .. } => println!(
"PoiseUnknownInteractionError: {} tried to execute an unknown interaction ({})",
interaction.user.name,
interaction.data.name
),
other => println!("PoiseOtherError: {}", other)
}
}),
2024-01-30 07:17:59 -05:00
initialize_owners: true,
2024-07-19 19:56:58 -04:00
event_handler: |ctx, event, framework, _| Box::pin(event_processor(ctx, event, framework)),
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)))
.build();
2024-07-19 19:56:58 -04:00
let mut client = ClientBuilder::new(
token_path().await.main,
GatewayIntents::GUILDS
)
.framework(framework)
.await.expect("Error creating client");
2023-12-07 22:01:13 -05:00
if let Err(why) = client.start().await {
2024-07-19 19:56:58 -04:00
println!("Error starting client: {:#?}", why);
2023-12-07 22:01:13 -05:00
}
}