87 lines
2.7 KiB
Rust
87 lines
2.7 KiB
Rust
mod commands;
|
|
mod config;
|
|
mod types;
|
|
mod util;
|
|
|
|
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::*;
|
|
|
|
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 commands = guild_id
|
|
.set_commands(
|
|
&ctx.http,
|
|
vec![
|
|
commands::roll::register(),
|
|
commands::random::register(),
|
|
commands::about::register(),
|
|
],
|
|
)
|
|
.await;
|
|
|
|
match commands {
|
|
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);
|
|
}
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
// setting up configuration
|
|
let _ = config::config();
|
|
|
|
// setting up the discord bot
|
|
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
|
|
|
|
// setting up the discord client
|
|
let intents = GatewayIntents::MESSAGE_CONTENT | GatewayIntents::GUILD_MESSAGES;
|
|
let mut client = Client::builder(token, intents)
|
|
.event_handler(Handler)
|
|
.await
|
|
.expect("Error creating client");
|
|
|
|
if let Err(why) = client.start().await {
|
|
println!("Client error: {why:?}");
|
|
}
|
|
}
|