77 lines
2.6 KiB
Rust
77 lines
2.6 KiB
Rust
|
|
use std::env;
|
||
|
|
|
||
|
|
use serenity::async_trait;
|
||
|
|
use serenity::builder::{CreateInteractionResponse, CreateInteractionResponseMessage};
|
||
|
|
use serenity::model::application::Interaction;
|
||
|
|
use serenity::model::gateway::Ready;
|
||
|
|
use serenity::model::id::GuildId;
|
||
|
|
use serenity::prelude::*;
|
||
|
|
|
||
|
|
use crate::commands;
|
||
|
|
|
||
|
|
struct Handler;
|
||
|
|
|
||
|
|
#[async_trait]
|
||
|
|
impl EventHandler for Handler {
|
||
|
|
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
||
|
|
if let Interaction::Command(command) = interaction {
|
||
|
|
let content = match command.data.name.as_str() {
|
||
|
|
"roll" => Some(commands::roll::run(&command.data.options()).await),
|
||
|
|
"random" => Some(commands::random::run(&command.data.options()).await),
|
||
|
|
"about" => Some(commands::about::run(&command.data.options()).await),
|
||
|
|
_ => Some("not implemented".to_string()),
|
||
|
|
};
|
||
|
|
|
||
|
|
if let Some(content) = content {
|
||
|
|
let data = CreateInteractionResponseMessage::new().content(content);
|
||
|
|
let builder = CreateInteractionResponse::Message(data);
|
||
|
|
if let Err(why) = command.create_response(&ctx.http, builder).await {
|
||
|
|
println!("Cannot respond to slash command: {why}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async fn ready(&self, ctx: Context, ready: Ready) {
|
||
|
|
println!("{} is connected!", ready.user.name);
|
||
|
|
|
||
|
|
let guild_id = GuildId::new(
|
||
|
|
env::var("GUILD_ID")
|
||
|
|
.expect("Expected GUILD_ID in environment")
|
||
|
|
.parse()
|
||
|
|
.expect("GUILD_ID must be an integer"),
|
||
|
|
);
|
||
|
|
|
||
|
|
let cmds = guild_id
|
||
|
|
.set_commands(
|
||
|
|
&ctx.http,
|
||
|
|
vec![
|
||
|
|
commands::roll::register(),
|
||
|
|
commands::random::register(),
|
||
|
|
commands::about::register(),
|
||
|
|
],
|
||
|
|
)
|
||
|
|
.await;
|
||
|
|
|
||
|
|
match cmds {
|
||
|
|
Ok(c) => println!("Registered {} commands!", c.len()),
|
||
|
|
Err(e) => println!("Error registering commands! Reason: {e}"),
|
||
|
|
}
|
||
|
|
|
||
|
|
println!("{} is ready to rock and roll!", ready.user.name);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub async fn run() {
|
||
|
|
let token = env::var("DISCORD_TOKEN").expect("Expected DISCORD_TOKEN in environment");
|
||
|
|
let intents = GatewayIntents::MESSAGE_CONTENT | GatewayIntents::GUILD_MESSAGES;
|
||
|
|
let mut client = Client::builder(token, intents)
|
||
|
|
.event_handler(Handler)
|
||
|
|
.await
|
||
|
|
.expect("Error creating Discord client");
|
||
|
|
|
||
|
|
if let Err(why) = client.start().await {
|
||
|
|
println!("Discord client error: {why:?}");
|
||
|
|
}
|
||
|
|
}
|