Skip to content

Commit 18a0233

Browse files
committed
Upload logs posted as attachments to paste.ee
This means a paste.ee API token is now required, the readme has been updated to reflect this.
1 parent d129163 commit 18a0233

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
RUST_LOG=error
33

44
DISCORD_TOKEN=
5+
PASTE_EE_TOKEN=
56
BACKGROUND_CAT_PREFIX=-

Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,27 @@ I made this bot because it was brought to my attention that other discords also
99

1010
- Build the code: `cargo build --bin discord-cat`
1111
- Copy `.env.example` to `target/debug/`
12-
- Customize `target/debug/.env` and include your Discord token in it, like this:
12+
- Customize `target/debug/.env` and include your Discord and paste.ee tokens in it, like this:
1313
```
1414
# See https://docs.rs/env_logger
1515
RUST_LOG=error
1616
1717
DISCORD_TOKEN=AAa0AAa0AAAaAaa...
18+
PASTE_EE_TOKEN=aaAAaAa0AaAA0...
1819
BACKGROUND_CAT_PREFIX=-
1920
```
2021
- Run the bot: `cargo run -p discord-cat`
2122

2223
## Running in production
2324

2425
- Copy `.env.example` to `.env`
25-
- Customize `.env` and include your Discord token in it, like this:
26+
- Customize `.env` and include your Discord and paste.ee tokens in it, like this:
2627
```
2728
# See https://docs.rs/env_logger
2829
RUST_LOG=error
2930
3031
DISCORD_TOKEN=AAa0AAa0AAAaAaa...
32+
PASTE_EE_TOKEN=aaAAaAa0AaAA0...
3133
BACKGROUND_CAT_PREFIX=-
3234
```
3335

apps/discord-cat/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ kankyo = "0.3.0"
1515
tokio = { version = "1.17.0", features = ["macros", "rt-multi-thread"] }
1616
futures = "0.3.21"
1717
serde = { version = "1.0.136", features = ["derive"] }
18+
json = "0.12.4"
1819

1920
[dependencies.serenity]
2021
version = "0.11.2"

apps/discord-cat/src/main.rs

+44
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ impl EventHandler for Handler {
142142
if content_type.is_some() && str::starts_with(&content_type.unwrap(), "text/plain") {
143143
let log = String::from_utf8_lossy(&content).into_owned();
144144

145+
upload_paste_ee(msg.channel_id, &log, &ctx).await;
146+
145147
let origins = common_origins(&log);
146148

147149
if origins.is_empty() {
@@ -184,6 +186,48 @@ impl EventHandler for Handler {
184186
}
185187
}
186188

189+
async fn upload_paste_ee(channel_id: ChannelId, log: &String, ctx: &Context) {
190+
let paste_ee_token = env::var("PASTE_EE_TOKEN").expect("Expected paste.ee API token in $PASTE_EE_TOKEN");
191+
let client = reqwest::Client::new();
192+
193+
let request_body = json::object! {
194+
description: "MultiMC Background Cat Log Upload",
195+
sections: [{ contents: log.as_str() }]
196+
}.dump();
197+
198+
let response = json::parse(
199+
client.post("https://api.paste.ee/v1/pastes")
200+
.header("Content-Type", "application/json")
201+
.header("X-Auth-Token", paste_ee_token)
202+
.body(request_body)
203+
.send()
204+
.await.unwrap()
205+
.text()
206+
.await.unwrap()
207+
.as_str()
208+
).unwrap();
209+
210+
if !&response["success"].as_bool().unwrap_or_default() {
211+
error!("paste.ee upload failed");
212+
} else {
213+
let link = &response["link"];
214+
215+
if let Err(why) = channel_id.send_message(&ctx, |m| {
216+
m.embed(|e| {
217+
e.title("Uploaded Log");
218+
e.colour(Colour::DARK_TEAL);
219+
e.field("Log uploaded to paste.ee:", link, true);
220+
debug!("Embed: {:?}", e);
221+
e
222+
});
223+
debug!("Embed: {:?}", m);
224+
m
225+
}).await {
226+
error!("Couldn't send message: {}", why);
227+
}
228+
info!("Uploaded attachment log to paste.ee: {}", link);
229+
}
230+
}
187231

188232
async fn send_help_reply(channel_id: ChannelId, mistakes: Vec<(&str, String)>, ctx: Context) {
189233
if let Err(why) = channel_id

0 commit comments

Comments
 (0)