2024-03-03 14:40:37 +00:00
|
|
|
pub mod commands;
|
2023-09-16 21:47:26 +00:00
|
|
|
|
2024-09-29 23:09:29 +00:00
|
|
|
use crate::commands::role_adder::tools::on_role_change;
|
|
|
|
use serenity::model::guild::Member;
|
2023-08-27 16:38:30 +00:00
|
|
|
use serenity::{
|
2024-10-28 00:59:04 +00:00
|
|
|
async_trait,
|
|
|
|
client::{Context, EventHandler},
|
|
|
|
model::{
|
|
|
|
application::{command::Command, interaction::Interaction},
|
|
|
|
gateway::{GatewayIntents, Ready},
|
|
|
|
prelude::Activity,
|
|
|
|
user::OnlineStatus,
|
|
|
|
},
|
|
|
|
Client,
|
2023-08-27 16:38:30 +00:00
|
|
|
};
|
2024-10-28 00:59:04 +00:00
|
|
|
use skynet_discord_bot::{get_config, Config};
|
2024-09-29 23:09:29 +00:00
|
|
|
use std::sync::Arc;
|
2024-10-28 21:40:48 +00:00
|
|
|
use serenity::model::id::GuildId;
|
2023-08-27 16:38:30 +00:00
|
|
|
use tokio::sync::RwLock;
|
2024-10-28 00:59:04 +00:00
|
|
|
use skynet_discord_bot::common::database::{db_init, get_server_config, get_server_member, DataBase};
|
2024-10-28 21:40:48 +00:00
|
|
|
use skynet_discord_bot::common::set_roles::committee::update_committees;
|
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 {
|
2024-09-29 20:04:08 +00:00
|
|
|
// handles previously linked accounts joining the server
|
2024-09-29 22:47:33 +00:00
|
|
|
async fn guild_member_addition(&self, ctx: Context, mut new_member: Member) {
|
2023-09-11 01:25:07 +00:00
|
|
|
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-10-28 21:40:48 +00:00
|
|
|
// committee server takes priority
|
|
|
|
if new_member.guild_id.eq(&GuildId(1220150752656363520)) {
|
|
|
|
let mut member = vec![new_member.clone()];
|
|
|
|
update_committees(&db, &ctx, &mut member).await;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-17 20:17:57 +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
|
|
|
|
2024-09-17 21:08:20 +00:00
|
|
|
if !new_member.roles.contains(&config.role_current) {
|
|
|
|
roles.push(config.role_current.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-17 20:41:30 +00:00
|
|
|
} else {
|
2024-09-16 17:49:28 +00:00
|
|
|
let msg = format!(
|
2024-09-17 20:41:30 +00:00
|
|
|
r#"
|
2024-09-18 06:15:25 +00:00
|
|
|
Welcome {} to the {} server!
|
|
|
|
Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use ``/link_wolves`` to get full access.
|
2024-09-17 20:41:30 +00:00
|
|
|
"#,
|
2024-09-17 21:11:34 +00:00
|
|
|
new_member.display_name(),
|
|
|
|
&config.server_name,
|
|
|
|
&config.wolves_link,
|
|
|
|
&config.server,
|
|
|
|
&config.bot_channel_id
|
2024-09-16 17:49:28 +00:00
|
|
|
);
|
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);
|
2024-09-17 08:00:07 +00:00
|
|
|
ctx.set_presence(Some(Activity::playing("with humanity's fate")), OnlineStatus::Online).await;
|
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))
|
2024-09-29 22:47:33 +00:00
|
|
|
.create_application_command(|command| commands::role_adder::edit::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))
|
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-09-29 22:47:33 +00:00
|
|
|
"roles_adder" => commands::role_adder::edit::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,
|
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
|
|
|
}
|
2024-09-29 20:04:08 +00:00
|
|
|
|
|
|
|
// handles role updates
|
2024-09-29 23:09:29 +00:00
|
|
|
async fn guild_member_update(&self, ctx: Context, _old_data: Option<Member>, new_data: Member) {
|
2024-09-29 20:04:08 +00:00
|
|
|
// get config/db
|
2024-09-29 22:47:33 +00:00
|
|
|
let db_lock = {
|
|
|
|
let data_read = ctx.data.read().await;
|
|
|
|
data_read.get::<DataBase>().expect("Expected Config in TypeMap.").clone()
|
|
|
|
};
|
2024-09-29 20:04:08 +00:00
|
|
|
|
2024-09-29 22:47:33 +00:00
|
|
|
let db = db_lock.read().await;
|
2024-09-29 20:04:08 +00:00
|
|
|
|
2024-09-29 22:47:33 +00:00
|
|
|
// check if the role changed is part of the oens for this server
|
|
|
|
on_role_change(&db, &ctx, new_data).await;
|
2024-09-29 20:04:08 +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,
|
2024-09-17 22:57:46 +00:00
|
|
|
Err(err) => {
|
|
|
|
dbg!(err);
|
2024-09-17 23:03:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-09-11 01:25:07 +00:00
|
|
|
};
|
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);
|
|
|
|
}
|
|
|
|
}
|