feat: initial register functionality
Some checks failed
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/clippy Pipeline failed
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
Jef Roosens 2023-05-15 17:38:13 +02:00
parent f8ce315d8e
commit e834b3308a
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
15 changed files with 183 additions and 1 deletions

View file

@ -1,12 +1,19 @@
mod affluence;
mod minecraft;
mod users;
use crate::{Context, Data, Error};
type EmbedField = (String, String, bool);
pub fn commands() -> Vec<poise::structs::Command<Data, Error>> {
vec![help(), affluence::available(), minecraft::ping_mc()]
vec![
help(),
affluence::available(),
minecraft::ping_mc(),
users::register(),
users::registered(),
]
}
/// Show this help menu

45
src/commands/users.rs Normal file
View file

@ -0,0 +1,45 @@
use crate::db::users::{user_all, user_insert, User};
use crate::{Context, Error};
#[poise::command(prefix_command, slash_command)]
pub async fn register(
ctx: Context<'_>,
first_name: String,
last_name: String,
email: String,
) -> Result<(), Error> {
let user = User {
discord_id: ctx.author().id.0 as i64,
first_name,
last_name,
email,
};
{
let mut conn = ctx.data().conn.lock().unwrap();
user_insert(&mut conn, &user);
}
Ok(())
}
#[poise::command(prefix_command, slash_command)]
pub async fn registered(ctx: Context<'_>) -> Result<(), Error> {
let users = {
let mut conn = ctx.data().conn.lock().unwrap();
user_all(&mut conn)
};
ctx.send(|f| {
f.embed(|e| {
e.description("Registered users").fields(
users
.into_iter()
.map(|u| (format!("{} {}", u.first_name, u.last_name), u.email, false)),
)
})
})
.await?;
Ok(())
}

2
src/db/mod.rs Normal file
View file

@ -0,0 +1,2 @@
mod schema;
pub mod users;

10
src/db/schema.rs Normal file
View file

@ -0,0 +1,10 @@
// @generated automatically by Diesel CLI.
diesel::table! {
users (discord_id) {
discord_id -> BigInt,
email -> Text,
first_name -> Text,
last_name -> Text,
}
}

22
src/db/users.rs Normal file
View file

@ -0,0 +1,22 @@
use super::schema::users;
use diesel::prelude::*;
use diesel::sqlite::SqliteConnection;
#[derive(Queryable, Insertable)]
pub struct User {
pub discord_id: i64,
pub email: String,
pub first_name: String,
pub last_name: String,
}
pub fn user_insert(conn: &mut SqliteConnection, user: &User) -> User {
diesel::insert_into(users::table)
.values(user)
.get_result(conn)
.expect("fuck")
}
pub fn user_all(conn: &mut SqliteConnection) -> Vec<User> {
users::table.load::<User>(conn).expect("nou")
}

View file

@ -1,7 +1,10 @@
mod commands;
mod db;
use affluences_api::AffluencesClient;
use diesel::Connection;
use poise::serenity_prelude as serenity;
use std::sync::Mutex;
use std::{env::var, time::Duration};
// Types used by all command functions
@ -11,6 +14,7 @@ type Context<'a> = poise::Context<'a, Data, Error>;
// Custom user data passed to all command functions
pub struct Data {
client: AffluencesClient,
conn: Mutex<diesel::sqlite::SqliteConnection>,
}
async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
@ -91,6 +95,9 @@ async fn main() {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {
client: AffluencesClient::new(),
conn: Mutex::new(
diesel::sqlite::SqliteConnection::establish("affy.db").unwrap(),
),
})
})
})

0
src/models.rs Normal file
View file