fmt: package it better

This commit is contained in:
silver 2023-09-17 15:35:41 +01:00
parent 14cbf0bcad
commit 98e61d7548
2 changed files with 173 additions and 177 deletions

View file

@ -15,7 +15,10 @@ use serenity::{
use skynet_discord_bot::{get_now_iso, random_string, Config, DataBase, Wolves, WolvesVerify}; use skynet_discord_bot::{get_now_iso, random_string, Config, DataBase, Wolves, WolvesVerify};
use sqlx::{Pool, Sqlite}; use sqlx::{Pool, Sqlite};
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String { pub(crate) mod link {
use super::*;
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
let db_lock = { let db_lock = {
let data_read = ctx.data.read().await; let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone() data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
@ -79,16 +82,16 @@ pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> Stri
} }
format!("Verification email sent to {}, user is {} {:?}", email, command.user.name, command.guild_id) format!("Verification email sent to {}, user is {} {:?}", email, command.user.name, command.guild_id)
} }
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command command
.name("link") .name("link")
.description("Set Wolves Email") .description("Set Wolves Email")
.create_option(|option| option.name("email").description("UL Wolves Email").kind(CommandOptionType::String).required(true)) .create_option(|option| option.name("email").description("UL Wolves Email").kind(CommandOptionType::String).required(true))
} }
async fn get_server_member_discord(db: &Pool<Sqlite>, user: &str) -> Option<Wolves> { async fn get_server_member_discord(db: &Pool<Sqlite>, user: &str) -> Option<Wolves> {
sqlx::query_as::<_, Wolves>( sqlx::query_as::<_, Wolves>(
r#" r#"
SELECT * SELECT *
@ -100,9 +103,9 @@ async fn get_server_member_discord(db: &Pool<Sqlite>, user: &str) -> Option<Wolv
.fetch_one(db) .fetch_one(db)
.await .await
.ok() .ok()
} }
async fn get_server_member_email(db: &Pool<Sqlite>, email: &str) -> Option<Wolves> { async fn get_server_member_email(db: &Pool<Sqlite>, email: &str) -> Option<Wolves> {
sqlx::query_as::<_, Wolves>( sqlx::query_as::<_, Wolves>(
r#" r#"
SELECT * SELECT *
@ -114,9 +117,9 @@ async fn get_server_member_email(db: &Pool<Sqlite>, email: &str) -> Option<Wolve
.fetch_one(db) .fetch_one(db)
.await .await
.ok() .ok()
} }
fn send_mail(config: &Config, email: &Wolves, auth: &str, user: &str) -> Result<smtp::response::Response, smtp::Error> { fn send_mail(config: &Config, email: &Wolves, auth: &str, user: &str) -> Result<smtp::response::Response, smtp::Error> {
let mail = &email.email; let mail = &email.email;
let discord = "https://discord.skynet.ie"; let discord = "https://discord.skynet.ie";
let sender = format!("UL Computer Society <{}>", &config.mail_user); let sender = format!("UL Computer Society <{}>", &config.mail_user);
@ -184,9 +187,9 @@ fn send_mail(config: &Config, email: &Wolves, auth: &str, user: &str) -> Result<
// Send the email // Send the email
mailer.send(&email) mailer.send(&email)
} }
async fn db_pending_clear_expired(pool: &Pool<Sqlite>) -> Option<WolvesVerify> { async fn db_pending_clear_expired(pool: &Pool<Sqlite>) -> Option<WolvesVerify> {
sqlx::query_as::<_, WolvesVerify>( sqlx::query_as::<_, WolvesVerify>(
r#" r#"
DELETE DELETE
@ -198,9 +201,9 @@ async fn db_pending_clear_expired(pool: &Pool<Sqlite>) -> Option<WolvesVerify> {
.fetch_one(pool) .fetch_one(pool)
.await .await
.ok() .ok()
} }
async fn get_from_db(db: &Pool<Sqlite>, user: &str) -> Option<WolvesVerify> { async fn get_from_db(db: &Pool<Sqlite>, user: &str) -> Option<WolvesVerify> {
sqlx::query_as::<_, WolvesVerify>( sqlx::query_as::<_, WolvesVerify>(
r#" r#"
SELECT * SELECT *
@ -212,9 +215,9 @@ async fn get_from_db(db: &Pool<Sqlite>, user: &str) -> Option<WolvesVerify> {
.fetch_one(db) .fetch_one(db)
.await .await
.ok() .ok()
} }
async fn save_to_db(db: &Pool<Sqlite>, record: &Wolves, auth: &str, user: &str) -> Result<Option<WolvesVerify>, sqlx::Error> { async fn save_to_db(db: &Pool<Sqlite>, record: &Wolves, auth: &str, user: &str) -> Result<Option<WolvesVerify>, sqlx::Error> {
sqlx::query_as::<_, WolvesVerify>( sqlx::query_as::<_, WolvesVerify>(
" "
INSERT INTO wolves_verify (email, discord, auth_code, date_expiry) INSERT INTO wolves_verify (email, discord, auth_code, date_expiry)
@ -227,4 +230,5 @@ async fn save_to_db(db: &Pool<Sqlite>, record: &Wolves, auth: &str, user: &str)
.bind(get_now_iso(false)) .bind(get_now_iso(false))
.fetch_optional(db) .fetch_optional(db)
.await .await
}
} }

View file

@ -4,10 +4,7 @@ use serenity::{
async_trait, async_trait,
client::{Context, EventHandler}, client::{Context, EventHandler},
model::{ model::{
application::{ application::{command::Command, interaction::Interaction},
command::Command,
interaction::{Interaction, InteractionResponseType},
},
gateway::{GatewayIntents, Ready}, gateway::{GatewayIntents, Ready},
guild, guild,
}, },
@ -59,7 +56,7 @@ impl EventHandler for Handler {
println!("[Main] {} is connected!", ready.user.name); println!("[Main] {} is connected!", ready.user.name);
Command::set_global_application_commands(&ctx.http, |commands| { Command::set_global_application_commands(&ctx.http, |commands| {
commands.create_application_command(|command| commands::link_email::register(command)) commands.create_application_command(|command| commands::link_email::link::register(command))
}) })
.await .await
.ok(); .ok();
@ -71,16 +68,11 @@ impl EventHandler for Handler {
//println!("Received command interaction: {:#?}", command); //println!("Received command interaction: {:#?}", command);
let content = match command.data.name.as_str() { let content = match command.data.name.as_str() {
"link" => commands::link_email::run(&command, &ctx).await, "link" => commands::link_email::link::run(&command, &ctx).await,
_ => "not implemented :(".to_string(), _ => "not implemented :(".to_string(),
}; };
if let Err(why) = command if let Err(why) = command.edit_original_interaction_response(&ctx.http, |response| response.content(content)).await {
.edit_original_interaction_response(&ctx.http, |response| {
response.content(content)
})
.await
{
println!("Cannot respond to slash command: {}", why); println!("Cannot respond to slash command: {}", why);
} }
} }