Skip to content

Commit

Permalink
Add logging to discord bot
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Jan 13, 2025
1 parent a1e808a commit 6b88e1a
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 66 deletions.
1 change: 1 addition & 0 deletions discord_bot/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions discord_bot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ hex-literal = "0.4.1"
hmac = "0.12.1"
indoc = "2.0.4"
itertools = "0.12.0"
log = "0.4.22"
once_cell = "1.19.0"
poise = "0.6.1"
rand = "0.8.5"
Expand Down
1 change: 1 addition & 0 deletions discord_bot/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ RUN apt-get update && \
rm -rf /var/lib/apt/lists/*
RUN /usr/sbin/update-ca-certificates
COPY --from=builder /usr/local/cargo/bin/discord_bot /usr/local/bin/discord_bot
ENV RUST_LOG="discord_bot=debug"
CMD ["discord_bot"]
5 changes: 3 additions & 2 deletions discord_bot/src/clickup/get_task.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{webhook::TaskTag, WinstonError, CLIENT};
use log::warn;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use strum::IntoEnumIterator;
Expand All @@ -23,8 +24,8 @@ pub async fn get_task_from_clickup(task_id: &str) -> Result<Task, WinstonError>
match serde_json::from_str::<Task>(&text) {
Ok(task) => Ok(task),
Err(e) => {
eprintln!("failed to deserialize task: {}", e);
eprintln!("response: {}", text);
warn!("failed to deserialize task: {}", e);
warn!("response: {}", text);
Err(WinstonError::ParseJson(e))
}
}
Expand Down
5 changes: 3 additions & 2 deletions discord_bot/src/clickup/get_tasks.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use log::warn;
use url::Url;

use crate::{WinstonError, CLICKUP_LIST_ID, CLIENT};
Expand Down Expand Up @@ -47,8 +48,8 @@ pub async fn get_tasks(query: TasksQuery) -> Result<TaskListings, WinstonError>
match serde_json::from_str::<TaskListings>(&text) {
Ok(tasks) => Ok(tasks),
Err(e) => {
eprintln!("failed to deserialize task: {}", e);
eprintln!("response: {}", text);
warn!("failed to deserialize task: {}", e);
warn!("response: {}", text);
Err(WinstonError::ParseJson(e))
}
}
Expand Down
4 changes: 3 additions & 1 deletion discord_bot/src/clickup/move_done.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use log::info;

use crate::WinstonError;

use super::{get_tasks, update_task, TaskStatus, TasksQuery, UpdateTask};
Expand All @@ -8,7 +10,7 @@ pub async fn move_done_to_beta() -> Result<(), WinstonError> {
})
.await?;

println!(
info!(
"Moving tasks to beta: {}",
tasks
.tasks
Expand Down
12 changes: 8 additions & 4 deletions discord_bot/src/discord/close_ticket.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::{self, Display, Formatter};

use indoc::formatdoc;
use log::{debug, warn};
use poise::{
serenity_prelude::{
ButtonStyle, CacheHttp, ChannelId, CreateButton, CreateEmbed, CreateEmbedFooter,
Expand Down Expand Up @@ -141,7 +142,7 @@ pub async fn close_ticket(
channel.send_message(&ctx, message).await?;

if let Err(e) = remove_support_members_from_thread(&ctx, channel.id).await {
eprintln!("Could not remove members from thread: {e}");
warn!("Could not remove members from thread: {e}");
}

channel
Expand Down Expand Up @@ -181,8 +182,10 @@ where
continue;
}

debug!("Removing member from thread: {user_id}");

if let Err(e) = channel_id.remove_thread_member(&ctx, user_id).await {
eprintln!("Could not remove member from thread: {e}");
warn!("Could not remove member from thread: {e}");
}
}

Expand All @@ -194,21 +197,22 @@ async fn is_support(ctx: &impl CacheHttp, thread_member: ThreadMember) -> bool {
Some(member) => member,
None => {
let Some(guild_id) = thread_member.guild_id else {
warn!("Could not get guild from thread member");
return false;
};

let guild = match guild_id.to_partial_guild(&ctx).await {
Ok(guild) => guild,
Err(e) => {
eprintln!("Could not get guild from thread member: {e}");
warn!("Could not get guild from thread member: {e}");
return false;
}
};

match guild.member(ctx, thread_member.user_id).await {
Ok(member) => member,
Err(e) => {
eprintln!("Could not get member from thread member: {e}");
warn!("Could not get member from thread member: {e}");
return false;
}
}
Expand Down
3 changes: 2 additions & 1 deletion discord_bot/src/discord/create_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
clickup::{create_task_in_clickup, TaskPriority, TaskSize, TaskType},
remove_support_members_from_thread, Context, WinstonError,
};
use log::warn;
use poise::{
serenity_prelude::{CreateEmbed, CreateMessage},
CreateReply,
Expand Down Expand Up @@ -80,7 +81,7 @@ pub async fn create_task(
.await?;

if let Err(e) = remove_support_members_from_thread(&ctx, channel).await {
eprintln!("Could not remove members from thread: {e}");
warn!("Could not remove members from thread: {e}");
}

Ok(())
Expand Down
17 changes: 9 additions & 8 deletions discord_bot/src/discord/task_fixed.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use async_trait::async_trait;
use indoc::formatdoc;
use itertools::Itertools;
use log::warn;
use poise::serenity_prelude::{
ComponentInteraction, Context, EditInteractionResponse, EditMessage, EventHandler,
GuildChannel, Interaction, UserId,
Expand Down Expand Up @@ -29,7 +30,7 @@ impl EventHandler for TaskFixedHandler {
}

if let Err(err) = component.defer_ephemeral(&ctx).await {
eprintln!("Failed to defer ephemeral: {}", err);
warn!("Failed to defer ephemeral: {}", err);
return;
}

Expand All @@ -41,7 +42,7 @@ impl EventHandler for TaskFixedHandler {
"Something whent wrong with the button. It seems to be outdated.",
)
.await;
eprintln!("Invalid custom_id: {}", custom_id);
warn!("Invalid custom_id: {}", custom_id);
return;
}
let task_status = split[1].to_string();
Expand Down Expand Up @@ -71,7 +72,7 @@ impl EventHandler for TaskFixedHandler {
"Something whent wrong with the button. It seems to be outdated.",
)
.await;
eprintln!("Invalid task status: {}", task_status);
warn!("Invalid task status: {}", task_status);
return;
}
};
Expand All @@ -83,7 +84,7 @@ impl EventHandler for TaskFixedHandler {
format!("Failed to mark task as {}: {}", task_status, e),
)
.await;
eprintln!("Failed to mark task as {}: {}", task_status, e);
warn!("Failed to mark task as {}: {}", task_status, e);
return;
}
}
Expand Down Expand Up @@ -197,7 +198,7 @@ pub async fn update_response(
.await;

if let Err(e) = result {
eprintln!("Failed to update response: {}", e);
warn!("Failed to update response: {}", e);
}
}

Expand All @@ -212,7 +213,7 @@ pub async fn check_permissions(
"Something whent wrong with the button. It seems to be outdated.",
)
.await;
eprintln!("No channel found");
warn!("No channel found");
return None;
};

Expand All @@ -223,7 +224,7 @@ pub async fn check_permissions(
"Something whent wrong with the button. It seems to be outdated.",
)
.await;
eprintln!("No guild channel found");
warn!("No guild channel found");
return None;
};

Expand All @@ -234,7 +235,7 @@ pub async fn check_permissions(
"Something whent wrong with the button. It seems to be outdated.",
)
.await;
eprintln!("No owner found for channel: {}", guild_channel.name());
warn!("No owner found for channel: {}", guild_channel.name());
return None;
};

Expand Down
5 changes: 3 additions & 2 deletions discord_bot/src/discord/thread_archiving.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use async_trait::async_trait;
use log::warn;
use poise::serenity_prelude::{Context, EditThread, EventHandler, GuildChannel};

use crate::{CloseReason, QUESTIONS_FORUM_ID};
Expand Down Expand Up @@ -30,7 +31,7 @@ impl EventHandler for ThreadArchivingHandler {
let parent = match parent.to_channel(&ctx).await {
Ok(parent) => parent,
Err(e) => {
eprintln!("Error getting parent channel: {}", e);
warn!("Error getting parent channel: {}", e);
return;
}
};
Expand Down Expand Up @@ -64,7 +65,7 @@ impl EventHandler for ThreadArchivingHandler {
.edit_thread(&ctx, EditThread::default().archived(has_close_tag))
.await
{
eprintln!("Error editing thread: {}", e);
warn!("Error editing thread: {}", e);
return;
}
}
Expand Down
9 changes: 5 additions & 4 deletions discord_bot/src/discord/thread_cleanup.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use chrono::Duration;
use indoc::formatdoc;
use log::{info, warn};
use poise::serenity_prelude::{
model::channel, ButtonStyle, Context, CreateButton, CreateEmbed, CreateMessage, EditThread,
ForumTag, ForumTagId, Mentionable, ReactionType, Timestamp,
Expand Down Expand Up @@ -82,7 +83,7 @@ async fn archive_thread(
return Ok(());
}

println!("Archiving thread {} ({})", thread.id, thread.name());
info!("Archiving thread {} ({})", thread.id, thread.name());

thread
.edit_thread(&discord, EditThread::default().archived(true))
Expand All @@ -107,7 +108,7 @@ async fn resolve_answered_thread(
return Ok(());
}

println!("Auto Resolving thread {} ({})", thread.id, thread.name());
info!("Auto Resolving thread {} ({})", thread.id, thread.name());

// Close the thread
let Some(resolved_tag) = available_tags.get_tag_id("resolved") else {
Expand Down Expand Up @@ -145,7 +146,7 @@ async fn resolve_answered_thread(
.await?;

if let Err(e) = remove_support_members_from_thread(&discord, thread.id).await {
eprintln!("Could not remove members from thread: {e}");
warn!("Could not remove members from thread: {e}");
}

Ok(())
Expand All @@ -166,7 +167,7 @@ async fn reask_verification(
return Ok(());
}

println!(
info!(
"Reasking verification for thread {} ({})",
thread.id,
thread.name()
Expand Down
11 changes: 6 additions & 5 deletions discord_bot/src/discord/thread_closed_blocker.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use async_trait::async_trait;
use indoc::formatdoc;
use log::warn;
use poise::serenity_prelude::{
Context, CreateMessage, EditMessage, EventHandler, Mentionable, Message,
};
Expand Down Expand Up @@ -31,7 +32,7 @@ impl EventHandler for ThreadClosedBlockerHandler {
let parent = match parent.to_channel(&ctx).await {
Ok(parent) => parent,
Err(e) => {
eprintln!("Error getting parent channel: {}", e);
warn!("Error getting parent channel: {}", e);
return;
}
};
Expand Down Expand Up @@ -80,14 +81,14 @@ impl EventHandler for ThreadClosedBlockerHandler {
.await;

if let Err(e) = new_message.delete(&ctx).await {
eprintln!("Error deleting message: {}", e);
warn!("Error deleting message: {}", e);
return;
}

let mut error_message = match error_message {
Ok(error_message) => error_message,
Err(e) => {
eprintln!("Error sending error message: {}", e);
warn!("Error sending error message: {}", e);
return;
}
};
Expand All @@ -114,15 +115,15 @@ impl EventHandler for ThreadClosedBlockerHandler {
)
.await
{
eprintln!("Error editing error message: {}", e);
warn!("Error editing error message: {}", e);
return;
}

tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}

if let Err(e) = error_message.delete(&ctx).await {
eprintln!("Error deleting error message: {}", e);
warn!("Error deleting error message: {}", e);
return;
}
}
Expand Down
Loading

0 comments on commit 6b88e1a

Please sign in to comment.