diff --git a/src/commands/command_macro/record.rs b/src/commands/command_macro/record.rs index f63968e..a40bb60 100644 --- a/src/commands/command_macro/record.rs +++ b/src/commands/command_macro/record.rs @@ -33,7 +33,11 @@ pub async fn record_macro( let row = sqlx::query!( " -SELECT 1 as _e FROM macro WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?) AND name = ?", + SELECT 1 as _e + FROM macro + WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?) + AND name = ? + ", guild_id.get(), name ) diff --git a/src/hooks.rs b/src/hooks.rs index 3cdef46..50f690a 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -1,3 +1,4 @@ +use log::warn; use poise::{serenity_prelude::model::channel::Channel, CreateReply}; use crate::{consts::MACRO_MAX_COMMANDS, models::command_macro::RecordedCommand, Context, Error}; @@ -6,8 +7,11 @@ async fn macro_check(ctx: Context<'_>) -> bool { if let Context::Application(app_ctx) = ctx { if let Some(guild_id) = ctx.guild_id() { if ctx.command().identifying_name != "finish_macro" { - if ctx.command().identifying_name != "remind" { - let _ = ctx + let mut lock = ctx.data().recording_macros.write().await; + + if let Some(command_macro) = lock.get_mut(&(guild_id, ctx.author().id)) { + if ctx.command().identifying_name != "remind" { + let _ = ctx .send( CreateReply::default() .ephemeral(true) @@ -15,12 +19,9 @@ async fn macro_check(ctx: Context<'_>) -> bool { ) .await; - return false; - } + return false; + } - let mut lock = ctx.data().recording_macros.write().await; - - if let Some(command_macro) = lock.get_mut(&(guild_id, ctx.author().id)) { if command_macro.commands.len() >= MACRO_MAX_COMMANDS { let _ = ctx .send( @@ -52,52 +53,48 @@ async fn macro_check(ctx: Context<'_>) -> bool { } async fn check_self_permissions(ctx: Context<'_>) -> bool { - if let Some(guild_id) = ctx.guild_id() { - let user_id = ctx.serenity_context().cache.current_user().id; + let user_id = ctx.serenity_context().cache.current_user().id; - let manage_webhooks = guild_id - .current_user_member(&ctx) - .await - .map_or(false, |m| m.permissions(&ctx).map_or(false, |p| p.manage_webhooks())); + let (view_channel, send_messages, embed_links, manage_webhooks) = ctx + .channel_id() + .to_channel(&ctx) + .await + .ok() + .and_then(|c| { + if let Channel::Guild(channel) = c { + let perms = channel.permissions_for_user(&ctx, user_id).ok()?; - let (view_channel, send_messages, embed_links) = ctx - .channel_id() - .to_channel(&ctx) - .await - .ok() - .and_then(|c| { - if let Channel::Guild(channel) = c { - let perms = channel.permissions_for_user(&ctx, user_id).ok()?; + Some(( + perms.view_channel(), + perms.send_messages(), + perms.embed_links(), + perms.manage_webhooks(), + )) + } else { + None + } + }) + .unwrap_or((false, false, false, false)); - Some((perms.view_channel(), perms.send_messages(), perms.embed_links())) - } else { - None - } - }) - .unwrap_or((false, false, false)); - - if manage_webhooks && send_messages && embed_links { - true - } else { - let _ = ctx - .send(CreateReply::default().content(format!( - "Please ensure the bot has the correct permissions: + if view_channel && manage_webhooks && send_messages && embed_links { + true + } else { + let _ = ctx + .send(CreateReply::default().content(format!( + "Please ensure the bot has the correct permissions: {} **View Channel** {} **Send Message** {} **Embed Links** {} **Manage Webhooks**", - if view_channel { "✅" } else { "❌" }, - if send_messages { "✅" } else { "❌" }, - if embed_links { "✅" } else { "❌" }, - if manage_webhooks { "✅" } else { "❌" }, - ))) - .await; + if view_channel { "✅" } else { "❌" }, + if send_messages { "✅" } else { "❌" }, + if embed_links { "✅" } else { "❌" }, + if manage_webhooks { "✅" } else { "❌" }, + ))) + .await; - false - } - } else { - true + false } } diff --git a/src/main.rs b/src/main.rs index d2ca4cb..5076edd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,7 +49,6 @@ type ApplicationContext<'a> = poise::ApplicationContext<'a, Data, Error>; pub struct Data { database: Pool, - http: reqwest::Client, recording_macros: RwLock>, popular_timezones: Vec, _broadcast: Sender<()>, @@ -252,7 +251,6 @@ async fn _main(tx: Sender<()>) -> Result<(), Box> { } Ok(Data { - http: reqwest::Client::new(), database, popular_timezones, recording_macros: Default::default(), diff --git a/src/models/command_macro.rs b/src/models/command_macro.rs index dc86cfc..506bebb 100644 --- a/src/models/command_macro.rs +++ b/src/models/command_macro.rs @@ -1,7 +1,6 @@ use chrono_tz::Tz; use poise::serenity_prelude::model::id::GuildId; use serde::{Deserialize, Serialize}; -use serde_json::Value; use crate::{ApplicationContext, Context};