Compare commits

...

33 Commits

Author SHA1 Message Date
66e72aeb54 surreal testing
All checks were successful
Test Rust project / test (ubuntu-latest, stable) (push) Successful in 4m29s
2024-07-29 16:08:04 -06:00
a701716958 surreal v1 2024-07-29 15:00:38 -06:00
8e53f2a234 add deserializing 2024-07-29 15:00:10 -06:00
9492f378d3 re-arrange 2024-07-29 13:38:51 -06:00
c5cb4eb524 don't serialize empty vecs
All checks were successful
Test Rust project / test (ubuntu-latest, stable) (push) Successful in 2m47s
2024-07-29 12:27:22 -06:00
620b855f1d update deps 2024-07-29 12:27:10 -06:00
da7de4a19e not going to dockerize this 2024-07-29 12:09:39 -06:00
3f3159ba23 git settings updates 2024-07-29 12:09:24 -06:00
c4b68a130e add clippy
All checks were successful
Test Rust project / test (ubuntu-latest, nightly) (push) Successful in 2m47s
Test Rust project / test (ubuntu-latest, stable) (push) Successful in 2m44s
2024-07-29 08:57:47 -06:00
f74cca0083 Merge branch 'main' of https://git.oliveratkinson.net/Oliver/discord-egress 2024-07-29 08:56:28 -06:00
88dff426b1 use custom poise fork until it gets updated 2024-07-29 08:55:56 -06:00
f54b60b50b Update .gitea/workflows/clippy.yaml
Some checks failed
Test Rust project / test (ubuntu-latest, nightly) (push) Failing after 26s
Test Rust project / test (ubuntu-latest, stable) (push) Failing after 26s
2024-07-29 14:43:42 +00:00
a58d0d3969 Update .gitea/workflows/clippy.yaml
Some checks failed
Test Rust project / test (ubuntu-latest, nightly) (push) Failing after 4s
Test Rust project / test (ubuntu-latest, stable) (push) Failing after 4s
2024-07-29 14:35:49 +00:00
6df1e9fa7c Update .gitea/workflows/clippy.yaml 2024-07-29 14:34:56 +00:00
43ca1e6089 maybe?
Some checks failed
Test Rust project / test (ubuntu-latest, nightly) (push) Failing after 37s
Test Rust project / test (ubuntu-latest, stable) (push) Failing after 24s
2024-07-29 08:31:33 -06:00
Oliver Atkinson
09df72de7b trying new git workflow
Some checks failed
Test Rust project / test (ubuntu-latest, nightly) (push) Failing after 15s
Test Rust project / test (ubuntu-latest, stable) (push) Failing after 4s
2024-07-29 08:07:26 -06:00
460e832cbf pull env every time
Some checks failed
Cargo Build & Test / Rust project - latest (nightly) (push) Failing after 54s
2024-07-27 15:06:42 -06:00
369553f7c6 env not working :(
Some checks failed
Cargo Build & Test / Rust project - latest (nightly) (push) Failing after 27s
2024-07-27 15:02:43 -06:00
86da8a032c pull in env
Some checks failed
Cargo Build & Test / Rust project - latest (nightly) (push) Failing after 29s
2024-07-27 15:00:14 -06:00
d597b90fc2 manually install rust
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 5s
Cargo Build & Test / Rust project - latest (nightly) (push) Failing after 36s
2024-07-27 14:58:38 -06:00
cb79724a48 rust support?
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 5s
Cargo Build & Test / Rust project - latest (beta) (push) Failing after 3s
Cargo Build & Test / Rust project - latest (nightly) (push) Failing after 3s
Cargo Build & Test / Rust project - latest (stable) (push) Failing after 3s
2024-07-27 14:52:36 -06:00
65dd0d5c1d testing actions
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 59s
2024-07-27 14:46:30 -06:00
Oliver Atkinson
e005918cc3 add serde 2024-07-25 15:07:09 -06:00
Oliver Atkinson
8dbc7ee863 Put scrape behind owner privliages
I don't want normies tyring to scrape the whole server
2024-07-25 14:42:15 -06:00
Oliver Atkinson
77c0c7af74 add info to the invokation of index 2024-07-25 14:31:21 -06:00
Oliver Atkinson
78f5799748 startup info 2024-07-25 14:29:49 -06:00
Oliver Atkinson
2fbdeeef06 Add better logging + Clippy warnings fix 2024-07-25 13:57:49 -06:00
Oliver Atkinson
4e9a70d60c add time to logging
makes looking at a lot of the same message output easier
2024-07-25 13:57:25 -06:00
Oliver Atkinson
430d6b76c4 use custom poise version
There are errors in the poise dependency serenity, they aren't yet in the newest version of poise, so we are using a git version
2024-07-25 13:56:54 -06:00
Oliver Atkinson
4d3a41db43 fix copied code to be applicable to this codebase 2024-07-25 13:17:03 -06:00
Oliver Atkinson
54c533abff re-write scrape code so it actually works 2024-07-25 13:16:46 -06:00
Oliver Atkinson
b7c9a9b0e3 Fix message counting logic 2024-07-25 13:15:31 -06:00
Oliver Atkinson
79638ed324 add tracing 2024-07-25 10:56:45 -06:00
9 changed files with 2644 additions and 148 deletions

View File

@@ -0,0 +1,20 @@
name: Test Rust project
on: [push]
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
rust: [stable]
steps:
- uses: https://git.oliveratkinson.net/Oliver/setup-rust-action@master
with:
rust-version: ${{ matrix.rust }}
- uses: actions/checkout@master
- name: Run tests
run: cargo test --verbose
- name: Clippy
run: cargo clippy

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
/target /target
/.vscode /.vscode
.env .env
server.json

2199
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -10,8 +10,12 @@ edition = "2021"
[dependencies] [dependencies]
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] }
# songbird = { version = "0.3.2", features = ["yt-dlp"] } poise = { features = ["cache"], git="https://github.com/Rushmore75/poise.git", rev="6afaf44a791cd3ff590c89fda26c94b4411b3266" }
poise = { version = "0.6", features = ["cache"] }
dotenv = "0.15.0" dotenv = "0.15.0"
anyhow = "1.0.75" anyhow = "1.0.75"
once_cell = "1.19.0" once_cell = "1.19.0"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
serde_json = "1.0.120"
serde = { version = "1.0.204", features = ["derive"] }
surrealdb = "1.5.4"

View File

@@ -1,15 +0,0 @@
# Build the execuitible
FROM rustlang/rust:nightly as builder
ENV RUSTFLAGS=""
WORKDIR /bot
COPY . .
RUN cargo build --release
# Now make the runtime container
FROM debian:bookworm-slim
COPY --from=builder /bot/target/release/discord_egress /usr/local/bin/discord_egress
COPY Cargo.lock /
CMD ["/usr/local/bin/discord_egress"]

View File

@@ -2,6 +2,26 @@
egress a discord server egress a discord server
### NOTE:
Using custom version of poise (the discord library) because I need to use a specific version of serenity (the discord library for poise). Here's how to set it up:
1) Clone Poise next to this repo:
```shell
git clone https://github.com/serenity-rs/poise.git
```
2) Change the version of serenity that poise uses:
```diff
[dependencies.serenity]
- version = "x.xx.x"
+ git = "https://github.com/serenity-rs/serenity.git"
+ rev = "e34f4491ee3a0b20ea8dc30cbc77b257f402f692"
```
> Just make sure that the rev is a sha1 commit hash sometime after 7/25/24
## Getting started ## Getting started
* goto: [Discord applicatoins](https://discord.com/developers/applications) and * goto: [Discord applicatoins](https://discord.com/developers/applications) and
create an application. create an application.

View File

@@ -1,30 +0,0 @@
version: '3.1'
name: discord-egress
services:
bot:
container_name: discord-egress_bot
build: https://git.oliveratkinson.net/Oliver/discord-egress.git
restart: always
environment:
# These will read from the .env file
DISCORD_INTENTS: ${DISCORD_INTENTS}
DISCORD_TOKEN: ${DISCORD_TOKEN}
DISCORD_ID: ${DISCORD_ID}
networks:
- external
- internal
redis:
container_name: discord-egress_redis
image: redis
restart: always
ports:
- 6379:6379
networks:
- internal
networks:
internal:
driver: bridge
internal: true
external:
driver: bridge

View File

@@ -1,18 +1,26 @@
use std::{collections::HashMap, fmt::Display, sync::Arc}; use std::{collections::HashMap, fmt::Display, fs, hint::black_box, 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}; use poise::{
serenity_prelude::{
struct Server { Cache, CacheHttp, ChannelId, ChannelType, GetMessages, GuildChannel, Http, Message,
channels: Vec<Channel>, },
orphanage: Vec<GuildChannel>, CreateReply,
needs_clean: bool, };
} use serde::{Deserialize, Serialize};
use surrealdb::{engine::remote::ws::Ws, opt::auth::Root, sql::Thing, Surreal};
use tokio::time::Instant;
use tracing::{debug, error, info, trace};
#[derive(Serialize, Deserialize)]
struct Channel { struct Channel {
this: GuildChannel, this: GuildChannel,
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
children: Vec<Channel>, children: Vec<Channel>,
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
messages: Vec<Message>, messages: Vec<Message>,
} }
@@ -25,16 +33,34 @@ impl Channel {
messages: Vec::new(), messages: Vec::new(),
} }
} }
fn is_category(&self) -> bool {
!self.children.is_empty() && self.messages.is_empty()
}
}
#[derive(Serialize, Deserialize)]
struct Server {
#[serde(default)]
name: String,
channels: Vec<Channel>,
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
orphanage: Vec<GuildChannel>,
#[serde(skip_serializing)]
#[serde(default)]
needs_clean: bool,
} }
impl Display for Server { impl Display for Server {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn print(f: &mut std::fmt::Formatter<'_>, tab: usize, channel: &Vec<Channel>) { fn print(f: &mut std::fmt::Formatter<'_>, tab: usize, channel: &Vec<Channel>) {
for i in channel { for i in channel {
for _ in 0..tab { let _ = write!(f, "\t"); } for _ in 0..tab {
let _ = writeln!(f, "{} {}", prefix(i.this.kind),i.this.name); let _ = write!(f, "\t");
print(f, tab+1, &i.children); }
let _ = writeln!(f, "{} {}", prefix(i.this.kind), i.this.name);
print(f, tab + 1, &i.children);
} }
} }
@@ -58,11 +84,10 @@ impl Display for Server {
print(f, 0, &self.channels); print(f, 0, &self.channels);
if self.needs_clean { if self.needs_clean {
let _ = writeln!(f, "Orphans: (please clean() before displaying...)"); let _ = writeln!(f, "Orphans: (please clean() before displaying...)");
for i in &self.orphanage { for i in &self.orphanage {
let _ = write!(f, "{} {},", prefix(i.kind),i.name); let _ = write!(f, "{} {},", prefix(i.kind), i.name);
} }
} }
@@ -71,43 +96,41 @@ impl Display for Server {
} }
impl Server { impl Server {
fn new(name: impl Into<String>) -> Self {
fn new() -> Self {
Self { Self {
name: name.into(),
channels: Vec::new(), channels: Vec::new(),
orphanage: Vec::new(), orphanage: Vec::new(),
needs_clean: false needs_clean: false,
} }
} }
// TODO this might be broken
fn search_by_id<'a>(target: &'a mut Vec<Channel>, find: &ChannelId) -> Option<&'a mut Channel> { fn search_by_id<'a>(target: &'a mut Vec<Channel>, find: &ChannelId) -> Option<&'a mut Channel> {
for child in target { for child in target {
if child.this.id == *find { if child.this.id == *find {
return Some(child); return Some(child);
} }
match Self::search_by_id(&mut child.children, find) { if let Some(x) = Self::search_by_id(&mut child.children, find) {
Some(x) => return Some(x), return Some(x);
None => {},
} }
} }
None None
} }
fn add(&mut self, insert: GuildChannel) { fn add(&mut self, insert: GuildChannel) {
// make sure the new item wants a parent // make sure the new item wants a parent
if let Some(parent_id) = &insert.parent_id { if let Some(parent_id) = &insert.parent_id {
// find the parent (needs to go thru all nodes) // 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) => { Some(parent_node) => {
parent_node.children.push(Channel::new(insert)); parent_node.children.push(Channel::new(insert));
}, }
None => { None => {
// couldn't find parent, store somewhere else until it's parent is added... // couldn't find parent, store somewhere else until it's parent is added...
self.orphanage.push(insert); self.orphanage.push(insert);
self.needs_clean = true; self.needs_clean = true;
}, }
} }
} else { } else {
self.channels.push(Channel::new(insert)); self.channels.push(Channel::new(insert));
@@ -116,7 +139,9 @@ impl Server {
/// Cleans out the orphan channels, finding them parents. You'll want to use this before displaying anything. /// Cleans out the orphan channels, finding them parents. You'll want to use this before displaying anything.
fn clean(&mut self) { fn clean(&mut self) {
if !self.needs_clean {return;} if !self.needs_clean {
return;
}
// Look thru the orphanage and try to find parents // Look thru the orphanage and try to find parents
for orphan in &self.orphanage { for orphan in &self.orphanage {
@@ -136,31 +161,66 @@ 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) {
let settings = GetMessages::default().limit(5); // 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, settings).await; walk_channels(&mut self.channels, cache).await;
// recursive walk thru the channels /// Recursive walk thru the channels
async fn walk_channels(all: &mut Vec<Channel>, cache: impl CacheHttp + Clone, settings: GetMessages) { async fn walk_channels(all: &mut Vec<Channel>, cache: impl CacheHttp + Clone) {
// 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 { for channel in all {
// get the messages
match Box::pin(channel.this.messages(cache.clone(), settings)).await {
Ok(mesgs) => {
// store messages in our server object
channel.messages = mesgs;
if channel.messages.is_empty() {
eprintln!("{} was empty - (Or incorrect permissions)", channel.this.name);
}
},
Err(e) => {
eprintln!("{}", e);
},
}
// Clone *should* be cheap - it's Arc under the hood // Clone *should* be cheap - it's Arc under the hood
Box::pin(walk_channels(&mut channel.children, cache.clone(), settings)).await; // get the messages
get_messages(channel, cache.clone(), settings).await;
}
/// Get all messages for 1 channel and children
async fn get_messages(
channel: &mut Channel,
cache: impl CacheHttp + Clone,
settings: GetMessages,
) {
// Loop thru all the messages in the channel in batches.
// Adding each batch to the current channel's messages the whole time.
let mut last_id = channel.this.last_message_id;
while let Some(last) = last_id {
match channel
.this
.messages(cache.clone(), settings.before(last))
.await
{
Ok(mut ok) => {
if ok.is_empty() {
debug!("Reached the beginning of {}", channel.this.name);
// Stop the loop if there are no more messages.
last_id = None;
} else {
trace!("Adding {} messages to \"{}\"", ok.len(), channel.this.name);
channel.messages.append(&mut ok);
// extract message id
if let Some(l) = channel.messages.last() {
last_id = Some(l.id);
}
}
}
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
Box::pin(walk_channels(&mut channel.children, cache.clone())).await;
} }
} }
} }
/// Walk thru all the channels and count the saved messages. Will only give relevant data if /// Walk thru all the channels and count the saved messages. Will only give relevant data if
/// done after `scrape_all()`. /// done after `scrape_all()`.
@@ -168,35 +228,295 @@ impl Server {
fn walk(this: &Vec<Channel>) -> usize { fn walk(this: &Vec<Channel>) -> usize {
let mut total = 0; let mut total = 0;
for channel in this { for channel in this {
total += channel.messages.len();
total += walk(&channel.children); total += walk(&channel.children);
}; }
total total
} }
walk(&self.channels) walk(&self.channels)
} }
async fn to_surreal(&self) -> surrealdb::Result<()> {
trace!("Connecting to surrealdb...");
// Connect to the server
let db = Surreal::new::<Ws>("127.0.0.1:8000").await?;
db.signin(Root {
username: "root",
password: "root",
})
.await?;
db.use_ns("egress").use_db(self.name.clone()).await?;
// =========================================================
// Ingress data
// Data will only be in three layers
// Layer 1: Categories (no parent)
// Layer 2: Channels (might have parent)
// Layer 3: Messages (has parent)
trace!("Starting ingress...");
for cat in self.channels.iter() {
match cat.this.kind {
ChannelType::Text => {
// This is a text channel
#[derive(Serialize)]
struct ChannelWrapper {
name: String,
nsfw: bool,
discord_id: u64,
discord_parent_id: Option<u64>,
topic: String,
}
let chan = &cat.this;
let dpi = if let Some(val) = chan.parent_id {Some(val.get())} else { None };
let new_channel: Vec<Thing> = db
.create("channel")
.content(ChannelWrapper {
name: chan.name.to_owned(),
nsfw: chan.nsfw,
discord_id: chan.id.get(),
discord_parent_id: dpi,
topic: chan.topic.to_owned().unwrap_or(String::new()),
})
.await?;
for msg in cat.messages.iter() {
#[derive(Serialize)]
struct Author {
nickname: String,
username: String,
/// B64 encoded string of image (for now)
avatar: String,
id: u64,
}
#[derive(Serialize)]
struct Attachment {
content_type: String,
filename: String,
url: String,
}
#[derive(Serialize)]
struct Reaction {
count: u64,
emoji: String,
}
#[derive(Serialize)]
struct MessageWrapper {
// FIXME learn how to do references
parent: Thing,
author: Author,
content: String,
utc_timestamp: String,
mentions: Vec<u64>,
attachments: Vec<Attachment>,
reactions: Vec<Reaction>,
pinned: bool,
}
let _: Vec<Thing> = db
.create("message")
.content(MessageWrapper {
parent: new_channel[0].clone(),
author: Author {
id: msg.author.id.get(),
nickname: msg.author.name.to_owned(),
username: msg.author.global_name.clone().unwrap_or(String::new()),
avatar: {
match msg.author.avatar {
Some(hash) => {
format!(
"https://cdn.discordapp.com/avatars/{}/{}.webp",
msg.author.id,
hash,
)
},
None => String::new(),
}
}
},
content: msg.content.clone(),
utc_timestamp: msg.timestamp.to_utc().to_string(),
mentions: msg.mentions.iter().map(|f| f.id.get()).collect(),
attachments: msg.attachments.iter().map(|f| Attachment {
content_type: f.content_type.clone().unwrap_or(String::new()),
filename: f.filename.to_owned(),
url: f.url.to_owned(),
}).collect(),
reactions: msg.reactions.iter().map(|f| Reaction {
count: f.count,
emoji: f.reaction_type.as_data(),
}).collect(),
pinned: msg.pinned,
})
.await?;
}
}
ChannelType::Private => todo!(),
ChannelType::Voice => todo!(),
ChannelType::GroupDm => todo!(),
ChannelType::Category => todo!(),
ChannelType::News => todo!(),
ChannelType::NewsThread => todo!(),
ChannelType::PublicThread => todo!(),
ChannelType::PrivateThread => todo!(),
ChannelType::Stage => todo!(),
ChannelType::Directory => todo!(),
ChannelType::Forum => todo!(),
ChannelType::Unknown(_) => todo!(),
_ => todo!(),
}
// TODO learn why this is a vec
// Do the first iteration of channels a bit different, so as to name it "category".
let new_category: Vec<Thing> = db.create("category").content(&cat.this).await?;
import_messages(&cat.messages, &new_category[0], &db).await?;
// Ok, now automatically recurse the rest of the structure and auto import as channels
// and messages.
import_channel(&cat.children, &new_category[0], &db).await?;
}
async fn import_channel(
channels: &Vec<Channel>,
parent: &Thing,
db: &Surreal<surrealdb::engine::remote::ws::Client>,
) -> surrealdb::Result<()> {
for channel in channels.iter() {
trace!("Importing channel \"{}\"", channel.this.name);
#[derive(Serialize)]
struct ChannelWrapper<'a, 'b> {
channel: &'a GuildChannel,
surreal_parent: &'b Thing,
}
let new_channel: Vec<Thing> = db
.create("channel")
.content(ChannelWrapper {
channel: &channel.this,
surreal_parent: &parent,
})
.await?;
import_messages(&channel.messages, &new_channel[0], &db).await?;
// async recursion - thus box
Box::pin(import_channel(&channel.children, &new_channel[0], &db)).await?;
}
Ok(())
}
async fn import_messages(
msgs: &Vec<Message>,
parent: &Thing,
db: &Surreal<surrealdb::engine::remote::ws::Client>,
) -> surrealdb::Result<()> {
trace!("Importing {} messages...", msgs.len());
for msg in msgs.iter() {
#[derive(Serialize)]
struct MessageWrapper<'a, 'b> {
message: &'a Message,
surreal_parent: &'b Thing,
}
let created: Vec<Thing> = db
.create("message")
.content(MessageWrapper {
message: &msg,
surreal_parent: &parent,
})
.await?;
trace!("Imported message {:?}", created);
}
Ok(())
}
// Data is all in
// =========================================================
Ok(())
}
} }
#[poise::command(slash_command, rename = "scrape_all", guild_only)] #[poise::command(slash_command, rename = "scrape_all", guild_only, owners_only)]
pub async fn scrape_all(ctx: Context<'_>) -> Result<(), Error> { pub async fn scrape_all(ctx: Context<'_>, pretty_print: bool) -> Result<(), Error> {
let guild = ctx.guild_id().unwrap().to_partial_guild(ctx.serenity_context()).await.unwrap(); let guild = ctx
if let Ok(map) = guild.channels(ctx.http()).await { .guild_id()
let mut server = index(map).await; .unwrap()
server.scrape_all().await; .to_partial_guild(ctx.serenity_context())
.await
.unwrap();
let _ = ctx.reply(&format!("Scraped {} messages", server.message_count())).await; 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, guild.name).await;
match ctx.reply("Starting scrape...").await {
Ok(ok) => {
let start = Instant::now();
server.scrape_all().await;
let end = start.elapsed().as_millis();
let msg_count = server.message_count();
if let Err(e) = server.to_surreal().await {
error!("{e}");
};
// let print = if pretty_print {
// serde_json::to_string_pretty(&server)
// } else {
// serde_json::to_string(&server)
// };
// match print {
// 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```")
)).await;
debug!("Scraped server in {}ms", end);
}
Err(e) => error!("{e} - While trying to reply to scrape command"),
}
} }
Ok(()) Ok(())
} }
pub async fn from_json() {
let data = fs::read_to_string("server.json").unwrap();
let server: Server = serde_json::from_str(&data).unwrap();
server.to_surreal().await.unwrap();
}
/// Get server's topology (and runs clean) /// Get server's topology (and runs clean)
async fn index(map: HashMap<ChannelId, GuildChannel>) -> Server { async fn index(map: HashMap<ChannelId, GuildChannel>, name: impl Into<String>) -> Server {
let mut server = Server::new(); let mut server = Server::new(name);
// iterate thru all channels // iterate thru all channels
map.into_iter().for_each(|(_id, current)| { map.into_iter().for_each(|(_id, current)| {
// println!("{} {} {:?}", current.name, current.id, current.parent_id); // println!("{} {} {:?}", current.name, current.id, current.parent_id);
server.add(current); server.add(current);
// TODO take note of position // TODO Take note of position
// Take node of vc user limit // TODO Take node of vc user limit
}); });
server.clean(); server.clean();
server server
@@ -205,13 +525,24 @@ async fn index(map: HashMap<ChannelId, GuildChannel>) -> Server {
// NOTE!!! Make sure these names in quotes are lowercase! // NOTE!!! Make sure these names in quotes are lowercase!
#[poise::command(slash_command, rename = "index", guild_only)] #[poise::command(slash_command, rename = "index", guild_only)]
pub async fn index_cmd(ctx: Context<'_>) -> Result<(), Error> { 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 { match guild.channels(ctx.http()).await {
Ok(ok) => { Ok(ok) => {
let server = index(ok).await; let server = index(ok, guild.name).await;
let _ = ctx.reply(server.to_string()).await; let _ = ctx.reply(server.to_string()).await;
}, }
Err(_) => todo!(), Err(_) => todo!(),
} }
Ok(()) Ok(())

View File

@@ -1,5 +1,9 @@
use std::collections::HashSet;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use poise::serenity_prelude::{self as serenity, GatewayIntents}; use poise::serenity_prelude::{self as serenity, GatewayIntents, UserId};
use tracing::{debug, error, info, warn, Level};
use tracing_subscriber::EnvFilter;
mod command; mod command;
pub struct Data {} // User data, which is stored and accessible in all command invocations pub struct Data {} // User data, which is stored and accessible in all command invocations
@@ -11,17 +15,32 @@ static ENV: Lazy<BotEnv> = Lazy::new(|| {
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
command::from_json().await;
return;
// Start the tracing subscriber
let filter = EnvFilter::builder()
.parse("discord_egress=trace,tokio=warn")
.expect("Could not create env filter.")
;
tracing_subscriber::fmt::fmt()
.with_max_level(Level::TRACE)
.with_target(true)
.with_env_filter(filter)
.with_thread_ids(false)
.with_file(false)
.init();
// Generate sick text like this: // Generate sick text like this:
// http://www.patorjk.com/software/taag/#p=testall&f=Graffiti&t=hello%20world // http://www.patorjk.com/software/taag/#p=testall&f=Graffiti&t=hello%20world
println!(r#" info!("Invite with: https://discord.com/api/oauth2/authorize?client_id={}&permissions={}&scope=bot",
Invite this bot with:
"#);
println!("https://discord.com/api/oauth2/authorize?client_id={}&permissions={}&scope=bot",
ENV.id, ENV.id,
ENV.intents.bits(), ENV.intents.bits(),
); );
print!("\n");
let mut owners = HashSet::new();
owners.insert(UserId::new(423970006334832650));
// Setup framework // Setup framework
let framework = poise::Framework::builder() let framework = poise::Framework::builder()
@@ -30,6 +49,7 @@ async fn main() {
command::index_cmd(), command::index_cmd(),
command::scrape_all(), command::scrape_all(),
], ],
owners,
..Default::default() ..Default::default()
}) })
.setup(|ctx, _ready, framework| { .setup(|ctx, _ready, framework| {
@@ -65,9 +85,7 @@ fn read_env() -> BotEnv {
// ==================== ID =========================== // ==================== ID ===========================
let id: String = std::env::var(DISCORD_ID) let id: String = std::env::var(DISCORD_ID)
.unwrap_or_else(|_| { .unwrap_or_else(|_| {
println!(r#" warn!("Missing {DISCORD_ID} This isn't really that problematic, just that the generated invite link won't work.");
WARN: Missing {DISCORD_ID}
> This isn't really that problematic, just that the generated invite link won't work."#);
String::from("") String::from("")
}); });
// ==================== Token ======================== // ==================== Token ========================
@@ -76,8 +94,8 @@ WARN: Missing {DISCORD_ID}
// ==================== Intents ====================== // ==================== Intents ======================
let intents_env: String = std::env::var(DISCORD_INTENTS) let intents_env: String = std::env::var(DISCORD_INTENTS)
.unwrap_or_else(|msg| { .unwrap_or_else(|msg| {
println!("DEBUG: what is: {msg}"); debug!("What is: {msg}");
println!("ERROR: Missing {DISCORD_INTENTS}"); error!("Missing {DISCORD_INTENTS}");
"0".to_string() "0".to_string()
}); });
// ==================== Parse Intents ================= // ==================== Parse Intents =================
@@ -85,7 +103,7 @@ WARN: Missing {DISCORD_ID}
let intents_u64 = intents_env.parse::<u64>().unwrap_or(0u64); let intents_u64 = intents_env.parse::<u64>().unwrap_or(0u64);
let intents_truncated: GatewayIntents = GatewayIntents::from_bits_truncate(intents_u64); let intents_truncated: GatewayIntents = GatewayIntents::from_bits_truncate(intents_u64);
if intents_truncated.bits() != intents_u64 { if intents_truncated.bits() != intents_u64 {
println!("WARN: Intents integer got truncated from {} to {}!", intents_u64, intents_truncated.bits()) warn!("Intents integer got truncated from {} to {}!", intents_u64, intents_truncated.bits())
}; };
BotEnv { BotEnv {
intents: intents_truncated, intents: intents_truncated,