log more actions + tidy up things

This commit is contained in:
seth 2024-03-03 18:32:44 -05:00
parent 651f14d724
commit 915ef54dc3
No known key found for this signature in database
GPG key ID: D31BD0D494BBEE86
14 changed files with 56 additions and 19 deletions

View file

@ -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?;

View file

@ -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;

View file

@ -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(())
}

View file

@ -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<u64>,
) -> Result<()> {
trace!("Running rory command");
let rory = rory::get(id).await?;
let embed = {

View file

@ -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()

View file

@ -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

View file

@ -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<User>,
) -> 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;

View file

@ -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<ChannelId>,
say_log_channel_id: Option<ChannelId>,
}
#[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<ChannelId>) -> 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<ChannelId> {
@ -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<ChannelId> {
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
}
}

View file

@ -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?;
}
}

View file

@ -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<Regex> = 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(());
}

View file

@ -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<bool> {
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<bool> {
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
);

View file

@ -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");

View file

@ -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!");

View file

@ -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<Vec<CreateEmbed>> {
let mut embeds: Vec<CreateEmbed> = 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())