forked from Skynet/discord-bot
parent
f11fdb4dde
commit
8760f4440a
7 changed files with 43 additions and 219 deletions
|
@ -6,7 +6,7 @@ use serenity::{
|
|||
prelude::{command::CommandOptionType, interaction::application_command::CommandDataOptionValue},
|
||||
},
|
||||
};
|
||||
use skynet_discord_bot::{DataBase, Servers, Wolves};
|
||||
use skynet_discord_bot::{DataBase, Servers};
|
||||
use sqlx::{Error, Pool, Sqlite};
|
||||
|
||||
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
|
||||
|
@ -130,11 +130,11 @@ pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicatio
|
|||
})
|
||||
}
|
||||
|
||||
async fn add_server(db: &Pool<Sqlite>, server: &Servers) -> Result<Option<Wolves>, Error> {
|
||||
async fn add_server(db: &Pool<Sqlite>, server: &Servers) -> Result<Option<Servers>, Error> {
|
||||
let role_past = server.role_past.map(|x| *x.as_u64() as i64);
|
||||
let role_current = server.role_current.map(|x| *x.as_u64() as i64);
|
||||
|
||||
sqlx::query_as::<_, Wolves>(
|
||||
sqlx::query_as::<_, Servers>(
|
||||
"
|
||||
INSERT OR REPLACE INTO servers (server, wolves_api, role_past, role_current)
|
||||
VALUES (?1, ?2, ?3, ?4)
|
||||
|
|
|
@ -16,6 +16,7 @@ use skynet_discord_bot::{get_now_iso, random_string, Config, DataBase, Wolves, W
|
|||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
pub(crate) mod link {
|
||||
use serenity::model::id::UserId;
|
||||
use super::*;
|
||||
|
||||
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
|
||||
|
@ -31,7 +32,7 @@ pub(crate) mod link {
|
|||
};
|
||||
let config = config_lock.read().await;
|
||||
|
||||
if get_server_member_discord(&db, &command.user.name).await.is_some() {
|
||||
if get_server_member_discord(&db, &command.user.id).await.is_some() {
|
||||
return "Already linked".to_string();
|
||||
}
|
||||
|
||||
|
@ -71,7 +72,7 @@ pub(crate) mod link {
|
|||
// generate a auth key
|
||||
let auth = random_string(20);
|
||||
match send_mail(&config, &details, &auth, &command.user.name) {
|
||||
Ok(_) => match save_to_db(&db, &details, &auth, &command.user.name).await {
|
||||
Ok(_) => match save_to_db(&db, &details, &auth, &command.user.id).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
return format!("Unable to save to db {} {e:?}", &details.email);
|
||||
|
@ -92,7 +93,7 @@ pub(crate) mod link {
|
|||
.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: &UserId) -> Option<Wolves> {
|
||||
sqlx::query_as::<_, Wolves>(
|
||||
r#"
|
||||
SELECT *
|
||||
|
@ -100,7 +101,7 @@ pub(crate) mod link {
|
|||
WHERE discord = ?
|
||||
"#,
|
||||
)
|
||||
.bind(user)
|
||||
.bind(*user.as_u64() as i64)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.ok()
|
||||
|
@ -218,7 +219,7 @@ pub(crate) mod link {
|
|||
.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: &UserId) -> Result<Option<WolvesVerify>, sqlx::Error> {
|
||||
sqlx::query_as::<_, WolvesVerify>(
|
||||
"
|
||||
INSERT INTO wolves_verify (email, discord, auth_code, date_expiry)
|
||||
|
@ -226,7 +227,7 @@ pub(crate) mod link {
|
|||
",
|
||||
)
|
||||
.bind(record.email.to_owned())
|
||||
.bind(user)
|
||||
.bind(*user.as_u64() as i64)
|
||||
.bind(auth.to_owned())
|
||||
.bind(get_now_iso(false))
|
||||
.fetch_optional(db)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue