feat: initial register functionality
This commit is contained in:
parent
f8ce315d8e
commit
e834b3308a
15 changed files with 183 additions and 1 deletions
|
|
@ -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
45
src/commands/users.rs
Normal 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
2
src/db/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
mod schema;
|
||||
pub mod users;
|
||||
10
src/db/schema.rs
Normal file
10
src/db/schema.rs
Normal 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
22
src/db/users.rs
Normal 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")
|
||||
}
|
||||
|
|
@ -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
0
src/models.rs
Normal file
Loading…
Add table
Add a link
Reference in a new issue