From def43bfa784f374d02dffd3984e95e7886c8e03f Mon Sep 17 00:00:00 2001 From: jude Date: Tue, 6 Feb 2024 20:08:59 +0000 Subject: [PATCH] Refactor macros --- Cargo.lock | 8 +- Cargo.toml | 2 +- src/commands/command_macro/delete.rs | 8 +- src/commands/command_macro/list.rs | 4 +- src/commands/command_macro/migrate.rs | 229 -------------------------- src/commands/command_macro/mod.rs | 1 - src/commands/command_macro/record.rs | 2 +- src/commands/command_macro/run.rs | 20 +-- src/commands/reminder_cmds.rs | 19 +-- src/hooks.rs | 31 ++-- src/main.rs | 3 +- src/models/command_macro.rs | 94 ++++++----- src/models/mod.rs | 6 +- src/utils.rs | 4 +- 14 files changed, 98 insertions(+), 333 deletions(-) delete mode 100644 src/commands/command_macro/migrate.rs diff --git a/Cargo.lock b/Cargo.lock index fa147b7..b081a91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2067,9 +2067,9 @@ checksum = "69d3587f8a9e599cc7ec2c00e331f71c4e69a5f9a4b8a6efd5b07466b9736f9a" [[package]] name = "poise" -version = "0.6.1-rc1" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bde9f83da70341825e4116c06ffd5f1b23155121c2801ea29a8e178a7fc9857" +checksum = "1819d5a45e3590ef33754abce46432570c54a120798bdbf893112b4211fa09a6" dependencies = [ "async-trait", "derivative", @@ -2084,9 +2084,9 @@ dependencies = [ [[package]] name = "poise_macros" -version = "0.6.1-rc1" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "130fd27280c82e5ab5b147838b5ff9f9da33603fbadfff8ff613de530c12922d" +checksum = "8fa2c123c961e78315cd3deac7663177f12be4460f5440dbf62a7ed37b1effea" dependencies = [ "darling", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 4e13ab8..27cec1d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ license = "AGPL-3.0 only" description = "Reminder Bot for Discord, now in Rust" [dependencies] -poise = "0.6.1-rc1" +poise = "0.6.1" dotenv = "0.15" tokio = { version = "1", features = ["process", "full"] } reqwest = "0.11" diff --git a/src/commands/command_macro/delete.rs b/src/commands/command_macro/delete.rs index d54bf2a..80c73de 100644 --- a/src/commands/command_macro/delete.rs +++ b/src/commands/command_macro/delete.rs @@ -17,7 +17,13 @@ pub async fn delete_macro( ) -> Result<(), Error> { match sqlx::query!( " -SELECT id FROM macro WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?) AND name = ?", + SELECT m.id + FROM macro m + INNER JOIN guilds + ON guilds.guild = m.guild_id + WHERE guild = ? + AND m.name = ? + ", ctx.guild_id().unwrap().get(), name ) diff --git a/src/commands/command_macro/list.rs b/src/commands/command_macro/list.rs index 97c5dfb..a124205 100644 --- a/src/commands/command_macro/list.rs +++ b/src/commands/command_macro/list.rs @@ -28,11 +28,11 @@ pub async fn list_macro(ctx: Context<'_>) -> Result<(), Error> { Ok(()) } -pub fn max_macro_page(macros: &[CommandMacro]) -> usize { +pub fn max_macro_page(macros: &[CommandMacro]) -> usize { ((macros.len() as f64) / 25.0).ceil() as usize } -pub fn show_macro_page(macros: &[CommandMacro], page: usize) -> CreateReply { +pub fn show_macro_page(macros: &[CommandMacro], page: usize) -> CreateReply { let pager = MacroPager::new(page); if macros.is_empty() { diff --git a/src/commands/command_macro/migrate.rs b/src/commands/command_macro/migrate.rs deleted file mode 100644 index 5001551..0000000 --- a/src/commands/command_macro/migrate.rs +++ /dev/null @@ -1,229 +0,0 @@ -use lazy_regex::regex; -use poise::{serenity_prelude::CommandOptionType, CreateReply}; -use regex::Captures; -use serde_json::{json, Value}; - -use crate::{models::command_macro::RawCommandMacro, Context, Error, GuildId}; - -struct Alias { - name: String, - command: String, -} - -/// Migrate old $alias reminder commands to macros. Only macro names that are not taken will be used. -#[poise::command( - slash_command, - rename = "migrate", - guild_only = true, - default_member_permissions = "MANAGE_GUILD", - identifying_name = "migrate_macro" -)] -pub async fn migrate_macro(ctx: Context<'_>) -> Result<(), Error> { - let guild_id = ctx.guild_id().unwrap(); - let mut transaction = ctx.data().database.begin().await?; - - let aliases = sqlx::query_as!( - Alias, - "SELECT name, command FROM command_aliases WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?)", - guild_id.get() - ) - .fetch_all(&mut *transaction) - .await?; - - let mut added_aliases = 0; - - for alias in aliases { - match parse_text_command(guild_id, alias.name, &alias.command) { - Some(cmd_macro) => { - sqlx::query!( - "INSERT INTO macro (guild_id, name, description, commands) VALUES ((SELECT id FROM guilds WHERE guild = ?), ?, ?, ?)", - cmd_macro.guild_id.get(), - cmd_macro.name, - cmd_macro.description, - cmd_macro.commands - ) - .execute(&mut *transaction) - .await?; - - added_aliases += 1; - } - - None => {} - } - } - - transaction.commit().await?; - - ctx.send(CreateReply::default().content(format!("Added {} macros.", added_aliases))).await?; - - Ok(()) -} - -fn parse_text_command( - guild_id: GuildId, - alias_name: String, - command: &str, -) -> Option { - match command.split_once(" ") { - Some((command_word, args)) => { - let command_word = command_word.to_lowercase(); - - if command_word == "r" - || command_word == "i" - || command_word == "remind" - || command_word == "interval" - { - let matcher = regex!( - r#"(?P(?:<@\d+>\s+|<@!\d+>\s+|<#\d+>\s+)*)(?P