discord-bot/src/main.rs

149 lines
5.2 KiB
Rust
Raw Normal View History

pub mod commands;
2023-09-16 21:47:26 +00:00
2023-08-27 16:38:30 +00:00
use serenity::{
async_trait,
client::{Context, EventHandler},
model::{
2023-09-17 14:35:41 +00:00
application::{command::Command, interaction::Interaction},
2023-08-27 16:38:30 +00:00
gateway::{GatewayIntents, Ready},
guild,
2023-08-27 16:38:30 +00:00
},
Client,
};
use std::sync::Arc;
use skynet_discord_bot::{db_init, get_config, get_server_config, get_server_member, Config, DataBase};
2023-08-27 16:38:30 +00:00
use tokio::sync::RwLock;
2023-08-25 22:36:46 +00:00
struct Handler;
2023-08-25 22:36:46 +00:00
#[async_trait]
impl EventHandler for Handler {
async fn guild_member_addition(&self, ctx: Context, mut new_member: guild::Member) {
let db_lock = {
2023-08-27 16:38:30 +00:00
let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Config in TypeMap.").clone()
2023-08-27 16:38:30 +00:00
};
let db = db_lock.read().await;
let config = match get_server_config(&db, &new_member.guild_id).await {
None => return,
Some(x) => x,
2023-08-27 16:38:30 +00:00
};
2024-09-16 17:49:28 +00:00
if get_server_member(&db, &new_member.guild_id, &new_member).await.is_ok() {
2023-08-27 16:38:30 +00:00
let mut roles = vec![];
if let Some(role) = &config.role_past {
if !new_member.roles.contains(role) {
roles.push(role.to_owned());
}
}
2023-08-27 16:38:30 +00:00
if let Some(role) = &config.role_current {
if !new_member.roles.contains(role) {
roles.push(role.to_owned());
2023-08-27 16:38:30 +00:00
}
}
if let Err(e) = new_member.add_roles(&ctx, &roles).await {
println!("{:?}", e);
2023-08-27 16:38:30 +00:00
}
2024-09-16 17:49:28 +00:00
} else if let Some(name) = new_member.guild_id.name(&ctx) {
let msg = format!(
"Welcome to the {} server! \n\
Sign up on ulwolves and go to #bot_commands and do /link_wolves to get full access",
name
);
2024-09-16 17:49:28 +00:00
if let Err(err) = new_member.user.direct_message(&ctx, |m| m.content(&msg)).await {
dbg!(err);
}
2023-08-25 22:36:46 +00:00
}
2023-08-27 16:38:30 +00:00
}
2023-09-16 21:47:26 +00:00
async fn ready(&self, ctx: Context, ready: Ready) {
println!("[Main] {} is connected!", ready.user.name);
2023-09-16 21:47:26 +00:00
match Command::set_global_application_commands(&ctx.http, |commands| {
commands
.create_application_command(|command| commands::add_server::register(command))
.create_application_command(|command| commands::link_email::link::register(command))
.create_application_command(|command| commands::link_email::verify::register(command))
2024-06-03 01:16:28 +00:00
.create_application_command(|command| commands::minecraft::server::add::register(command))
.create_application_command(|command| commands::minecraft::server::list::register(command))
2024-06-03 02:56:45 +00:00
.create_application_command(|command| commands::minecraft::server::delete::register(command))
2024-06-03 01:16:28 +00:00
.create_application_command(|command| commands::minecraft::user::add::register(command))
2024-08-31 18:20:24 +00:00
// for committee server, temp
.create_application_command(|command| commands::committee::link::register(command))
.create_application_command(|command| commands::committee::verify::register(command))
2023-09-16 21:47:26 +00:00
})
.await
{
Ok(_) => {}
Err(e) => {
println!("{:?}", e)
}
}
2023-09-16 21:47:26 +00:00
}
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
if let Interaction::ApplicationCommand(command) = interaction {
let _ = command.defer_ephemeral(&ctx.http).await;
2023-09-16 21:47:26 +00:00
//println!("Received command interaction: {:#?}", command);
let content = match command.data.name.as_str() {
2024-06-03 01:06:35 +00:00
// user commands
"link_wolves" => commands::link_email::link::run(&command, &ctx).await,
"verify" => commands::link_email::verify::run(&command, &ctx).await,
2024-06-03 01:16:28 +00:00
"link_minecraft" => commands::minecraft::user::add::run(&command, &ctx).await,
2024-06-03 01:06:35 +00:00
// admin commands
"add" => commands::add_server::run(&command, &ctx).await,
2024-06-03 01:21:21 +00:00
"minecraft_add" => commands::minecraft::server::add::run(&command, &ctx).await,
"minecraft_list" => commands::minecraft::server::list::run(&command, &ctx).await,
2024-06-03 02:56:45 +00:00
"minecraft_delete" => commands::minecraft::server::delete::run(&command, &ctx).await,
2024-08-31 18:20:24 +00:00
// for teh committee server, temporary
"link_committee" => commands::committee::link::run(&command, &ctx).await,
"verify_committee" => commands::committee::verify::run(&command, &ctx).await,
2023-09-16 21:47:26 +00:00
_ => "not implemented :(".to_string(),
};
2023-09-17 14:35:41 +00:00
if let Err(why) = command.edit_original_interaction_response(&ctx.http, |response| response.content(content)).await {
2023-09-16 21:47:26 +00:00
println!("Cannot respond to slash command: {}", why);
}
}
2023-08-27 16:38:30 +00:00
}
}
2023-08-25 22:36:46 +00:00
#[tokio::main]
async fn main() {
2023-08-27 16:38:30 +00:00
let config = get_config();
let db = match db_init(&config).await {
Ok(x) => x,
Err(_) => return,
};
2023-08-27 16:38:30 +00:00
// Intents are a bitflag, bitwise operations can be used to dictate which intents to use
let intents = GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT | GatewayIntents::GUILD_MEMBERS;
// Build our client.
let mut client = Client::builder(&config.discord_token, intents)
.event_handler(Handler {})
2023-08-27 16:38:30 +00:00
.await
.expect("Error creating client");
{
let mut data = client.data.write().await;
2023-08-27 16:30:54 +00:00
2023-08-27 16:38:30 +00:00
data.insert::<Config>(Arc::new(RwLock::new(config)));
data.insert::<DataBase>(Arc::new(RwLock::new(db)));
2023-08-27 16:38:30 +00:00
}
// Finally, start a single shard, and start listening to events.
//
// Shards will automatically attempt to reconnect, and will perform
// exponential backoff until it reconnects.
if let Err(why) = client.start().await {
println!("Client error: {:?}", why);
}
}