generated from Oliver/discord-bot-template
Compare commits
No commits in common. "77c0c7af7441e5555f526babfc6d10f7d0e383b4" and "2fbdeeef067a63541b8f9c80d4b904b86fbaae3f" have entirely different histories.
77c0c7af74
...
2fbdeeef06
@ -1,20 +1,17 @@
|
||||
use std::{collections::HashMap, fmt::Display, fs, sync::Arc};
|
||||
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 serde::Serialize;
|
||||
use tokio::time::Instant;
|
||||
use tracing::{debug, error, info, trace};
|
||||
use tracing::{debug, error, trace};
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Server {
|
||||
channels: Vec<Channel>,
|
||||
orphanage: Vec<GuildChannel>,
|
||||
needs_clean: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Channel {
|
||||
this: GuildChannel,
|
||||
children: Vec<Channel>,
|
||||
@ -139,7 +136,6 @@ impl Server {
|
||||
|
||||
/// Scrapes messages for all the channels in `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));
|
||||
walk_channels(&mut self.channels, cache).await;
|
||||
|
||||
@ -202,20 +198,11 @@ impl Server {
|
||||
}
|
||||
walk(&self.channels)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[poise::command(slash_command, rename = "scrape_all", guild_only)]
|
||||
pub async fn scrape_all(ctx: Context<'_>) -> Result<(), Error> {
|
||||
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 {
|
||||
let mut server = index(map).await;
|
||||
match ctx.reply("Starting scrape...").await {
|
||||
@ -224,21 +211,8 @@ pub async fn scrape_all(ctx: Context<'_>) -> Result<(), Error> {
|
||||
server.scrape_all().await;
|
||||
let end = start.elapsed().as_millis();
|
||||
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(
|
||||
&format!("Done. Stats: \n```toml\nMessages saved: {msg_count}\nElapsed time: {end}ms\n```")
|
||||
&format!("Done. Stats: \n```toml\nMessages: {msg_count}\nElapsed time: {end}ms\n```")
|
||||
)).await;
|
||||
debug!("Scraped server in {}ms", end);
|
||||
},
|
||||
@ -265,14 +239,8 @@ async fn index(map: HashMap<ChannelId, GuildChannel>) -> Server {
|
||||
// NOTE!!! Make sure these names in quotes are lowercase!
|
||||
#[poise::command(slash_command, rename = "index", guild_only)]
|
||||
pub async fn index_cmd(ctx: Context<'_>) -> Result<(), Error> {
|
||||
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}) is indexing {}", guild.name);
|
||||
} else {
|
||||
info!("{invoker} is indexing {}", guild.name);
|
||||
}
|
||||
|
||||
let guild = ctx.guild_id().unwrap().to_partial_guild(ctx.serenity_context()).await.unwrap();
|
||||
match guild.channels(ctx.http()).await {
|
||||
Ok(ok) => {
|
||||
let server = index(ok).await;
|
||||
|
Loading…
x
Reference in New Issue
Block a user