From a55e9f9ce6ec38701e086c8c1f19b97f2bd0bdd7 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 12 Feb 2026 16:18:59 +0100 Subject: [PATCH] feat(commands): adding new main.rs - Will fetch all commands --- src/main.rs | 66 +++++++++++++---------------------------------------- 1 file changed, 16 insertions(+), 50 deletions(-) diff --git a/src/main.rs b/src/main.rs index 27c8d92..ac08ee3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,64 +1,30 @@ -use serenity::{ - Client, - all::{Command, Context, EventHandler, GatewayIntents, Interaction, Ready}, -}; -use std::env; -use tokio; - mod commands; +mod events; -struct Bot; - -#[serenity::async_trait] -impl EventHandler for Bot { - async fn ready(&self, ctx: Context, ready: Ready) { - println!("TTY is running on '{}' \n", ready.user.name); - - println!("Starting command registration:"); - - match Command::set_global_commands(&ctx.http, vec![commands::utils::ping::register()]).await - { - Ok(cmds) => { - for cmd in &cmds { - println!("\t✅ | {}", cmd.name); - } - } - Err(why) => eprintln!("❌ | Error cannot register a command : {why:?}"), - } - } - - async fn interaction_create(&self, ctx: Context, interaction: Interaction) { - let Interaction::Command(command) = interaction else { - return; - }; - - let result = match command.data.name.as_str() { - commands::utils::ping::COMMAND_NAME => commands::utils::ping::run(&ctx, &command).await, - other => { - eprintln!("⚠️ | Unknown command: /{other}"); - return; - } - }; - if let Err(e) = result { - eprintln!("❌ | Error on /{} : {e:?}", command.data.name); - } - } -} +use events::Bot; +use commands::all_commands; +use serenity::all::*; +use std::env; #[tokio::main] async fn main() { dotenvy::dotenv().ok(); - let token: String = env::var("DISCORD_TOKEN").expect("❌ | Missing DISCORD_TOKEN on env file"); + let token: String = env::var("DISCORD_TOKEN") + .expect("❌ | DISCORD_TOKEN missing (check the env file)"); - let intents = GatewayIntents::default(); + let intents: GatewayIntents = GatewayIntents::empty(); - let mut client = Client::builder(&token, intents) - .event_handler(Bot) + let bot: Bot = Bot { + commands: all_commands(), + }; + + let mut client: Client = Client::builder(&token, intents) + .event_handler(bot) .await - .expect("❌ | Cannot connect to the discord client"); + .expect("❌ | Error when loading bot"); if let Err(why) = client.start().await { - eprintln!("❌ | Client error : {why:?}"); + eprintln!("❌ Client error: {why:?}"); } }