Add better logging + Clippy warnings fix

This commit is contained in:
Oliver Atkinson 2024-07-25 13:57:49 -06:00
parent 4e9a70d60c
commit 2fbdeeef06

View File

@ -3,6 +3,7 @@ use std::{collections::HashMap, fmt::Display, sync::Arc};
use crate::Context;
use anyhow::Error;
use poise::{serenity_prelude::{Cache, CacheHttp, ChannelId, ChannelType, GetMessages, GuildChannel, Http, Message}, CreateReply};
use tokio::time::Instant;
use tracing::{debug, error, trace};
struct Server {
@ -86,9 +87,8 @@ impl Server {
if child.this.id == *find {
return Some(child);
}
match Self::search_by_id(&mut child.children, find) {
Some(x) => return Some(x),
None => {},
if let Some(x) = Self::search_by_id(&mut child.children, find) {
return Some(x);
}
}
None
@ -99,7 +99,7 @@ impl Server {
if let Some(parent_id) = &insert.parent_id {
// find the parent (needs to go thru all nodes)
match Self::search_by_id(&mut self.channels, &parent_id) {
match Self::search_by_id(&mut self.channels, parent_id) {
Some(parent_node) => {
parent_node.children.push(Channel::new(insert));
},
@ -141,7 +141,10 @@ impl Server {
/// Recursive walk thru the channels
async fn walk_channels(all: &mut Vec<Channel>, cache: impl CacheHttp + Clone) {
let settings = GetMessages::default().limit(5);
// Qty of messages to take at a time, max=100
let batch_size = 100;
let settings = GetMessages::default().limit(batch_size);
for channel in all {
// Clone *should* be cheap - it's Arc under the hood
// get the messages
@ -169,7 +172,11 @@ impl Server {
}
}
},
Err(e) => error!("Error while trying to get messages - {e}"),
Err(e) => {
error!("While reading messages in \"{}\" before `{}` - {e}", channel.this.name, last);
// Stop reading this channel on an error.
last_id = None;
},
}
}
// Then recurse into children channels
@ -200,8 +207,14 @@ pub async fn scrape_all(ctx: Context<'_>) -> Result<(), Error> {
let mut server = index(map).await;
match ctx.reply("Starting scrape...").await {
Ok(ok) => {
let start = Instant::now();
server.scrape_all().await;
let _ = ok.edit(ctx, CreateReply::default().content(&format!("Scraped {} messages", server.message_count()))).await;
let end = start.elapsed().as_millis();
let msg_count = server.message_count();
let _ = ok.edit(ctx, CreateReply::default().content(
&format!("Done. Stats: \n```toml\nMessages: {msg_count}\nElapsed time: {end}ms\n```")
)).await;
debug!("Scraped server in {}ms", end);
},
Err(e) => error!("{e} - While trying to reply to scrape command"),
}