From f405cbbb93eb39436392525b1f2d9ccc81153c44 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Sat, 16 Sep 2023 22:47:26 +0100 Subject: [PATCH] feat: start of slash commands --- src/commands/link_email.rs | 58 ++++++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 1 + src/main.rs | 36 ++++++++++++++++++++++- 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/commands/link_email.rs create mode 100644 src/commands/mod.rs diff --git a/src/commands/link_email.rs b/src/commands/link_email.rs new file mode 100644 index 0000000..7e275e9 --- /dev/null +++ b/src/commands/link_email.rs @@ -0,0 +1,58 @@ +use serenity::builder::CreateApplicationCommand; +use serenity::client::Context; +use serenity::model::application::interaction::Interaction; +use serenity::model::application::interaction::application_command::ApplicationCommandInteraction; +use serenity::model::id::GuildId; +use serenity::model::prelude::command::CommandOptionType; +use serenity::model::prelude::interaction::application_command::{ + CommandDataOption, + CommandDataOptionValue, +}; +use serenity::model::user::User; +use sqlx::{Pool, Sqlite}; +use skynet_discord_bot::DataBase; + + +pub async fn run(options: &ApplicationCommandInteraction, ctx: &Context) -> String { + let db_lock = { + let data_read = ctx.data.read().await; + data_read.get::().expect("Expected Config in TypeMap.").clone() + }; + let db = db_lock.read().await; + + + + + let option = options.data.options.get(0).expect("Expected email option").resolved.as_ref().expect("Expected email object"); + + if let CommandDataOptionValue::String(email) = option { + format!("Email is {}, user is {} {:?}", email, options.user.name, options.guild_id) + } else { + "Please provide a valid user".to_string() + } +} + +pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { + command.name("link").description("Set Wolves Email").create_option(|option| { + option + .name("email") + .description("UL Wolves Email") + .kind(CommandOptionType::String) + .required(true) + }) +} + +async fn get_server_member(db: &Pool, server: &GuildId) -> Vec { + sqlx::query_as::<_, Accounts>( + r#" + SELECT * + FROM wolves + WHERE server = ? AND discord IS NOT NULL AND expiry > ? + "#, + ) + .bind(*server.as_u64() as i64) + .bind(get_now_iso(true)) + .fetch_all(db) + .await + .unwrap_or_default() +} \ No newline at end of file diff --git a/src/commands/mod.rs b/src/commands/mod.rs new file mode 100644 index 0000000..291a2ed --- /dev/null +++ b/src/commands/mod.rs @@ -0,0 +1 @@ +pub mod link_email; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 63431be..065d2d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,17 @@ +mod commands; + use serenity::{ async_trait, client::{Context, EventHandler}, model::{ gateway::{GatewayIntents, Ready}, guild, + application::{interaction::{Interaction, InteractionResponseType}, command::Command}, }, Client, }; use std::sync::Arc; +use serenity::model::prelude::interaction; use skynet_discord_bot::{db_init, get_config, get_server_config, get_server_member, Config, DataBase}; use tokio::sync::RwLock; @@ -49,8 +53,38 @@ impl EventHandler for Handler { } } - async fn ready(&self, _ctx: Context, ready: Ready) { + async fn ready(&self, ctx: Context, ready: Ready) { println!("[Main] {} is connected!", ready.user.name); + + + Command::set_global_application_commands(&ctx.http, |commands| { + commands + .create_application_command(|command| commands::link_email::register(command)) + }) + .await.ok(); + + } + + 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); + } + } } }