From 6ae2353c927d80e734d03b2c464ddc6926407ae2 Mon Sep 17 00:00:00 2001 From: jude Date: Sun, 20 Feb 2022 12:19:39 +0000 Subject: [PATCH] add distinct identifying names. log errors in run_macro --- Cargo.lock | 2 -- Cargo.toml | 3 +- src/commands/moderation_cmds.rs | 51 ++++++++++++++++++++++++++------- src/commands/reminder_cmds.rs | 22 +++++++------- src/commands/todo_cmds.rs | 50 +++++++++++++++++++++++++------- src/hooks.rs | 2 +- src/main.rs | 12 +++++--- 7 files changed, 102 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aa5f7e1..318b043 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1886,7 +1886,6 @@ checksum = "58893f751c9b0412871a09abd62ecd2a00298c6c83befa223ef98c52aef40cbe" [[package]] name = "poise" version = "0.1.0" -source = "git+https://github.com/kangalioo/poise?branch=master#38bcca284cbc9fb52cd770d7af64fbd4b3495cc8" dependencies = [ "async-trait", "futures-core", @@ -1901,7 +1900,6 @@ dependencies = [ [[package]] name = "poise_macros" version = "0.1.0" -source = "git+https://github.com/kangalioo/poise?branch=master#38bcca284cbc9fb52cd770d7af64fbd4b3495cc8" dependencies = [ "darling", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 15969dc..0104bb2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,8 @@ authors = ["jellywx "] edition = "2018" [dependencies] -poise = { git = "https://github.com/kangalioo/poise", branch = "master" } +#poise = { git = "https://github.com/kangalioo/poise", branch = "master" } +poise = { path = "/home/jude/poise" } dotenv = "0.15" tokio = { version = "1", features = ["process", "full"] } reqwest = "0.11" diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index e1b6939..329daa8 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -28,7 +28,7 @@ async fn timezone_autocomplete(ctx: Context<'_>, partial: String) -> Vec } /// Select your timezone -#[poise::command(slash_command)] +#[poise::command(slash_command, identifying_name = "timezone")] pub async fn timezone( ctx: Context<'_>, #[description = "Timezone to use from this list: https://gist.github.com/JellyWX/913dfc8b63d45192ad6cb54c829324ee"] @@ -150,13 +150,23 @@ WHERE } /// Record and replay command sequences -#[poise::command(slash_command, rename = "macro", check = "guild_only")] +#[poise::command( + slash_command, + rename = "macro", + check = "guild_only", + identifying_name = "macro_base" +)] pub async fn macro_base(_ctx: Context<'_>) -> Result<(), Error> { Ok(()) } /// Start recording up to 5 commands to replay -#[poise::command(slash_command, rename = "record", check = "guild_only")] +#[poise::command( + slash_command, + rename = "record", + check = "guild_only", + identifying_name = "record_macro" +)] pub async fn record_macro( ctx: Context<'_>, #[description = "Name for the new macro"] name: String, @@ -235,7 +245,7 @@ Please use `/macro finish` to end this recording before starting another.", slash_command, rename = "finish", check = "guild_only", - identifying_name = "macro_finish" + identifying_name = "finish_macro" )] pub async fn finish_macro(ctx: Context<'_>) -> Result<(), Error> { let key = (ctx.guild_id().unwrap(), ctx.author().id); @@ -288,7 +298,12 @@ pub async fn finish_macro(ctx: Context<'_>) -> Result<(), Error> { } /// List recorded macros -#[poise::command(slash_command, rename = "list", check = "guild_only")] +#[poise::command( + slash_command, + rename = "list", + check = "guild_only", + identifying_name = "list_macro" +)] pub async fn list_macro(ctx: Context<'_>) -> Result<(), Error> { let macros = ctx.command_macros().await?; @@ -304,7 +319,12 @@ pub async fn list_macro(ctx: Context<'_>) -> Result<(), Error> { } /// Run a recorded macro -#[poise::command(slash_command, rename = "run", check = "guild_only")] +#[poise::command( + slash_command, + rename = "run", + check = "guild_only", + identifying_name = "run_macro" +)] pub async fn run_macro( ctx: poise::ApplicationContext<'_, Data, Error>, #[description = "Name of macro to run"] @@ -317,13 +337,17 @@ pub async fn run_macro( for command in command_macro.commands { if let Some(action) = command.action { - (action)(poise::ApplicationContext { args: &command.options, ..ctx }) + match (action)(poise::ApplicationContext { args: &command.options, ..ctx }) .await - .ok() - .unwrap(); + { + Ok(()) => {} + Err(e) => { + println!("{:?}", e); + } + } } else { Context::Application(ctx) - .say(format!("Command \"{}\" failed to execute", command.command_name)) + .say(format!("Command \"{}\" not found", command.command_name)) .await?; } } @@ -338,7 +362,12 @@ pub async fn run_macro( } /// Delete a recorded macro -#[poise::command(slash_command, rename = "delete", check = "guild_only")] +#[poise::command( + slash_command, + rename = "delete", + check = "guild_only", + identifying_name = "delete_macro" +)] pub async fn delete_macro( ctx: Context<'_>, #[description = "Name of macro to delete"] diff --git a/src/commands/reminder_cmds.rs b/src/commands/reminder_cmds.rs index 007dcab..2e3fc3f 100644 --- a/src/commands/reminder_cmds.rs +++ b/src/commands/reminder_cmds.rs @@ -39,7 +39,7 @@ use crate::{ }; /// Pause all reminders on the current channel until a certain time or indefinitely -#[poise::command(slash_command)] +#[poise::command(slash_command, identifying_name = "pause")] pub async fn pause( ctx: Context<'_>, #[description = "When to pause until"] until: Option, @@ -90,7 +90,7 @@ pub async fn pause( } /// Move all reminders in the current server by a certain amount of time. Times get added together -#[poise::command(slash_command)] +#[poise::command(slash_command, identifying_name = "offset")] pub async fn offset( ctx: Context<'_>, #[description = "Number of hours to offset by"] hours: Option, @@ -147,7 +147,7 @@ WHERE FIND_IN_SET(channels.`channel`, ?)", } /// Nudge all future reminders on this channel by a certain amount (don't use for DST! See `/offset`) -#[poise::command(slash_command)] +#[poise::command(slash_command, identifying_name = "nudge")] pub async fn nudge( ctx: Context<'_>, #[description = "Number of minutes to nudge new reminders by"] minutes: Option, @@ -170,7 +170,7 @@ pub async fn nudge( } /// View reminders on a specific channel -#[poise::command(slash_command)] +#[poise::command(slash_command, identifying_name = "look")] pub async fn look( ctx: Context<'_>, #[description = "Channel to view reminders on"] channel: Option, @@ -260,7 +260,7 @@ pub async fn look( } /// Delete reminders -#[poise::command(slash_command, rename = "del")] +#[poise::command(slash_command, rename = "del", identifying_name = "delete")] pub async fn delete(ctx: Context<'_>) -> Result<(), Error> { let timezone = ctx.timezone().await; @@ -422,13 +422,13 @@ fn time_difference(start_time: NaiveDateTime) -> String { } /// Manage timers -#[poise::command(slash_command, rename = "timer")] +#[poise::command(slash_command, rename = "timer", identifying_name = "timer_base")] pub async fn timer_base(_ctx: Context<'_>) -> Result<(), Error> { Ok(()) } /// List the timers in this server or DM channel -#[poise::command(slash_command, rename = "list")] +#[poise::command(slash_command, rename = "list", identifying_name = "list_timer")] pub async fn list_timer(ctx: Context<'_>) -> Result<(), Error> { let owner = ctx.guild_id().map(|g| g.0).unwrap_or_else(|| ctx.author().id.0); @@ -452,7 +452,7 @@ pub async fn list_timer(ctx: Context<'_>) -> Result<(), Error> { } /// Start a new timer from now -#[poise::command(slash_command, rename = "start")] +#[poise::command(slash_command, rename = "start", identifying_name = "start_timer")] pub async fn start_timer( ctx: Context<'_>, #[description = "Name for the new timer"] name: String, @@ -482,7 +482,7 @@ pub async fn start_timer( } /// Delete a timer -#[poise::command(slash_command, rename = "delete")] +#[poise::command(slash_command, rename = "delete", identifying_name = "delete_timer")] pub async fn delete_timer( ctx: Context<'_>, #[description = "Name of timer to delete"] name: String, @@ -509,8 +509,8 @@ pub async fn delete_timer( } /// Create a new reminder -#[poise::command(slash_command)] -pub(crate) async fn remind( +#[poise::command(slash_command, identifying_name = "remind")] +pub async fn remind( ctx: Context<'_>, #[description = "A description of the time to set the reminder for"] time: String, #[description = "The message content to send"] content: String, diff --git a/src/commands/todo_cmds.rs b/src/commands/todo_cmds.rs index 39bf319..bab13df 100644 --- a/src/commands/todo_cmds.rs +++ b/src/commands/todo_cmds.rs @@ -11,19 +11,29 @@ use crate::{ }; /// Manage todo lists -#[poise::command(slash_command, rename = "todo")] +#[poise::command(slash_command, rename = "todo", identifying_name = "todo_base")] pub async fn todo_base(_ctx: Context<'_>) -> Result<(), Error> { Ok(()) } /// Manage the server todo list -#[poise::command(slash_command, rename = "server", check = "guild_only")] +#[poise::command( + slash_command, + rename = "server", + check = "guild_only", + identifying_name = "todo_guild_base" +)] pub async fn todo_guild_base(_ctx: Context<'_>) -> Result<(), Error> { Ok(()) } /// Add an item to the server todo list -#[poise::command(slash_command, rename = "add")] +#[poise::command( + slash_command, + rename = "add", + check = "guild_only", + identifying_name = "todo_guild_add" +)] pub async fn todo_guild_add( ctx: Context<'_>, #[description = "The task to add to the todo list"] task: String, @@ -44,7 +54,12 @@ VALUES ((SELECT id FROM guilds WHERE guild = ?), ?)", } /// View and remove from the server todo list -#[poise::command(slash_command, rename = "view")] +#[poise::command( + slash_command, + rename = "view", + check = "guild_only", + identifying_name = "todo_guild_view" +)] pub async fn todo_guild_view(ctx: Context<'_>) -> Result<(), Error> { let values = sqlx::query!( "SELECT todos.id, value FROM todos @@ -71,13 +86,23 @@ WHERE guilds.guild = ?", } /// Manage the channel todo list -#[poise::command(slash_command, rename = "channel", check = "guild_only")] +#[poise::command( + slash_command, + rename = "channel", + check = "guild_only", + identifying_name = "todo_channel_base" +)] pub async fn todo_channel_base(_ctx: Context<'_>) -> Result<(), Error> { Ok(()) } /// Add an item to the channel todo list -#[poise::command(slash_command, rename = "add")] +#[poise::command( + slash_command, + rename = "add", + check = "guild_only", + identifying_name = "todo_channel_add" +)] pub async fn todo_channel_add( ctx: Context<'_>, #[description = "The task to add to the todo list"] task: String, @@ -99,7 +124,12 @@ VALUES ((SELECT id FROM guilds WHERE guild = ?), (SELECT id FROM channels WHERE } /// View and remove from the channel todo list -#[poise::command(slash_command, rename = "view")] +#[poise::command( + slash_command, + rename = "view", + check = "guild_only", + identifying_name = "todo_channel_view" +)] pub async fn todo_channel_view(ctx: Context<'_>) -> Result<(), Error> { let values = sqlx::query!( "SELECT todos.id, value FROM todos @@ -127,13 +157,13 @@ WHERE channels.channel = ?", } /// Manage your personal todo list -#[poise::command(slash_command, rename = "user")] +#[poise::command(slash_command, rename = "user", identifying_name = "todo_user_base")] pub async fn todo_user_base(_ctx: Context<'_>) -> Result<(), Error> { Ok(()) } /// Add an item to your personal todo list -#[poise::command(slash_command, rename = "add")] +#[poise::command(slash_command, rename = "add", identifying_name = "todo_user_add")] pub async fn todo_user_add( ctx: Context<'_>, #[description = "The task to add to the todo list"] task: String, @@ -154,7 +184,7 @@ VALUES ((SELECT id FROM users WHERE user = ?), ?)", } /// View and remove from your personal todo list -#[poise::command(slash_command, rename = "view")] +#[poise::command(slash_command, rename = "view", identifying_name = "todo_user_view")] pub async fn todo_user_view(ctx: Context<'_>) -> Result<(), Error> { let values = sqlx::query!( "SELECT todos.id, value FROM todos diff --git a/src/hooks.rs b/src/hooks.rs index 612c3ec..5fdf406 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -15,7 +15,7 @@ pub async fn guild_only(ctx: Context<'_>) -> Result { 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 != "macro_finish" { + 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)) { diff --git a/src/main.rs b/src/main.rs index 33299ff..ec07695 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ mod models; mod time_parser; mod utils; -use std::{collections::HashMap, env, sync::atomic::AtomicBool}; +use std::{collections::HashMap, env, fmt::Formatter, sync::atomic::AtomicBool}; use chrono_tz::Tz; use dotenv::dotenv; @@ -45,6 +45,12 @@ pub struct Data { is_loop_running: AtomicBool, } +impl std::fmt::Debug for Data { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "Data {{ .. }}") + } +} + #[tokio::main] async fn main() -> Result<(), Box> { env_logger::init(); @@ -120,9 +126,7 @@ async fn main() -> Result<(), Box> { Pool::connect(&env::var("DATABASE_URL").expect("No database URL provided")).await.unwrap(); let popular_timezones = sqlx::query!( - " -SELECT timezone FROM users GROUP BY timezone ORDER BY COUNT(timezone) DESC LIMIT 21 - " + "SELECT timezone FROM users GROUP BY timezone ORDER BY COUNT(timezone) DESC LIMIT 21" ) .fetch_all(&database) .await