From c2fe6acecff74d6d5650b8838602f9d3a2521009 Mon Sep 17 00:00:00 2001 From: Stachelbeere1248 Date: Tue, 2 Jul 2024 21:36:34 +0200 Subject: [PATCH] account logging --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/commands/account.rs | 38 ++++++++++++++++++++++---------------- src/commands/bots.rs | 2 +- src/commands/helpstart.rs | 6 ++---- src/commands/lfg.rs | 7 +++---- src/commands/mod.rs | 4 ++-- src/main.rs | 22 ++++++++++++---------- 8 files changed, 53 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bd43f0a..bbeec18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2842,6 +2842,15 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "uuid" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5de17fd2f7da591098415cff336e12965a28061ddace43b59cb3c430179c9439" +dependencies = [ + "getrandom", +] + [[package]] name = "vcpkg" version = "0.2.15" @@ -3217,4 +3226,5 @@ dependencies = [ "sqlx", "tokio", "tracing", + "uuid", ] diff --git a/Cargo.toml b/Cargo.toml index fd72f0b..147dadf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ sqlx = { version = "0.7.4" , features = ["sqlite", "sqlx-sqlite", "runtime-tokio reqwest = { version = "0.12.5" } serde = { version = "1.0.203", features = ["derive"] } serde_json = { version = "1.0.119" } +uuid = { version = "1.9.1", features = ["v4"] } diff --git a/src/commands/account.rs b/src/commands/account.rs index 3694fdd..1373c4c 100644 --- a/src/commands/account.rs +++ b/src/commands/account.rs @@ -1,8 +1,8 @@ use poise::CreateReply; use serde::{Deserialize, Serialize}; -use serenity::all::User; +use serenity::all::{ChannelId, CreateMessage, User}; +use serenity::builder::CreateAllowedMentions; use sqlx::{query_as, Executor, Pool, Sqlite}; - use crate::{Context, Error}; #[poise::command(slash_command, subcommands("add", "list"))] @@ -11,30 +11,32 @@ pub(crate) async fn account(_ctx: Context<'_>) -> Result<(), Error> { } #[poise::command(slash_command)] -pub(crate) async fn add(ctx: Context<'_>, ign: String, user: Option) -> Result<(), Error> { - let user_id = user.clone().map(|user| user.id.get()); - let user_name = user.clone().map(|user| user.name); - let author_name = ctx.author().name.clone(); +pub(crate) async fn add(ctx: Context<'_>, ign: String) -> Result<(), Error> { let pool = ctx.data().sqlite_pool.clone(); - let minecraft_uuid = minecraft_uuid_for_username(ign).await?; + let minecraft_uuid = minecraft_uuid_for_username(ign.clone()).await?; let hypixel_linked_discord = linked_discord_for_uuid( ctx.data().hypixel_api_client.clone(), minecraft_uuid.as_str(), ) .await?; - if hypixel_linked_discord.eq(&user_name.unwrap_or(author_name)) { + if hypixel_linked_discord.eq(ctx.author().name.as_str()) { link( - user_id.unwrap_or(ctx.author().id.get()), + ctx.author().id.get(), minecraft_uuid.as_str(), &pool, ) .await; - if let Err(why) = ctx - .send(CreateReply::default().content("Linked accounts.")) - .await - { - println!("Error sending message: {why}"); - } + let s = format!("## User <@{}> added an account:\n### added:\n- name: {}\n- uuid: {}", + ctx.author().id.get(), + ign.clone(), + minecraft_uuid + ); + ChannelId::new(1257776992497959075).send_message(ctx, + CreateMessage::new() + .content(s) + .allowed_mentions(CreateAllowedMentions::new().empty_roles().empty_users()) + ).await?; + ctx.send(CreateReply::default().content("Linked accounts.")).await?; } Ok(()) } @@ -57,7 +59,10 @@ pub(crate) async fn list(ctx: Context<'_>, user: Option) -> Result<(), Err Some(id) => minecraft_uuids(&pool, id).await, None => Vec::new(), }; - let mut content = format!("## {}'s linked accounts: ", user_name.unwrap_or(author_name)); + let mut content = format!( + "## {}'s linked accounts: ", + user_name.unwrap_or(author_name) + ); for l in t { content.push_str(format!("\nuuid: {}", l).as_str()) } @@ -201,6 +206,7 @@ async fn link_id_from_minecraft(pool: &Pool, minecraft_uuid: String) -> .unwrap() .map(|minecraft_link: MinecraftLink| minecraft_link.link_id.cast_unsigned()); } + async fn new_link_id(pool: &Pool) -> u16 { let result: Result = query_as("SELECT link_id FROM minecraft_links WHERE link_id = (SELECT MAX(link_id) FROM minecraft_links) LIMIT 1;") .fetch_one(pool) diff --git a/src/commands/bots.rs b/src/commands/bots.rs index 9aa85fd..b245985 100644 --- a/src/commands/bots.rs +++ b/src/commands/bots.rs @@ -1,7 +1,7 @@ use std::string::String; -use crate::{Context, Error}; use crate::commands::command_helper; +use crate::{Context, Error}; #[poise::command(slash_command, guild_only, owners_only)] pub(crate) async fn bots( diff --git a/src/commands/helpstart.rs b/src/commands/helpstart.rs index 449331c..2911c9f 100644 --- a/src/commands/helpstart.rs +++ b/src/commands/helpstart.rs @@ -1,8 +1,8 @@ use poise::CreateReply; use serenity::all::CreateAllowedMentions; -use crate::{Context, Error}; use crate::commands::command_helper; +use crate::{Context, Error}; #[poise::command(slash_command, guild_only)] pub(crate) async fn helpstart( @@ -29,9 +29,7 @@ pub(crate) async fn helpstart( needed_players - bots )) .ephemeral(false) - .allowed_mentions(CreateAllowedMentions::new() - .roles(vec![1008075054971621448]) - ), + .allowed_mentions(CreateAllowedMentions::new().roles(vec![1008075054971621448])), Err(why) => reply.content(why.to_string()).ephemeral(true), } }; diff --git a/src/commands/lfg.rs b/src/commands/lfg.rs index 3554f5c..05e44d1 100644 --- a/src/commands/lfg.rs +++ b/src/commands/lfg.rs @@ -109,7 +109,7 @@ pub(crate) async fn lfg( let ping = match guild_id { 1256217633959841853 => new_ping, 995300932164276234 => old_ping, - _ => 0 + _ => 0, }; let difficulty: Difficulty = match map { DeadEnd | BadBlood | Prison => difficulty.unwrap_or(Normal), @@ -155,11 +155,10 @@ enum Expert { Speedrun, } #[poise::command(slash_command, guild_only, rename = "expert-lfg")] -pub(crate) async fn expert ( +pub(crate) async fn expert( ctx: Context<'_>, - #[rename = "map"] - mode: Expert, + #[rename = "map"] mode: Expert, #[min = 1_u8] #[max = 3_u8] diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 9e66465..b32caa5 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,6 +1,6 @@ +pub(crate) mod account; pub(crate) mod bots; pub(crate) mod command_helper; pub(crate) mod helpstart; pub(crate) mod lfg; -pub(crate) mod xd; -pub(crate) mod account; +pub(crate) mod xd; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7283c61..8f34e11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,9 +7,12 @@ use std::sync::Arc; use std::time::Duration; use poise::{async_trait, serenity_prelude as serenity}; -use serenity::{client::EventHandler, FullEvent, model::id::UserId}; -use serenity::all::{ActivityData, Attachment, ChannelId, CreateAttachment, CreateMessage, Event, Guild, GuildChannel}; use serenity::all::Route::Channel; +use serenity::all::{ + ActivityData, Attachment, ChannelId, CreateAttachment, CreateMessage, Event, Guild, + GuildChannel, +}; +use serenity::{client::EventHandler, model::id::UserId, FullEvent}; use sqlx::{Acquire, ConnectOptions, Executor, Sqlite}; use tokio::sync::RwLock; @@ -18,7 +21,7 @@ mod commands; struct Data { bots: Arc>, sqlite_pool: sqlx::Pool, - hypixel_api_client: reqwest::Client + hypixel_api_client: reqwest::Client, } // User data, which is stored and accessible in all command invocations type Error = Box; type Context<'a> = poise::Context<'a, Data, Error>; @@ -26,12 +29,11 @@ struct ReadyHandler; #[tokio::main] async fn main() { - - let sqlite_pool = sqlx::sqlite::SqlitePoolOptions::new() .idle_timeout(Duration::from_secs(10)) .max_connections(3) - .connect_lazy("sqlite:accounts.db").unwrap(); + .connect_lazy("sqlite:accounts.db") + .unwrap(); let hypixel_api: String = std::env::var("HYPIXEL_API_KEY").unwrap(); let hypixel_api_client = { @@ -42,10 +44,10 @@ async fn main() { ); reqwest::ClientBuilder::new() .default_headers(headers) - .build().unwrap() + .build() + .unwrap() }; - let options = poise::FrameworkOptions { commands: vec![ commands::lfg::lfg(), @@ -82,7 +84,7 @@ async fn main() { Ok(Data { bots: Arc::new(RwLock::new(0)), sqlite_pool, - hypixel_api_client + hypixel_api_client, }) }) }) @@ -107,7 +109,7 @@ async fn event_handler( match event { FullEvent::Ready { data_about_bot, .. } => { println!("Logged in as {}", data_about_bot.user.name); - }, + } _ => {} } Ok(())