discord-bot/src/main.rs

170 lines
5.8 KiB
Rust
Raw Normal View History

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::{
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},
2024-09-17 08:00:07 +00:00
prelude::Activity,
user::OnlineStatus,
2023-08-27 16:38:30 +00:00
},
Client,
};
use skynet_discord_bot::{db_init, get_config, get_server_config, get_server_member, Config, DataBase};
2024-09-29 23:09:29 +00:00
use std::sync::Arc;
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 {
// handles previously linked accounts joining the server
async fn guild_member_addition(&self, ctx: Context, mut new_member: 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
};
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 !new_member.roles.contains(&config.role_current) {
roles.push(config.role_current.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-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#"
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: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);
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
match Command::set_global_application_commands(&ctx.http, |commands| {
commands
.create_application_command(|command| commands::add_server::register(command))
.create_application_command(|command| commands::role_adder::edit::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))
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,
"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,
"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
}
// 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) {
// get config/db
let db_lock = {
let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Config in TypeMap.").clone()
};
let db = db_lock.read().await;
// check if the role changed is part of the oens for this server
on_role_change(&db, &ctx, new_data).await;
}
}
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(err) => {
dbg!(err);
2024-09-17 23:03:53 +00:00
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);
}
}