feat(commands): adding new main.rs

- Will fetch all commands
This commit is contained in:
Raphael 2026-02-12 16:18:59 +01:00
parent cc7369bcff
commit a55e9f9ce6
No known key found for this signature in database

View file

@ -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:?}");
}
}