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

user-register
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

1
.env 100644
View File

@ -0,0 +1 @@
DATABASE_URL=sqlite://affy.db

1
.gitignore vendored
View File

@ -24,3 +24,4 @@ rust-project.json
# End of https://www.toptal.com/developers/gitignore/api/rust,rust-analyzer # End of https://www.toptal.com/developers/gitignore/api/rust,rust-analyzer
*.db

69
Cargo.lock generated
View File

@ -35,6 +35,7 @@ dependencies = [
"affluences-api", "affluences-api",
"async-minecraft-ping", "async-minecraft-ping",
"chrono", "chrono",
"diesel",
"poise", "poise",
"tokio", "tokio",
"uuid", "uuid",
@ -413,6 +414,28 @@ dependencies = [
"syn 1.0.109", "syn 1.0.109",
] ]
[[package]]
name = "diesel"
version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72eb77396836a4505da85bae0712fa324b74acfe1876d7c2f7e694ef3d0ee373"
dependencies = [
"diesel_derives",
"libsqlite3-sys",
]
[[package]]
name = "diesel_derives"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ad74fdcf086be3d4fdd142f67937678fe60ed431c3b2f08599e7687269410c4"
dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.10.6" version = "0.10.6"
@ -793,6 +816,16 @@ version = "0.2.144"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
[[package]]
name = "libsqlite3-sys"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afc22eff61b133b115c6e8c74e818c628d6d5e7a502afea6f64dee076dd94326"
dependencies = [
"pkg-config",
"vcpkg",
]
[[package]] [[package]]
name = "link-cplusplus" name = "link-cplusplus"
version = "1.0.8" version = "1.0.8"
@ -955,6 +988,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
[[package]] [[package]]
name = "poise" name = "poise"
version = "0.5.5" version = "0.5.5"
@ -992,6 +1031,30 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn 1.0.109",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.56" version = "1.0.56"
@ -1644,6 +1707,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"

View File

@ -22,3 +22,4 @@ chrono = "*"
uuid = "*" uuid = "*"
poise = "0.5.5" poise = "0.5.5"
async-minecraft-ping = "0.8.0" async-minecraft-ping = "0.8.0"
diesel = { version = "2.0.4", features = ["sqlite", "returning_clauses_for_sqlite_3_35"] }

8
diesel.toml 100644
View File

@ -0,0 +1,8 @@
# For documentation on how to configure this file,
# see https://diesel.rs/guides/configuring-diesel-cli
[print_schema]
file = "src/db/schema.rs"
[migrations_directory]
dir = "migrations"

0
migrations/.keep 100644
View File

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE users;

View File

@ -0,0 +1,7 @@
-- Your SQL goes here
CREATE TABLE users (
discord_id UNSIGNED BIG INT PRIMARY KEY NOT NULL,
email TEXT UNIQUE NOT NULL,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL
);

View File

@ -1,12 +1,19 @@
mod affluence; mod affluence;
mod minecraft; mod minecraft;
mod users;
use crate::{Context, Data, Error}; use crate::{Context, Data, Error};
type EmbedField = (String, String, bool); type EmbedField = (String, String, bool);
pub fn commands() -> Vec<poise::structs::Command<Data, Error>> { 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 /// Show this help menu

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 100644
View File

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

10
src/db/schema.rs 100644
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 100644
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 commands;
mod db;
use affluences_api::AffluencesClient; use affluences_api::AffluencesClient;
use diesel::Connection;
use poise::serenity_prelude as serenity; use poise::serenity_prelude as serenity;
use std::sync::Mutex;
use std::{env::var, time::Duration}; use std::{env::var, time::Duration};
// Types used by all command functions // 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 // Custom user data passed to all command functions
pub struct Data { pub struct Data {
client: AffluencesClient, client: AffluencesClient,
conn: Mutex<diesel::sqlite::SqliteConnection>,
} }
async fn on_error(error: poise::FrameworkError<'_, Data, Error>) { 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?; poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data { Ok(Data {
client: AffluencesClient::new(), client: AffluencesClient::new(),
conn: Mutex::new(
diesel::sqlite::SqliteConnection::establish("affy.db").unwrap(),
),
}) })
}) })
}) })

0
src/models.rs 100644
View File