generated from Oliver/discord-bot-template
Compare commits
22 Commits
2fbdeeef06
...
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 |
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
|
/target
|
||||||
/.vscode
|
/.vscode
|
||||||
.env
|
.env
|
||||||
|
server.json
|
||||||
|
|||||||
4
Cargo.lock
generated
4
Cargo.lock
generated
@@ -354,6 +354,8 @@ dependencies = [
|
|||||||
"dotenv",
|
"dotenv",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"poise",
|
"poise",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
@@ -933,6 +935,7 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "poise"
|
name = "poise"
|
||||||
version = "0.6.1"
|
version = "0.6.1"
|
||||||
|
source = "git+https://github.com/Rushmore75/poise.git?rev=6afaf44a791cd3ff590c89fda26c94b4411b3266#6afaf44a791cd3ff590c89fda26c94b4411b3266"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"derivative",
|
"derivative",
|
||||||
@@ -950,6 +953,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "poise_macros"
|
name = "poise_macros"
|
||||||
version = "0.6.1"
|
version = "0.6.1"
|
||||||
|
source = "git+https://github.com/Rushmore75/poise.git?rev=6afaf44a791cd3ff590c89fda26c94b4411b3266#6afaf44a791cd3ff590c89fda26c94b4411b3266"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"darling",
|
"darling",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
|
|||||||
@@ -10,10 +10,11 @@ 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"], path="../poise" }
|
|
||||||
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 = "0.1.40"
|
||||||
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
|
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"]
|
|
||||||
@@ -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
|
|
||||||
@@ -1,20 +1,27 @@
|
|||||||
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>,
|
||||||
|
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||||
orphanage: Vec<GuildChannel>,
|
orphanage: Vec<GuildChannel>,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
needs_clean: bool,
|
needs_clean: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
struct Channel {
|
struct Channel {
|
||||||
this: GuildChannel,
|
this: GuildChannel,
|
||||||
|
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||||
children: Vec<Channel>,
|
children: Vec<Channel>,
|
||||||
|
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||||
messages: Vec<Message>,
|
messages: Vec<Message>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,6 +143,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 +206,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, 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.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 +228,27 @@ 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();
|
||||||
|
|
||||||
|
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(
|
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);
|
||||||
},
|
},
|
||||||
@@ -239,8 +275,14 @@ 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 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 {
|
match guild.channels(ctx.http()).await {
|
||||||
Ok(ok) => {
|
Ok(ok) => {
|
||||||
let server = index(ok).await;
|
let server = index(ok).await;
|
||||||
|
|||||||
14
src/main.rs
14
src/main.rs
@@ -1,5 +1,7 @@
|
|||||||
|
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::{debug, error, info, warn, Level};
|
||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
mod command;
|
mod command;
|
||||||
@@ -30,14 +32,13 @@ async fn main() {
|
|||||||
|
|
||||||
// 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
|
||||||
info!(r#"
|
info!("Invite with: https://discord.com/api/oauth2/authorize?client_id={}&permissions={}&scope=bot",
|
||||||
Invite this bot with:
|
|
||||||
"#);
|
|
||||||
info!("https://discord.com/api/oauth2/authorize?client_id={}&permissions={}&scope=bot",
|
|
||||||
ENV.id,
|
ENV.id,
|
||||||
ENV.intents.bits(),
|
ENV.intents.bits(),
|
||||||
);
|
);
|
||||||
info!("\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()
|
||||||
@@ -46,6 +47,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| {
|
||||||
|
|||||||
Reference in New Issue
Block a user