From 915ef54dc3bd8de729e35e286efbc9ed53b39768 Mon Sep 17 00:00:00 2001 From: seth Date: Sun, 3 Mar 2024 18:32:44 -0500 Subject: [PATCH] log more actions + tidy up things --- src/commands/general/joke.rs | 2 ++ src/commands/general/members.rs | 2 ++ src/commands/general/ping.rs | 2 ++ src/commands/general/rory.rs | 2 ++ src/commands/general/say.rs | 2 +- src/commands/general/stars.rs | 3 +++ src/commands/general/tag.rs | 7 ++++-- src/config/discord.rs | 28 +++++++++++++++++++----- src/handlers/event/delete_on_reaction.rs | 2 ++ src/handlers/event/eta.rs | 4 +++- src/handlers/event/pluralkit.rs | 6 ++--- src/handlers/event/support_onboard.rs | 6 ++--- src/main.rs | 3 ++- src/utils/resolve_message.rs | 6 +++-- 14 files changed, 56 insertions(+), 19 deletions(-) diff --git a/src/commands/general/joke.rs b/src/commands/general/joke.rs index e31c395..b6b2c60 100644 --- a/src/commands/general/joke.rs +++ b/src/commands/general/joke.rs @@ -2,10 +2,12 @@ use crate::api::dadjoke; use crate::Context; use eyre::Result; +use log::trace; /// It's a joke #[poise::command(slash_command, prefix_command)] pub async fn joke(ctx: Context<'_>) -> Result<()> { + trace!("Running joke command"); let joke = dadjoke::get_joke().await?; ctx.reply(joke).await?; diff --git a/src/commands/general/members.rs b/src/commands/general/members.rs index 5387681..39da469 100644 --- a/src/commands/general/members.rs +++ b/src/commands/general/members.rs @@ -1,12 +1,14 @@ use crate::{consts, Context}; use eyre::{OptionExt, Result}; +use log::trace; use poise::serenity_prelude::CreateEmbed; use poise::CreateReply; /// Returns the number of members in the server #[poise::command(slash_command, prefix_command)] pub async fn members(ctx: Context<'_>) -> Result<()> { + trace!("Running members command"); let guild = ctx.guild().ok_or_eyre("Couldn't fetch guild!")?.to_owned(); let count = guild.member_count; diff --git a/src/commands/general/ping.rs b/src/commands/general/ping.rs index 8e46531..f21a7c0 100644 --- a/src/commands/general/ping.rs +++ b/src/commands/general/ping.rs @@ -1,10 +1,12 @@ use crate::Context; use eyre::Result; +use log::trace; /// Replies with pong! #[poise::command(slash_command, prefix_command, ephemeral)] pub async fn ping(ctx: Context<'_>) -> Result<()> { + trace!("Running ping command!"); ctx.reply("Pong!").await?; Ok(()) } diff --git a/src/commands/general/rory.rs b/src/commands/general/rory.rs index ae61ab6..ea2b0a1 100644 --- a/src/commands/general/rory.rs +++ b/src/commands/general/rory.rs @@ -2,6 +2,7 @@ use crate::api::rory; use crate::Context; use eyre::Result; +use log::trace; use poise::serenity_prelude::{CreateEmbed, CreateEmbedFooter}; use poise::CreateReply; @@ -11,6 +12,7 @@ pub async fn rory( ctx: Context<'_>, #[description = "specify a Rory ID"] id: Option, ) -> Result<()> { + trace!("Running rory command"); let rory = rory::get(id).await?; let embed = { diff --git a/src/commands/general/say.rs b/src/commands/general/say.rs index 1413b60..eeb2e9b 100644 --- a/src/commands/general/say.rs +++ b/src/commands/general/say.rs @@ -22,7 +22,7 @@ pub async fn say(ctx: Context<'_>, #[description = "Just content?"] content: Str channel.say(ctx, &content).await?; ctx.say("I said what you said!").await?; - if let Some(channel_id) = ctx.data().config.discord.channels.say_log_channel_id { + if let Some(channel_id) = ctx.data().config.discord.channels().say_log_channel_id() { let log_channel = guild .channels .iter() diff --git a/src/commands/general/stars.rs b/src/commands/general/stars.rs index ecb4051..200a562 100644 --- a/src/commands/general/stars.rs +++ b/src/commands/general/stars.rs @@ -1,12 +1,15 @@ use crate::{consts, Context}; use eyre::{Context as _, Result}; +use log::trace; use poise::serenity_prelude::CreateEmbed; use poise::CreateReply; /// Returns GitHub stargazer count #[poise::command(slash_command, prefix_command)] pub async fn stars(ctx: Context<'_>) -> Result<()> { + trace!("Running stars command"); + let prismlauncher = ctx .data() .octocrab diff --git a/src/commands/general/tag.rs b/src/commands/general/tag.rs index ccf1325..5d95e28 100644 --- a/src/commands/general/tag.rs +++ b/src/commands/general/tag.rs @@ -3,7 +3,8 @@ use crate::tags::Tag; use crate::{consts, Context}; use std::env; -use eyre::{OptionExt, Result}; +use eyre::{eyre, Result}; +use log::trace; use once_cell::sync::Lazy; use poise::serenity_prelude::{Color, CreateEmbed, User}; use poise::CreateReply; @@ -18,11 +19,13 @@ pub async fn tag( #[description = "the copypasta you want to send"] name: Choice, user: Option, ) -> Result<()> { + trace!("Running tag command"); + let tag_file = name.as_str(); let tag = TAGS .iter() .find(|t| t.file_name == tag_file) - .ok_or_eyre("Tried to get non-existent tag: {tag_file}")?; + .ok_or_else(|| eyre!("Tried to get non-existent tag: {tag_file}"))?; let frontmatter = &tag.frontmatter; diff --git a/src/config/discord.rs b/src/config/discord.rs index 0e1ea7e..f5f9879 100644 --- a/src/config/discord.rs +++ b/src/config/discord.rs @@ -3,17 +3,21 @@ use std::str::FromStr; use log::{info, warn}; use poise::serenity_prelude::ChannelId; -#[derive(Debug, Clone, Default)] +#[derive(Clone, Copy, Debug, Default)] pub struct RefractionChannels { - pub say_log_channel_id: Option, + say_log_channel_id: Option, } -#[derive(Debug, Clone, Default)] +#[derive(Clone, Copy, Debug, Default)] pub struct Config { - pub channels: RefractionChannels, + channels: RefractionChannels, } impl RefractionChannels { + pub fn new(say_log_channel_id: Option) -> Self { + Self { say_log_channel_id } + } + pub fn new_from_env() -> Self { let say_log_channel_id = Self::get_channel_from_env("DISCORD_SAY_LOG_CHANNELID"); @@ -23,7 +27,7 @@ impl RefractionChannels { warn!("DISCORD_SAY_LOG_CHANNELID is empty; this will disable logging in your server."); } - Self { say_log_channel_id } + Self::new(say_log_channel_id) } fn get_channel_from_env(var: &str) -> Option { @@ -31,12 +35,24 @@ impl RefractionChannels { .ok() .and_then(|env_var| ChannelId::from_str(&env_var).ok()) } + + pub fn say_log_channel_id(self) -> Option { + self.say_log_channel_id + } } impl Config { + pub fn new(channels: RefractionChannels) -> Self { + Self { channels } + } + pub fn new_from_env() -> Self { let channels = RefractionChannels::new_from_env(); - Self { channels } + Self::new(channels) + } + + pub fn channels(self) -> RefractionChannels { + self.channels } } diff --git a/src/handlers/event/delete_on_reaction.rs b/src/handlers/event/delete_on_reaction.rs index 671a608..de3b314 100644 --- a/src/handlers/event/delete_on_reaction.rs +++ b/src/handlers/event/delete_on_reaction.rs @@ -1,4 +1,5 @@ use eyre::{Context as _, Result}; +use log::trace; use poise::serenity_prelude::{Context, InteractionType, Reaction}; pub async fn handle(ctx: &Context, reaction: &Reaction) -> Result<()> { @@ -17,6 +18,7 @@ pub async fn handle(ctx: &Context, reaction: &Reaction) -> Result<()> { && interaction.user == user && reaction.emoji.unicode_eq("❌") { + trace!("Deleting our own message at the request of {}", user.tag()); message.delete(ctx).await?; } } diff --git a/src/handlers/event/eta.rs b/src/handlers/event/eta.rs index db6cc41..b1c2d72 100644 --- a/src/handlers/event/eta.rs +++ b/src/handlers/event/eta.rs @@ -1,4 +1,5 @@ use eyre::Result; +use log::trace; use once_cell::sync::Lazy; use poise::serenity_prelude::{Context, Message}; use rand::seq::SliceRandom; @@ -6,7 +7,7 @@ use regex::Regex; static ETA_REGEX: Lazy = Lazy::new(|| Regex::new(r"\beta\b").unwrap()); -pub const ETA_MESSAGES: [&str; 16] = [ +const ETA_MESSAGES: [&str; 16] = [ "Sometime", "Some day", "Not far", @@ -27,6 +28,7 @@ pub const ETA_MESSAGES: [&str; 16] = [ pub async fn handle(ctx: &Context, message: &Message) -> Result<()> { if !ETA_REGEX.is_match(&message.content) { + trace!("The message '{}' (probably) doesn't say ETA", message.content); return Ok(()); } diff --git a/src/handlers/event/pluralkit.rs b/src/handlers/event/pluralkit.rs index 38a845f..e37b79a 100644 --- a/src/handlers/event/pluralkit.rs +++ b/src/handlers/event/pluralkit.rs @@ -2,14 +2,14 @@ use crate::{api, Data}; use std::time::Duration; use eyre::Result; -use log::{debug, trace}; +use log::trace; use poise::serenity_prelude::{Context, Message}; use tokio::time::sleep; const PK_DELAY_SEC: Duration = Duration::from_secs(1000); pub async fn is_message_proxied(message: &Message) -> Result { - debug!( + trace!( "Waiting on PluralKit API for {} seconds", PK_DELAY_SEC.as_secs() ); @@ -22,7 +22,7 @@ pub async fn is_message_proxied(message: &Message) -> Result { pub async fn handle(_: &Context, msg: &Message, data: &Data) -> Result<()> { if msg.webhook_id.is_some() { - debug!( + trace!( "Message {} has a webhook ID. Checking if it was sent through PluralKit", msg.id ); diff --git a/src/handlers/event/support_onboard.rs b/src/handlers/event/support_onboard.rs index 2d9fc52..ed170e1 100644 --- a/src/handlers/event/support_onboard.rs +++ b/src/handlers/event/support_onboard.rs @@ -1,12 +1,12 @@ use eyre::{eyre, OptionExt, Result}; -use log::debug; +use log::{debug, trace}; use poise::serenity_prelude::{ ChannelType, Context, CreateAllowedMentions, CreateMessage, GuildChannel, }; pub async fn handle(ctx: &Context, thread: &GuildChannel) -> Result<()> { if thread.kind != ChannelType::PublicThread { - debug!("Not doing support onboard in non-thread channel"); + trace!("Not doing support onboard in non-public thread channel"); return Ok(()); } @@ -15,7 +15,7 @@ pub async fn handle(ctx: &Context, thread: &GuildChannel) -> Result<()> { .ok_or_else(|| eyre!("Couldn't get parent ID from thread {}!", thread.name))? .name(ctx) .await - .unwrap_or(String::new()) + .unwrap_or_default() != "support" { debug!("Not posting onboarding message to threads outside of support"); diff --git a/src/main.rs b/src/main.rs index 3637034..94b35e6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use std::sync::Arc; use std::time::Duration; use eyre::{eyre, Context as _, Report, Result}; -use log::{info, warn}; +use log::{info, trace, warn}; use octocrab::Octocrab; use poise::{ @@ -71,6 +71,7 @@ async fn setup( "Couldn't connect to storage! Is your daemon running?" )); } + trace!("Redis connection looks good!"); poise::builtins::register_globally(ctx, &framework.options().commands).await?; info!("Registered global commands!"); diff --git a/src/utils/resolve_message.rs b/src/utils/resolve_message.rs index a1554c5..4e4d9b5 100644 --- a/src/utils/resolve_message.rs +++ b/src/utils/resolve_message.rs @@ -1,7 +1,7 @@ use std::str::FromStr; use eyre::{eyre, Context as _, Result}; -use log::debug; +use log::{debug, trace}; use once_cell::sync::Lazy; use poise::serenity_prelude::{ ChannelId, ChannelType, Colour, Context, CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter, @@ -32,7 +32,9 @@ pub async fn resolve(ctx: &Context, msg: &Message) -> Result> { let mut embeds: Vec = vec![]; - for (_, [_server_id, channel_id, message_id]) in matches { + for (url, [_server_id, channel_id, message_id]) in matches { + trace!("Attempting to resolve message {message_id} from URL {url}"); + let channel = ChannelId::from_str(channel_id) .wrap_err_with(|| format!("Couldn't parse channel ID {channel_id}!"))? .to_channel_cached(ctx.as_ref())