feat(commands): adding new main.rs
- Will fetch all commands
This commit is contained in:
parent
cc7369bcff
commit
a55e9f9ce6
1 changed files with 16 additions and 50 deletions
66
src/main.rs
66
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 commands;
|
||||||
|
mod events;
|
||||||
|
|
||||||
struct Bot;
|
use events::Bot;
|
||||||
|
use commands::all_commands;
|
||||||
#[serenity::async_trait]
|
use serenity::all::*;
|
||||||
impl EventHandler for Bot {
|
use std::env;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
dotenvy::dotenv().ok();
|
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)
|
let bot: Bot = Bot {
|
||||||
.event_handler(Bot)
|
commands: all_commands(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut client: Client = Client::builder(&token, intents)
|
||||||
|
.event_handler(bot)
|
||||||
.await
|
.await
|
||||||
.expect("❌ | Cannot connect to the discord client");
|
.expect("❌ | Error when loading bot");
|
||||||
|
|
||||||
if let Err(why) = client.start().await {
|
if let Err(why) = client.start().await {
|
||||||
eprintln!("❌ | Client error : {why:?}");
|
eprintln!("❌ Client error: {why:?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue