This commit is contained in:
jude 2024-02-09 17:03:04 +00:00
parent def43bfa78
commit fa7ec8731b
4 changed files with 46 additions and 48 deletions

View File

@ -33,7 +33,11 @@ pub async fn record_macro(
let row = sqlx::query!( 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(), guild_id.get(),
name name
) )

View File

@ -1,3 +1,4 @@
use log::warn;
use poise::{serenity_prelude::model::channel::Channel, CreateReply}; use poise::{serenity_prelude::model::channel::Channel, CreateReply};
use crate::{consts::MACRO_MAX_COMMANDS, models::command_macro::RecordedCommand, Context, Error}; use crate::{consts::MACRO_MAX_COMMANDS, models::command_macro::RecordedCommand, Context, Error};
@ -6,6 +7,9 @@ async fn macro_check(ctx: Context<'_>) -> bool {
if let Context::Application(app_ctx) = ctx { if let Context::Application(app_ctx) = ctx {
if let Some(guild_id) = ctx.guild_id() { if let Some(guild_id) = ctx.guild_id() {
if ctx.command().identifying_name != "finish_macro" { if ctx.command().identifying_name != "finish_macro" {
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" { if ctx.command().identifying_name != "remind" {
let _ = ctx let _ = ctx
.send( .send(
@ -18,9 +22,6 @@ async fn macro_check(ctx: Context<'_>) -> bool {
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 { if command_macro.commands.len() >= MACRO_MAX_COMMANDS {
let _ = ctx let _ = ctx
.send( .send(
@ -52,15 +53,9 @@ async fn macro_check(ctx: Context<'_>) -> bool {
} }
async fn check_self_permissions(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 let (view_channel, send_messages, embed_links, manage_webhooks) = ctx
.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) = ctx
.channel_id() .channel_id()
.to_channel(&ctx) .to_channel(&ctx)
.await .await
@ -69,14 +64,19 @@ async fn check_self_permissions(ctx: Context<'_>) -> bool {
if let Channel::Guild(channel) = c { if let Channel::Guild(channel) = c {
let perms = channel.permissions_for_user(&ctx, user_id).ok()?; let perms = channel.permissions_for_user(&ctx, user_id).ok()?;
Some((perms.view_channel(), perms.send_messages(), perms.embed_links())) Some((
perms.view_channel(),
perms.send_messages(),
perms.embed_links(),
perms.manage_webhooks(),
))
} else { } else {
None None
} }
}) })
.unwrap_or((false, false, false)); .unwrap_or((false, false, false, false));
if manage_webhooks && send_messages && embed_links { if view_channel && manage_webhooks && send_messages && embed_links {
true true
} else { } else {
let _ = ctx let _ = ctx
@ -96,9 +96,6 @@ async fn check_self_permissions(ctx: Context<'_>) -> bool {
false false
} }
} else {
true
}
} }
pub async fn all_checks(ctx: Context<'_>) -> Result<bool, Error> { pub async fn all_checks(ctx: Context<'_>) -> Result<bool, Error> {

View File

@ -49,7 +49,6 @@ type ApplicationContext<'a> = poise::ApplicationContext<'a, Data, Error>;
pub struct Data { pub struct Data {
database: Pool<Database>, database: Pool<Database>,
http: reqwest::Client,
recording_macros: RwLock<HashMap<(GuildId, UserId), CommandMacro>>, recording_macros: RwLock<HashMap<(GuildId, UserId), CommandMacro>>,
popular_timezones: Vec<Tz>, popular_timezones: Vec<Tz>,
_broadcast: Sender<()>, _broadcast: Sender<()>,
@ -252,7 +251,6 @@ async fn _main(tx: Sender<()>) -> Result<(), Box<dyn StdError + Send + Sync>> {
} }
Ok(Data { Ok(Data {
http: reqwest::Client::new(),
database, database,
popular_timezones, popular_timezones,
recording_macros: Default::default(), recording_macros: Default::default(),

View File

@ -1,7 +1,6 @@
use chrono_tz::Tz; use chrono_tz::Tz;
use poise::serenity_prelude::model::id::GuildId; use poise::serenity_prelude::model::id::GuildId;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::{ApplicationContext, Context}; use crate::{ApplicationContext, Context};