2024-03-03 14:40:37 +00:00
|
|
|
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},
|
2023-09-11 01:25:07 +00:00
|
|
|
guild,
|
2023-08-27 16:38:30 +00:00
|
|
|
},
|
|
|
|
Client,
|
|
|
|
};
|
2023-09-11 01:25:07 +00:00
|
|
|
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
|
|
|
|
2023-09-11 01:25:07 +00:00
|
|
|
struct Handler;
|
2023-08-27 14:39:09 +00:00
|
|
|
|
2023-08-25 22:36:46 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl EventHandler for Handler {
|
2023-09-11 01:25:07 +00:00
|
|
|
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;
|
2023-09-11 01:25:07 +00:00
|
|
|
data_read.get::<DataBase>().expect("Expected Config in TypeMap.").clone()
|
2023-08-27 16:38:30 +00:00
|
|
|
};
|
|
|
|
|
2023-09-11 01:25:07 +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![];
|
|
|
|
|
2023-09-11 01:25:07 +00:00
|
|
|
if let Some(role) = &config.role_past {
|
2023-09-11 17:18:59 +00:00
|
|
|
if !new_member.roles.contains(role) {
|
2023-09-11 01:25:07 +00:00
|
|
|
roles.push(role.to_owned());
|
2023-08-26 14:55:07 +00:00
|
|
|
}
|
2023-09-11 01:25:07 +00:00
|
|
|
}
|
2023-08-27 16:38:30 +00:00
|
|
|
|
2023-09-11 01:25:07 +00:00
|
|
|
if let Some(role) = &config.role_current {
|
2023-09-11 17:18:59 +00:00
|
|
|
if !new_member.roles.contains(role) {
|
2023-09-11 01:25:07 +00:00
|
|
|
roles.push(role.to_owned());
|
2023-08-27 16:38:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 01:25:07 +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:42:18 +00:00
|
|
|
|
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);
|
2024-09-16 17:42:18 +00:00
|
|
|
}
|
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) {
|
2023-09-11 01:25:07 +00:00
|
|
|
println!("[Main] {} is connected!", ready.user.name);
|
2023-09-16 21:47:26 +00:00
|
|
|
|
2023-09-17 18:30:59 +00:00
|
|
|
match Command::set_global_application_commands(&ctx.http, |commands| {
|
2023-09-17 17:00:05 +00:00
|
|
|
commands
|
2023-09-17 18:30:59 +00:00
|
|
|
.create_application_command(|command| commands::add_server::register(command))
|
2023-09-17 17:00:05 +00:00
|
|
|
.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))
|
2024-06-03 02:04:12 +00:00
|
|
|
.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
|
|
|
})
|
2023-09-16 23:14:50 +00:00
|
|
|
.await
|
2023-09-17 18:30:59 +00:00
|
|
|
{
|
|
|
|
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 {
|
2023-09-17 14:25:17 +00:00
|
|
|
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
|
2024-03-02 21:45:43 +00:00
|
|
|
"link_wolves" => commands::link_email::link::run(&command, &ctx).await,
|
2023-09-17 17:00:05 +00:00
|
|
|
"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,
|
2024-06-03 02:04:12 +00:00
|
|
|
"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-27 14:39:09 +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();
|
2023-09-11 01:25:07 +00:00
|
|
|
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)
|
2023-09-11 01:25:07 +00:00
|
|
|
.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)));
|
2023-09-11 01:25:07 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|