generated from Oliver/discord-bot-template
Compare commits
29 Commits
f433e0e4af
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c5cb4eb524 | |||
| 620b855f1d | |||
| da7de4a19e | |||
| 3f3159ba23 | |||
| c4b68a130e | |||
| f74cca0083 | |||
| 88dff426b1 | |||
| f54b60b50b | |||
| a58d0d3969 | |||
| 6df1e9fa7c | |||
| 43ca1e6089 | |||
|
|
09df72de7b | ||
| 460e832cbf | |||
| 369553f7c6 | |||
| 86da8a032c | |||
| d597b90fc2 | |||
| cb79724a48 | |||
| 65dd0d5c1d | |||
|
|
e005918cc3 | ||
|
|
8dbc7ee863 | ||
|
|
77c0c7af74 | ||
|
|
78f5799748 | ||
|
|
2fbdeeef06 | ||
|
|
4e9a70d60c | ||
|
|
430d6b76c4 | ||
|
|
4d3a41db43 | ||
|
|
54c533abff | ||
|
|
b7c9a9b0e3 | ||
|
|
79638ed324 |
20
.gitea/workflows/rust.yaml
Normal file
20
.gitea/workflows/rust.yaml
Normal 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
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
/target
|
||||
/.vscode
|
||||
.env
|
||||
server.json
|
||||
|
||||
150
Cargo.lock
generated
150
Cargo.lock
generated
@@ -354,7 +354,11 @@ dependencies = [
|
||||
"dotenv",
|
||||
"once_cell",
|
||||
"poise",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -731,6 +735,12 @@ dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.155"
|
||||
@@ -759,6 +769,15 @@ version = "0.4.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
||||
|
||||
[[package]]
|
||||
name = "matchers"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558"
|
||||
dependencies = [
|
||||
"regex-automata 0.1.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.4"
|
||||
@@ -816,6 +835,16 @@ dependencies = [
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nu-ansi-term"
|
||||
version = "0.46.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
|
||||
dependencies = [
|
||||
"overload",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-conv"
|
||||
version = "0.1.0"
|
||||
@@ -856,6 +885,12 @@ version = "1.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
||||
|
||||
[[package]]
|
||||
name = "overload"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.3"
|
||||
@@ -900,25 +935,25 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
||||
[[package]]
|
||||
name = "poise"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1819d5a45e3590ef33754abce46432570c54a120798bdbf893112b4211fa09a6"
|
||||
source = "git+https://github.com/Rushmore75/poise.git?rev=6afaf44a791cd3ff590c89fda26c94b4411b3266#6afaf44a791cd3ff590c89fda26c94b4411b3266"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"derivative",
|
||||
"futures-util",
|
||||
"indexmap",
|
||||
"parking_lot",
|
||||
"poise_macros",
|
||||
"regex",
|
||||
"serenity",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"trim-in-place",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "poise_macros"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fa2c123c961e78315cd3deac7663177f12be4460f5440dbf62a7ed37b1effea"
|
||||
source = "git+https://github.com/Rushmore75/poise.git?rev=6afaf44a791cd3ff590c89fda26c94b4411b3266#6afaf44a791cd3ff590c89fda26c94b4411b3266"
|
||||
dependencies = [
|
||||
"darling",
|
||||
"proc-macro2",
|
||||
@@ -1014,8 +1049,17 @@ checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
"regex-automata 0.4.7",
|
||||
"regex-syntax 0.8.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
|
||||
dependencies = [
|
||||
"regex-syntax 0.6.29",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1026,9 +1070,15 @@ checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
"regex-syntax 0.8.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.4"
|
||||
@@ -1280,8 +1330,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "serenity"
|
||||
version = "0.12.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "880a04106592d0a8f5bdacb1d935889bfbccb4a14f7074984d9cd857235d34ac"
|
||||
source = "git+https://github.com/serenity-rs/serenity.git?rev=e34f4491ee3a0b20ea8dc30cbc77b257f402f692#e34f4491ee3a0b20ea8dc30cbc77b257f402f692"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"async-trait",
|
||||
@@ -1321,6 +1370,15 @@ dependencies = [
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sharded-slab"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "skeptic"
|
||||
version = "0.13.7"
|
||||
@@ -1466,6 +1524,16 @@ dependencies = [
|
||||
"syn 2.0.72",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "1.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.3.36"
|
||||
@@ -1626,8 +1694,44 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"valuable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-log"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
|
||||
dependencies = [
|
||||
"log",
|
||||
"once_cell",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-subscriber"
|
||||
version = "0.3.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b"
|
||||
dependencies = [
|
||||
"matchers",
|
||||
"nu-ansi-term",
|
||||
"once_cell",
|
||||
"regex",
|
||||
"sharded-slab",
|
||||
"smallvec",
|
||||
"thread_local",
|
||||
"tracing",
|
||||
"tracing-core",
|
||||
"tracing-log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "trim-in-place"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "343e926fc669bc8cde4fa3129ab681c63671bae288b1f1081ceee6d9d37904fc"
|
||||
|
||||
[[package]]
|
||||
name = "triomphe"
|
||||
version = "0.1.13"
|
||||
@@ -1756,6 +1860,12 @@ version = "0.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.4"
|
||||
@@ -1891,6 +2001,22 @@ dependencies = [
|
||||
"rustls-pki-types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.8"
|
||||
@@ -1900,6 +2026,12 @@ dependencies = [
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "windows-core"
|
||||
version = "0.52.0"
|
||||
|
||||
@@ -10,8 +10,11 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] }
|
||||
# songbird = { version = "0.3.2", features = ["yt-dlp"] }
|
||||
poise = { version = "0.6", features = ["cache"] }
|
||||
poise = { features = ["cache"], git="https://github.com/Rushmore75/poise.git", rev="6afaf44a791cd3ff590c89fda26c94b4411b3266" }
|
||||
dotenv = "0.15.0"
|
||||
anyhow = "1.0.75"
|
||||
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"] }
|
||||
|
||||
15
Dockerfile
15
Dockerfile
@@ -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"]
|
||||
20
README.md
20
README.md
@@ -2,6 +2,26 @@
|
||||
|
||||
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
|
||||
* goto: [Discord applicatoins](https://discord.com/developers/applications) and
|
||||
create an application.
|
||||
|
||||
@@ -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
|
||||
146
src/command.rs
146
src/command.rs
@@ -1,18 +1,27 @@
|
||||
use std::{collections::HashMap, fmt::Display, sync::Arc};
|
||||
use std::{collections::HashMap, fmt::Display, fs, sync::Arc};
|
||||
|
||||
use crate::Context;
|
||||
use anyhow::Error;
|
||||
use poise::serenity_prelude::{Cache, CacheHttp, ChannelId, ChannelType, GetMessages, GuildChannel, Http, Message};
|
||||
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};
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Server {
|
||||
channels: Vec<Channel>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
orphanage: Vec<GuildChannel>,
|
||||
#[serde(skip_serializing)]
|
||||
needs_clean: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Channel {
|
||||
this: GuildChannel,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
children: Vec<Channel>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
messages: Vec<Message>,
|
||||
}
|
||||
|
||||
@@ -80,17 +89,15 @@ impl Server {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO this might be broken
|
||||
fn search_by_id<'a>(target: &'a mut Vec<Channel>, find: &ChannelId) -> Option<&'a mut Channel> {
|
||||
for child in target {
|
||||
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 +106,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));
|
||||
},
|
||||
@@ -136,31 +143,55 @@ impl Server {
|
||||
|
||||
/// Scrapes messages for all the channels in `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));
|
||||
walk_channels(&mut self.channels, cache, settings).await;
|
||||
walk_channels(&mut self.channels, cache).await;
|
||||
|
||||
// recursive walk thru the channels
|
||||
async fn walk_channels(all: &mut Vec<Channel>, cache: impl CacheHttp + Clone, settings: GetMessages) {
|
||||
/// Recursive walk thru the channels
|
||||
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 {
|
||||
// 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
|
||||
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
|
||||
/// done after `scrape_all()`.
|
||||
@@ -168,23 +199,62 @@ impl Server {
|
||||
fn walk(this: &Vec<Channel>) -> usize {
|
||||
let mut total = 0;
|
||||
for channel in this {
|
||||
total += channel.messages.len();
|
||||
total += walk(&channel.children);
|
||||
};
|
||||
total
|
||||
}
|
||||
walk(&self.channels)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[poise::command(slash_command, rename = "scrape_all", guild_only)]
|
||||
pub async fn scrape_all(ctx: Context<'_>) -> Result<(), Error> {
|
||||
#[poise::command(slash_command, rename = "scrape_all", guild_only, owners_only)]
|
||||
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 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;
|
||||
server.scrape_all().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();
|
||||
|
||||
let _ = ctx.reply(&format!("Scraped {} messages", server.message_count())).await;
|
||||
}
|
||||
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(())
|
||||
}
|
||||
|
||||
@@ -195,8 +265,8 @@ async fn index(map: HashMap<ChannelId, GuildChannel>) -> Server {
|
||||
map.into_iter().for_each(|(_id, current)| {
|
||||
// println!("{} {} {:?}", current.name, current.id, current.parent_id);
|
||||
server.add(current);
|
||||
// TODO take note of position
|
||||
// Take node of vc user limit
|
||||
// TODO Take note of position
|
||||
// TODO Take node of vc user limit
|
||||
});
|
||||
server.clean();
|
||||
server
|
||||
@@ -205,8 +275,14 @@ 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);
|
||||
}
|
||||
|
||||
match guild.channels(ctx.http()).await {
|
||||
Ok(ok) => {
|
||||
let server = index(ok).await;
|
||||
|
||||
40
src/main.rs
40
src/main.rs
@@ -1,5 +1,9 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
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;
|
||||
|
||||
pub struct Data {} // User data, which is stored and accessible in all command invocations
|
||||
@@ -12,16 +16,29 @@ static ENV: Lazy<BotEnv> = Lazy::new(|| {
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
||||
// 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:
|
||||
// http://www.patorjk.com/software/taag/#p=testall&f=Graffiti&t=hello%20world
|
||||
println!(r#"
|
||||
Invite this bot with:
|
||||
"#);
|
||||
println!("https://discord.com/api/oauth2/authorize?client_id={}&permissions={}&scope=bot",
|
||||
info!("Invite with: https://discord.com/api/oauth2/authorize?client_id={}&permissions={}&scope=bot",
|
||||
ENV.id,
|
||||
ENV.intents.bits(),
|
||||
);
|
||||
print!("\n");
|
||||
|
||||
let mut owners = HashSet::new();
|
||||
owners.insert(UserId::new(423970006334832650));
|
||||
|
||||
// Setup framework
|
||||
let framework = poise::Framework::builder()
|
||||
@@ -30,6 +47,7 @@ async fn main() {
|
||||
command::index_cmd(),
|
||||
command::scrape_all(),
|
||||
],
|
||||
owners,
|
||||
..Default::default()
|
||||
})
|
||||
.setup(|ctx, _ready, framework| {
|
||||
@@ -65,9 +83,7 @@ fn read_env() -> BotEnv {
|
||||
// ==================== ID ===========================
|
||||
let id: String = std::env::var(DISCORD_ID)
|
||||
.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("")
|
||||
});
|
||||
// ==================== Token ========================
|
||||
@@ -76,8 +92,8 @@ WARN: Missing {DISCORD_ID}
|
||||
// ==================== Intents ======================
|
||||
let intents_env: String = std::env::var(DISCORD_INTENTS)
|
||||
.unwrap_or_else(|msg| {
|
||||
println!("DEBUG: what is: {msg}");
|
||||
println!("ERROR: Missing {DISCORD_INTENTS}");
|
||||
debug!("What is: {msg}");
|
||||
error!("Missing {DISCORD_INTENTS}");
|
||||
"0".to_string()
|
||||
});
|
||||
// ==================== Parse Intents =================
|
||||
@@ -85,7 +101,7 @@ WARN: Missing {DISCORD_ID}
|
||||
let intents_u64 = intents_env.parse::<u64>().unwrap_or(0u64);
|
||||
let intents_truncated: GatewayIntents = GatewayIntents::from_bits_truncate(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 {
|
||||
intents: intents_truncated,
|
||||
|
||||
Reference in New Issue
Block a user