2023-09-16 21:47:26 +00:00
|
|
|
mod commands;
|
|
|
|
|
2023-08-27 16:38:30 +00:00
|
|
|
use serenity::{
|
|
|
|
async_trait,
|
|
|
|
client::{Context, EventHandler},
|
|
|
|
model::{
|
2023-09-16 23:14:50 +00:00
|
|
|
application::{
|
|
|
|
command::Command,
|
|
|
|
interaction::{Interaction, InteractionResponseType},
|
|
|
|
},
|
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
|
|
|
};
|
|
|
|
|
2023-09-11 01:25:07 +00:00
|
|
|
if get_server_member(&db, &new_member.guild_id, &new_member).await.is_some() {
|
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
|
|
|
}
|
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
|
|
|
|
|
|
|
Command::set_global_application_commands(&ctx.http, |commands| {
|
2023-09-16 23:14:50 +00:00
|
|
|
commands.create_application_command(|command| commands::link_email::register(command))
|
2023-09-16 21:47:26 +00:00
|
|
|
})
|
2023-09-16 23:14:50 +00:00
|
|
|
.await
|
|
|
|
.ok();
|
2023-09-16 21:47:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
|
|
|
if let Interaction::ApplicationCommand(command) = interaction {
|
|
|
|
//println!("Received command interaction: {:#?}", command);
|
|
|
|
|
|
|
|
let content = match command.data.name.as_str() {
|
|
|
|
"link" => commands::link_email::run(&command, &ctx).await,
|
|
|
|
_ => "not implemented :(".to_string(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(why) = command
|
|
|
|
.create_interaction_response(&ctx.http, |response| {
|
|
|
|
response
|
|
|
|
.kind(InteractionResponseType::ChannelMessageWithSource)
|
|
|
|
.interaction_response_data(|message| message.content(content).ephemeral(true))
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|