html terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
mod maps;
mod player;

use crate::webhook::Webhook;
use anyhow::{anyhow, Result};
use itertools::Itertools;
use maps::Maps;
use minify_js::TopLevelMode;
use player::Players;
use regex::Regex;
use serenity::http::{CacheHttp, Http};
use serenity::prelude::*;
use std::fs::read_to_string;
use std::str::FromStr;
use std::sync::{Arc, LazyLock, Mutex, OnceLock};
use tokio::sync::broadcast;

pub struct Data {
    stdin: broadcast::Sender<String>,
}

static SKIPPING: OnceLock<(Arc<Mutex<u8>>, broadcast::Sender<String>)> = OnceLock::new();

#[macro_export]
macro_rules! send {
    ($e:expr, $fmt:literal $(, $args:expr)* $(,)?) => {
        $e.send(format!($fmt $(, $args)*))
    };
}

macro_rules! send_ctx {
    ($e:expr,$fmt:literal $(, $args:expr)* $(,)?) => {
        $e.data().stdin.send(format!($fmt $(, $args)*))
    };
}

#[cfg(not(debug_assertions))]
const PFX: &'static str = ">";
#[cfg(debug_assertions)]
const PFX: &'static str = "-";

const SUCCESS: (u8, u8, u8) = (34, 139, 34);
const FAIL: (u8, u8, u8) = (255, 69, 0);

pub struct Bot;
impl Bot {
    pub async fn spawn(stdout: broadcast::Receiver<String>, stdin: broadcast::Sender<String>) {
        let tok = std::env::var("TOKEN").unwrap_or(read_to_string("token").expect("wher token"));
        let f: poise::FrameworkBuilder<Data, anyhow::Error> = poise::Framework::builder()
            .options(poise::FrameworkOptions {
                commands: vec![
                    raw(),
                    say(),
                    ban(),
                    unban(),
                    add_admin(),
                    remove_admin(),
                    js(),
                    maps(),
                    players(),
                    status(),
                    start(),
                    end(),
                    help(),
                ],
                prefix_options: poise::PrefixFrameworkOptions {
                    edit_tracker: Some(poise::EditTracker::for_timespan(
                        std::time::Duration::from_secs(2 * 60),
                    )),
                    prefix: Some(PFX.to_string()),
                    ..Default::default()
                },
                ..Default::default()
            })
            .token(tok)
            .intents(GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT)
            .setup(|ctx, _ready, framework| {
                Box::pin(async move {
                    poise::builtins::register_globally(ctx, &framework.options().commands).await?;
                    println!("registered");
                    Ok(Data { stdin })
                })
            });

        tokio::spawn(async move {
            let http = Http::new("");
            let mut wh = Webhook::new(&http, &std::env::var("WEBHOOK").expect("no webhook!")).await;
            SKIPPING.get_or_init(|| (wh.skip.clone(), wh.skipped.clone()));
            wh.link(stdout).await;
        });
        tokio::spawn(async move {
            f.run().await.unwrap();
        });
    }
}

type Context<'a> = poise::Context<'a, Data, anyhow::Error>;

#[poise::command(
    prefix_command,
    required_permissions = "USE_SLASH_COMMANDS",
    category = "Control"
)]
/// send a raw command to the server
async fn raw(
    ctx: Context<'_>,
    #[description = "Command"]
    #[rest]
    cmd: String,
) -> Result<()> {
    send_ctx!(ctx, "{cmd}")?;
    println!("sent");
    Ok(())
}

macro_rules! return_next {
    ($ctx:expr) => {{
        let line = get_nextblock().await;
        $ctx.send(|m| m.content(line)).await?;
        Ok(())
    }};
}

async fn get_nextblock() -> String {
    let (skip_count, skip_send) = SKIPPING.get().unwrap();
    {
        *skip_count.lock().unwrap() += 1;
    }
    skip_send
        .subscribe()
        .recv()
        .await
        .unwrap_or("._?".to_string())
}

#[poise::command(slash_command, category = "Control")]
/// say something as the server
async fn say(ctx: Context<'_>, #[description = "Message"] message: String) -> Result<()> {
    let _ = ctx.defer().await;
    ctx.data().stdin.send(format!("say {message}"))?;
    return_next!(ctx)
}

#[poise::command(slash_command, category = "Control")]
/// ban a player by uuid and ip
async fn ban(
    ctx: Context<'_>,
    #[description = "player to ban"]
    #[autocomplete = "player::autocomplete"]
    player: String,
) -> Result<()> {
    let _ = ctx.defer().await;
    let player = Players::find(&ctx.data().stdin, player)
        .await
        .unwrap()
        .unwrap();
    send_ctx!(ctx, "ban ip {}", player.ip)?;
    send_ctx!(ctx, "ban id {}", player.uuid)?;
    return_next!(ctx)
}

#[poise::command(slash_command, category = "Control")]
/// unban a player by uuid or ip
async fn unban(
    ctx: Context<'_>,
    #[description = "Player id/ip"]
    #[rename = "ip_or_id"]
    player: String,
) -> Result<()> {
    let _ = ctx.defer().await;
    send_ctx!(ctx, "unban {}", player)?;
    return_next!(ctx)
}

#[poise::command(
    prefix_command,
    required_permissions = "USE_SLASH_COMMANDS",
    category = "Control",
    track_edits
)]
/// run arbitrary javascript
async fn js(
    ctx: Context<'_>,
    #[description = "Script"]
    #[rest]
    script: String,
) -> Result<()> {
    static RE: LazyLock<Regex> =
        LazyLock::new(|| Regex::new(r#"```(js|javascript)?([^`]+)```"#).unwrap());
    let _ = ctx.channel_id().start_typing(&ctx.serenity_context().http);
    let mat = RE
        .captures(&script)
        .ok_or(anyhow::anyhow!(r#"no code found (use \`\`\`js...\`\`\`."#))?;
    let script = mat.get(2).unwrap().as_str();
    let mut out = vec![];
    let script = if minify_js::minify(TopLevelMode::Global, script.into(), &mut out).is_err() {
        std::borrow::Cow::from(script.replace('\n', ";")) // xd
    } else {
        String::from_utf8_lossy(&out)
    };
    send_ctx!(ctx, "js {script}")?;
    let line = get_nextblock().await;
    ctx.send(|m| m.content(line)).await?;
    Ok(())
}

fn strip_colors(from: &str) -> String {
    let mut result = String::new();
    result.reserve(from.len());
    let mut level: u8 = 0;
    for c in from.chars() {
        if c == '[' {
            level += 1;
        } else if c == ']' {
            level -= 1;
        } else if level == 0 {
            result.push(c);
        }
    }
    result
}

#[poise::command(
    slash_command,
    required_permissions = "USE_SLASH_COMMANDS",
    category = "Info"
)]
/// lists the maps.
pub async fn maps(ctx: Context<'_>) -> Result<()> {
    let _ = ctx.defer().await;
    let maps = Maps::get_all(&ctx.data().stdin).await;
    poise::send_reply(ctx, |m| {
        m.embed(|e| {
            for (k, v) in maps.iter().enumerate() {
                e.field((k + 1).to_string(), v, true);
            }
            e.description("map list.").color(SUCCESS)
        })
    })
    .await?;
    Ok(())
}

#[poise::command(prefix_command, slash_command, category = "Info")]
/// server status.
pub async fn status(ctx: Context<'_>) -> Result<()> {
    let _ = ctx.defer_or_broadcast().await;
    send_ctx!(ctx, "status")?;
    let block = tokio::select! {
        block = get_nextblock() => block,
        _ = async_std::task::sleep(std::time::Duration::from_secs(5)) =>
            { poise::send_reply(ctx, |m| m.embed(|e| e.title("server down").color(FAIL))).await?; return Ok(()) },
    };
    let (tps, mem, pcount) = block
        .split('/')
        .map(|s| u32::from_str(s.trim().split_once(' ').unwrap().0).unwrap())
        .collect_tuple()
        .ok_or(anyhow!("couldnt split block"))?;
    poise::send_reply(ctx, |m| {
        m.embed(|e| {
            if pcount > 0 {
                e.footer(|f| f.text("see /players for player list"));
            }
            e.title("server online")
                .field("tps", tps, true)
                .field("memory use", mem, true)
                .field("players", pcount, true)
                .color(SUCCESS)
        })
    })
    .await?;
    Ok(())
}

#[poise::command(slash_command, prefix_command, category = "Info")]
/// lists the currently online players.
pub async fn players(ctx: Context<'_>) -> Result<()> {
    let _ = ctx.defer().await;
    let players = Players::get_all(&ctx.data().stdin).await.unwrap().clone();
    let perms = ctx
        .partial_guild()
        .await
        .unwrap()
        .member_permissions(ctx.http(), ctx.author().id)
        .await?;
    // let perms = ctx
    //     .author_member()
    //     .await
    //     .ok_or(anyhow!("couldnt get perms"))?
    //     .permissions(ctx.cache().unwrap())?;
    poise::send_reply(ctx, |m| {
        m.embed(|e| {
            if players.is_empty() {
                return e.title("no players online.").color(FAIL);
            }
            e.fields(players.into_iter().map(|p| {
                let admins = if p.admin { " [A]" } else { "" };
                (
                    p.name,
                    if perms.use_slash_commands() {
                        format!("{id}, {ip}", id = p.uuid, ip = p.ip) + admins
                    } else {
                        admins.to_string()
                    },
                    true,
                )
            }));
            e.description("currently online players.").color(SUCCESS)
        })
    })
    .await?;
    Ok(())
}

#[poise::command(
    slash_command,
    required_permissions = "USE_SLASH_COMMANDS",
    category = "Control"
)]
/// start the game.
pub async fn start(
    ctx: Context<'_>,
    #[description = "the map"]
    #[autocomplete = "maps::autocomplete"]
    map: String,
) -> Result<()> {
    let _ = ctx.defer().await;
    send_ctx!(ctx, "host {}", Maps::find(&map, &ctx.data().stdin).await)?;
    return_next!(ctx)
}

#[poise::command(
    slash_command,
    category = "Control",
    required_permissions = "USE_SLASH_COMMANDS"
)]
/// end the game.
pub async fn end(
    ctx: Context<'_>,
    #[description = "the map to go to"]
    #[autocomplete = "maps::autocomplete"]
    map: String,
) -> Result<()> {
    let _ = ctx.defer().await;
    send_ctx!(
        ctx,
        "gameover {}",
        Maps::find(&map, &ctx.data().stdin).await
    )?;
    return_next!(ctx)
}

#[poise::command(slash_command, category = "Configuration")]
/// make somebody a admin
pub async fn add_admin(
    ctx: Context<'_>,
    #[description = "The player to make admin"]
    #[autocomplete = "player::autocomplete"]
    player: String,
) -> Result<()> {
    let player = Players::find(&ctx.data().stdin, player)
        .await
        .unwrap()
        .unwrap();
    send_ctx!(ctx, "admin add {}", player.uuid)?;
    Ok(())
}

#[poise::command(slash_command, category = "Configuration")]
/// remove the admin status
pub async fn remove_admin(
    ctx: Context<'_>,
    #[description = "The player to remove admin status from"]
    #[autocomplete = "player::autocomplete"]
    player: String,
) -> Result<()> {
    let player = Players::find(&ctx.data().stdin, player)
        .await
        .unwrap()
        .unwrap();
    send_ctx!(ctx, "admin remove {}", player.uuid)?;
    Ok(())
}

#[poise::command(
    prefix_command,
    slash_command,
    required_permissions = "USE_SLASH_COMMANDS",
    track_edits,
    category = "Info"
)]
/// show help and stuff
pub async fn help(
    ctx: Context<'_>,
    #[description = "Specific command to show help about"]
    #[autocomplete = "poise::builtins::autocomplete_command"]
    command: Option<String>,
) -> Result<()> {
    poise::builtins::help(
        ctx,
        command.as_deref(),
        poise::builtins::HelpConfiguration {
            extra_text_at_bottom: "Mindustry server management bot",
            ..Default::default()
        },
    )
    .await?;
    Ok(())
}