generated from Oliver/discord-bot-template
startup info
This commit is contained in:
parent
2fbdeeef06
commit
78f5799748
@ -1,17 +1,20 @@
|
|||||||
use std::{collections::HashMap, fmt::Display, sync::Arc};
|
use std::{collections::HashMap, fmt::Display, fs, sync::Arc};
|
||||||
|
|
||||||
use crate::Context;
|
use crate::Context;
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use poise::{serenity_prelude::{Cache, CacheHttp, ChannelId, ChannelType, GetMessages, GuildChannel, Http, Message}, CreateReply};
|
use poise::{serenity_prelude::{Cache, CacheHttp, ChannelId, ChannelType, GetMessages, GuildChannel, Http, Message}, CreateReply};
|
||||||
|
use serde::Serialize;
|
||||||
use tokio::time::Instant;
|
use tokio::time::Instant;
|
||||||
use tracing::{debug, error, trace};
|
use tracing::{debug, error, info, trace};
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
struct Server {
|
struct Server {
|
||||||
channels: Vec<Channel>,
|
channels: Vec<Channel>,
|
||||||
orphanage: Vec<GuildChannel>,
|
orphanage: Vec<GuildChannel>,
|
||||||
needs_clean: bool,
|
needs_clean: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
struct Channel {
|
struct Channel {
|
||||||
this: GuildChannel,
|
this: GuildChannel,
|
||||||
children: Vec<Channel>,
|
children: Vec<Channel>,
|
||||||
@ -136,6 +139,7 @@ impl Server {
|
|||||||
|
|
||||||
/// Scrapes messages for all the channels in `self`.
|
/// Scrapes messages for all the channels in `self`.
|
||||||
async fn scrape_all(&mut self) {
|
async fn scrape_all(&mut self) {
|
||||||
|
// TODO add status from this command, just because it's cool
|
||||||
let cache: (&Arc<Cache>, &Http) = (&Arc::new(Cache::new()), &Http::new(&crate::ENV.token));
|
let cache: (&Arc<Cache>, &Http) = (&Arc::new(Cache::new()), &Http::new(&crate::ENV.token));
|
||||||
walk_channels(&mut self.channels, cache).await;
|
walk_channels(&mut self.channels, cache).await;
|
||||||
|
|
||||||
@ -198,11 +202,20 @@ impl Server {
|
|||||||
}
|
}
|
||||||
walk(&self.channels)
|
walk(&self.channels)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[poise::command(slash_command, rename = "scrape_all", guild_only)]
|
#[poise::command(slash_command, rename = "scrape_all", guild_only)]
|
||||||
pub async fn scrape_all(ctx: Context<'_>) -> Result<(), Error> {
|
pub async fn scrape_all(ctx: Context<'_>) -> Result<(), Error> {
|
||||||
let guild = ctx.guild_id().unwrap().to_partial_guild(ctx.serenity_context()).await.unwrap();
|
let guild = ctx.guild_id().unwrap().to_partial_guild(ctx.serenity_context()).await.unwrap();
|
||||||
|
|
||||||
|
let invoker = ctx.author().name.clone();
|
||||||
|
if let Some(nickname) = ctx.author().nick_in(ctx.http(), guild.id).await {
|
||||||
|
info!("{invoker} ({nickname}) started a scrape of {}", guild.name);
|
||||||
|
} else {
|
||||||
|
info!("{invoker} started a scrape of {}", guild.name);
|
||||||
|
}
|
||||||
|
|
||||||
if let Ok(map) = guild.channels(ctx.http()).await {
|
if let Ok(map) = guild.channels(ctx.http()).await {
|
||||||
let mut server = index(map).await;
|
let mut server = index(map).await;
|
||||||
match ctx.reply("Starting scrape...").await {
|
match ctx.reply("Starting scrape...").await {
|
||||||
@ -211,8 +224,21 @@ pub async fn scrape_all(ctx: Context<'_>) -> Result<(), Error> {
|
|||||||
server.scrape_all().await;
|
server.scrape_all().await;
|
||||||
let end = start.elapsed().as_millis();
|
let end = start.elapsed().as_millis();
|
||||||
let msg_count = server.message_count();
|
let msg_count = server.message_count();
|
||||||
|
|
||||||
|
match serde_json::to_string(&server) {
|
||||||
|
Ok(ok) => {
|
||||||
|
if let Err(e) = fs::write("server.json", ok) {
|
||||||
|
error!("Problem writing server to disk: {e}");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
error!("Trying to serialize server: {err}");
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Done. Print stats.
|
||||||
let _ = ok.edit(ctx, CreateReply::default().content(
|
let _ = ok.edit(ctx, CreateReply::default().content(
|
||||||
&format!("Done. Stats: \n```toml\nMessages: {msg_count}\nElapsed time: {end}ms\n```")
|
&format!("Done. Stats: \n```toml\nMessages saved: {msg_count}\nElapsed time: {end}ms\n```")
|
||||||
)).await;
|
)).await;
|
||||||
debug!("Scraped server in {}ms", end);
|
debug!("Scraped server in {}ms", end);
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user