diff --git a/Cargo.lock b/Cargo.lock index 04c1b9d..89823f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -115,16 +115,16 @@ dependencies = [ "log", "pin-project-lite", "tokio", - "tokio-rustls 0.23.4", + "tokio-rustls", "tungstenite", - "webpki-roots 0.22.4", + "webpki-roots", ] [[package]] name = "atoi" -version = "0.4.0" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "616896e05fc0e2649463a93a15183c6a16bf03413a7af88ef1285ddedfa9cda5" +checksum = "d7c57d12312ff59c811c0643f4d80830505833c9ffaebd193d819392b265be8e" dependencies = [ "num-traits", ] @@ -169,9 +169,9 @@ checksum = "3bdca834647821e0b13d9539a8634eb62d3501b6b6c2cec1722786ee6671b851" [[package]] name = "bigdecimal" -version = "0.2.2" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1e50562e37200edf7c6c43e54a08e64a5553bfb59d9c297d5572512aa517256" +checksum = "6aaf33151a6429fe9211d1b276eafdf70cdff28b071e76c0b0e1503221ea3744" dependencies = [ "num-bigint", "num-integer", @@ -281,6 +281,7 @@ dependencies = [ "chrono", "chrono-tz-build", "phf", + "serde", ] [[package]] @@ -353,18 +354,18 @@ dependencies = [ [[package]] name = "crc" -version = "2.1.0" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49fc9a695bca7f35f5f4c15cddc84415f66a74ea78eef08e90c5024f2b540e23" +checksum = "53757d12b596c16c78b83458d732a5d1a17ab3f53f2f7412f6fb57cc8a140ab3" dependencies = [ "crc-catalog", ] [[package]] name = "crc-catalog" -version = "1.1.1" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccaeedb56da03b09f598226e25e80088cb4cd25f316e6e4df7d695f0feeb1403" +checksum = "2d0165d2900ae6778e36e80bbc4da3b5eefccee9ba939761f9c2882a5d9af3ff" [[package]] name = "crc32fast" @@ -488,7 +489,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3495912c9c1ccf2e18976439f4443f3fee0fd61f424ff99fde6a66b15ecb448f" dependencies = [ "cfg-if 1.0.0", - "hashbrown 0.12.3", + "hashbrown", "lock_api", "parking_lot_core 0.9.3", "serde", @@ -566,12 +567,41 @@ dependencies = [ "subtle", ] +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi 0.3.9", +] + [[package]] name = "dotenv" version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" +[[package]] +name = "dotenvy" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e851a83c30366fd01d75b913588e95e74a1705c1ecc5d58b1f8e1a6d556525f" +dependencies = [ + "dirs", +] + [[package]] name = "either" version = "1.7.0" @@ -589,9 +619,9 @@ dependencies = [ [[package]] name = "env_logger" -version = "0.8.4" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" +checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3" dependencies = [ "atty", "humantime", @@ -901,26 +931,20 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.11.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" dependencies = [ "ahash", ] -[[package]] -name = "hashbrown" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" - [[package]] name = "hashlink" -version = "0.7.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7249a3129cbc1ffccd74857f81464a323a152173cdb134e0fd81bc803b29facf" +checksum = "d452c155cb93fecdfb02a73dd57b5d8e442c2063bd7aac72f1bc5e4263a43086" dependencies = [ - "hashbrown 0.11.2", + "hashbrown", ] [[package]] @@ -1043,9 +1067,9 @@ checksum = "d87c48c02e0dc5e3b849a2041db3029fd066650f8f717c07bf8ed78ccb895cac" dependencies = [ "http", "hyper", - "rustls 0.20.6", + "rustls", "tokio", - "tokio-rustls 0.23.4", + "tokio-rustls", ] [[package]] @@ -1103,7 +1127,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" dependencies = [ "autocfg", - "hashbrown 0.12.3", + "hashbrown", "serde", ] @@ -1197,6 +1221,29 @@ dependencies = [ "winapi-build", ] +[[package]] +name = "lazy-regex" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6b12f2eb6ed7d39405c5eb25a034b4c106a9ad87a6d9be3298de6c5f32fd57d" +dependencies = [ + "lazy-regex-proc_macros", + "once_cell", + "regex", +] + +[[package]] +name = "lazy-regex-proc_macros" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2496e5264069bc726ccf37eb76b9cd89406ae110d836c3f76729f99c8a23293" +dependencies = [ + "proc-macro2", + "quote", + "regex", + "syn", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -1459,9 +1506,9 @@ dependencies = [ [[package]] name = "num-bigint" -version = "0.3.3" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f6f7833f2cbf2360a6cfd58cd41a53aa7a90bd4c202f5b1c7dd2ed73c57b2c3" +checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f" dependencies = [ "autocfg", "num-integer", @@ -2020,6 +2067,17 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom 0.2.7", + "redox_syscall", + "thiserror", +] + [[package]] name = "ref-cast" version = "1.0.8" @@ -2072,16 +2130,17 @@ version = "1.6.3" dependencies = [ "base64", "chrono", - "chrono-tz 0.5.3", + "chrono-tz 0.6.3", "dotenv", "env_logger", + "lazy-regex", "lazy_static", "levenshtein", "log", "num-integer", "poise", "postman", - "rand 0.7.3", + "rand 0.8.5", "regex", "reminder_web", "reqwest", @@ -2148,21 +2207,21 @@ dependencies = [ "native-tls", "percent-encoding", "pin-project-lite", - "rustls 0.20.6", + "rustls", "rustls-pemfile", "serde", "serde_json", "serde_urlencoded", "tokio", "tokio-native-tls", - "tokio-rustls 0.23.4", + "tokio-rustls", "tokio-util", "tower-service", "url", "wasm-bindgen", "wasm-bindgen-futures", "web-sys", - "webpki-roots 0.22.4", + "webpki-roots", "winreg", ] @@ -2285,7 +2344,7 @@ dependencies = [ "percent-encoding", "pin-project-lite", "ref-cast", - "rustls 0.20.6", + "rustls", "rustls-pemfile", "serde", "smallvec", @@ -2293,7 +2352,7 @@ dependencies = [ "state", "time 0.3.11", "tokio", - "tokio-rustls 0.23.4", + "tokio-rustls", "uncased", ] @@ -2317,19 +2376,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "rustls" -version = "0.19.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35edb675feee39aec9c99fa5ff985081995a06d594114ae14cbe797ad7b7a6d7" -dependencies = [ - "base64", - "log", - "ring", - "sct 0.6.1", - "webpki 0.21.4", -] - [[package]] name = "rustls" version = "0.20.6" @@ -2338,8 +2384,8 @@ checksum = "5aab8ee6c7097ed6057f43c187a62418d0c05a4bd5f18b3571db50ee0f9ce033" dependencies = [ "log", "ring", - "sct 0.7.0", - "webpki 0.22.0", + "sct", + "webpki", ] [[package]] @@ -2394,16 +2440,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" -[[package]] -name = "sct" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b362b83898e0e69f38515b82ee15aa80636befe47c3b6d3d89a911e78fc228ce" -dependencies = [ - "ring", - "untrusted", -] - [[package]] name = "sct" version = "0.7.0" @@ -2657,9 +2693,9 @@ dependencies = [ [[package]] name = "sqlx" -version = "0.5.13" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "551873805652ba0d912fec5bbb0f8b4cdd96baf8e2ebf5970e5671092966019b" +checksum = "788841def501aabde58d3666fcea11351ec3962e6ea75dbcd05c84a71d68bcd1" dependencies = [ "sqlx-core", "sqlx-macros", @@ -2667,9 +2703,9 @@ dependencies = [ [[package]] name = "sqlx-core" -version = "0.5.13" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e48c61941ccf5ddcada342cd59e3e5173b007c509e1e8e990dafc830294d9dc5" +checksum = "8c21d3b5e7cadfe9ba7cdc1295f72cc556c750b4419c27c219c0693198901f8e" dependencies = [ "ahash", "atoi", @@ -2681,6 +2717,7 @@ dependencies = [ "crc", "crossbeam-queue", "digest", + "dotenvy", "either", "event-listener", "futures-channel", @@ -2701,7 +2738,8 @@ dependencies = [ "percent-encoding", "rand 0.8.5", "rsa", - "rustls 0.19.1", + "rustls", + "rustls-pemfile", "serde", "serde_json", "sha-1", @@ -2713,17 +2751,16 @@ dependencies = [ "thiserror", "tokio-stream", "url", - "webpki 0.21.4", - "webpki-roots 0.21.1", + "webpki-roots", ] [[package]] name = "sqlx-macros" -version = "0.5.13" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc0fba2b0cae21fc00fe6046f8baa4c7fcb49e379f0f592b04696607f69ed2e1" +checksum = "4adfd2df3557bddd3b91377fc7893e8fa899e9b4061737cbade4e1bb85f1b45c" dependencies = [ - "dotenv", + "dotenvy", "either", "heck", "once_cell", @@ -2739,13 +2776,13 @@ dependencies = [ [[package]] name = "sqlx-rt" -version = "0.5.13" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4db708cd3e459078f85f39f96a00960bd841f66ee2a669e90bf36907f5a79aae" +checksum = "7be52fc7c96c136cedea840ed54f7d446ff31ad670c9dea95ebcb998530971a3" dependencies = [ "once_cell", "tokio", - "tokio-rustls 0.22.0", + "tokio-rustls", ] [[package]] @@ -2960,26 +2997,15 @@ dependencies = [ "tokio", ] -[[package]] -name = "tokio-rustls" -version = "0.22.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc6844de72e57df1980054b38be3a9f4702aba4858be64dd700181a8a6d0e1b6" -dependencies = [ - "rustls 0.19.1", - "tokio", - "webpki 0.21.4", -] - [[package]] name = "tokio-rustls" version = "0.23.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c43ee83903113e03984cb9e5cebe6c04a5116269e900e3ddba8f068a62adda59" dependencies = [ - "rustls 0.20.6", + "rustls", "tokio", - "webpki 0.22.0", + "webpki", ] [[package]] @@ -3104,12 +3130,12 @@ dependencies = [ "httparse", "log", "rand 0.8.5", - "rustls 0.20.6", + "rustls", "sha-1", "thiserror", "url", "utf-8", - "webpki 0.22.0", + "webpki", ] [[package]] @@ -3415,16 +3441,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "webpki" -version = "0.21.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8e38c0608262c46d4a56202ebabdeb094cef7e560ca7a226c6bf055188aa4ea" -dependencies = [ - "ring", - "untrusted", -] - [[package]] name = "webpki" version = "0.22.0" @@ -3435,22 +3451,13 @@ dependencies = [ "untrusted", ] -[[package]] -name = "webpki-roots" -version = "0.21.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aabe153544e473b775453675851ecc86863d2a81d786d741f6b76778f2a48940" -dependencies = [ - "webpki 0.21.4", -] - [[package]] name = "webpki-roots" version = "0.22.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1c760f0d366a6c24a02ed7816e23e691f5d92291f94d15e836006fd11b04daf" dependencies = [ - "webpki 0.22.0", + "webpki", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index d4c6383..9502464 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,20 +9,21 @@ poise = "0.2" dotenv = "0.15" tokio = { version = "1", features = ["process", "full"] } reqwest = "0.11" -regex = "1.4" +lazy-regex = "2.3.0" +regex = "1.6" log = "0.4" -env_logger = "0.8" +env_logger = "0.9" chrono = "0.4" -chrono-tz = { version = "0.5", features = ["serde"] } +chrono-tz = { version = "0.6", features = ["serde"] } lazy_static = "1.4" num-integer = "0.1" serde = "1.0" serde_json = "1.0" serde_repr = "0.1" rmp-serde = "0.15" -rand = "0.7" +rand = "0.8" levenshtein = "1.0" -sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "macros", "mysql", "bigdecimal", "chrono"]} +sqlx = { version = "0.6", features = ["runtime-tokio-rustls", "macros", "mysql", "bigdecimal", "chrono"]} base64 = "0.13" [dependencies.postman] diff --git a/postman/Cargo.toml b/postman/Cargo.toml index c72fdbc..2070b55 100644 --- a/postman/Cargo.toml +++ b/postman/Cargo.toml @@ -12,5 +12,5 @@ chrono-tz = { version = "0.5", features = ["serde"] } lazy_static = "1.4" num-integer = "0.1" serde = "1.0" -sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "macros", "mysql", "bigdecimal", "chrono", "json"]} +sqlx = { version = "0.6", features = ["runtime-tokio-rustls", "macros", "mysql", "bigdecimal", "chrono", "json"]} serenity = { version = "0.11.1", default-features = false, features = ["builder", "cache", "client", "gateway", "http", "model", "utils", "rustls_backend"] } diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index 266216c..dcdfe70 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -2,17 +2,19 @@ use std::collections::hash_map::Entry; use chrono::offset::Utc; use chrono_tz::{Tz, TZ_VARIANTS}; +use lazy_regex::regex; use levenshtein::levenshtein; -use poise::CreateReply; +use poise::{serenity_prelude::command::CommandOptionType, CreateReply}; +use serde_json::{json, Value}; use crate::{ component_models::pager::{MacroPager, Pager}, consts::{EMBED_DESCRIPTION_MAX_LENGTH, THEME_COLOR}, models::{ - command_macro::{guild_command_macro, CommandMacro}, + command_macro::{guild_command_macro, CommandMacro, RawCommandMacro}, CtxData, }, - Context, Data, Error, + Context, Data, Error, GuildId, }; async fn timezone_autocomplete(ctx: Context<'_>, partial: String) -> Vec { @@ -576,3 +578,172 @@ pub fn show_macro_page(macros: &[CommandMacro], page: usize) -> Crea reply } + +struct Alias { + id: u32, + guild_id: u32, + 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 * FROM command_aliases WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?)", + guild_id.0 + ) + .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.0, + cmd_macro.name, + cmd_macro.description, + cmd_macro.commands + ) + .execute(&mut transaction) + .await?; + + added_aliases += 1; + } + + None => {} + } + } + + transaction.commit().await?; + + ctx.send(|b| b.content(format!("Added {} macros.", added_aliases))).await?; + + Ok(()) +} + +// const REGEX_REMIND_COMMAND: Regex = RegexBuilder::new( +// r#"(?P(?:<@\d+>\s+|<@!\d+>\s+|<#\d+>\s+)*)(?P