Kon/src/main.rs

203 lines
6.5 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::{
2024-07-25 01:39:15 -04:00
utils::{
token_path,
mention_dev
},
2024-07-19 19:56:58 -04:00
config::BINARY_PROPERTIES
},
// controllers::database::DatabaseController
2024-01-30 07:17:59 -05:00
};
2024-07-19 19:56:58 -04:00
2024-07-27 19:39:56 -04:00
use std::{
thread::current,
sync::Arc
};
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-01-11 21:50:38 -05:00
GatewayIntents
};
2023-12-07 22:01:13 -05:00
2024-07-27 19:39:56 -04:00
type Error = Box<dyn std::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(
2024-07-27 19:39:56 -04:00
ctx: &Context,
2024-07-19 19:56:58 -04:00
event: &FullEvent,
2024-07-30 22:21:26 -04:00
framework: poise::FrameworkContext<'_, (), Error>
2024-07-19 19:56:58 -04:00
) -> Result<(), Error> {
match event {
FullEvent::Ratelimit { data } => {
println!("Event[Ratelimit]: {:#?}", data);
}
2024-07-30 22:21:26 -04:00
FullEvent::Message { new_message } => {
if new_message.author.bot || !new_message.guild_id.is_none() {
return Ok(());
}
if new_message.content.to_lowercase().starts_with("deploy") && new_message.author.id == BINARY_PROPERTIES.developers[0] {
let builder = poise::builtins::create_application_commands(&framework.options().commands);
let commands = Command::set_global_commands(&ctx.http, builder).await;
let mut commands_deployed = std::collections::HashSet::new();
match commands {
Ok(cmdmap) => for command in cmdmap.iter() {
commands_deployed.insert(command.name.clone());
},
Err(y) => {
eprintln!("Error registering commands: {:?}", y);
new_message.reply(&ctx.http, "Deployment failed, check console for more details!").await?;
}
}
if commands_deployed.len() > 0 {
new_message.reply(&ctx.http, format!(
"Deployed the commands globally:\n- {}",
commands_deployed.into_iter().collect::<Vec<_>>().join("\n- ")
)).await?;
}
}
}
2024-07-27 19:39:56 -04:00
FullEvent::Ready { .. } => {
let thread_id = format!("{:?}", current().id());
let thread_num: String = thread_id.chars().filter(|c| c.is_digit(10)).collect();
println!("Event[Ready]: Task Scheduler operating on thread {}", thread_num);
let ctx = Arc::new(ctx.clone());
tokio::spawn(async move {
match internals::tasks::rss::rss(ctx).await {
Ok(_) => {},
Err(y) => {
eprintln!("TaskScheduler[Main:RSS:Error]: Task execution failed: {}", y);
if let Some(source) = y.source() {
eprintln!("TaskScheduler[Main:RSS:Error]: Task execution failed caused by: {:#?}", source);
}
}
}
});
}
2024-07-19 19:56:58 -04:00
_ => {}
}
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![
2024-07-30 05:00:31 -04:00
commands::ilo::ilo(),
2023-12-07 22:01:13 -05:00
commands::ping::ping(),
2024-01-30 13:22:08 -05:00
commands::status::status(),
2024-07-22 02:37:19 -04:00
commands::midi::midi_to_wav(),
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!(
2024-07-25 01:39:15 -04:00
"Encountered an error during command execution, ask {} to check console for more details!",
mention_dev(ctx).unwrap_or_default()
2024-07-19 19:56:58 -04:00
)).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
2024-07-30 22:21:26 -04:00
| GatewayIntents::MESSAGE_CONTENT
| GatewayIntents::DIRECT_MESSAGES
2024-07-19 19:56:58 -04:00
)
.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
}
}