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
use crate::webhook::Webhook;
use anyhow::Result;
use futures_util::StreamExt;
use minify_js::TopLevelMode;
use regex::Regex;
use serenity::http::Http;
use serenity::prelude::*;
use std::fs::read_to_string;
use std::sync::{Arc, Mutex, OnceLock};
use strum::{AsRefStr, EnumString, EnumVariantNames, VariantNames};
use tokio::sync::broadcast;
use tokio::sync::OnceCell as TokLock;
pub struct Data {
    stdin: broadcast::Sender<String>,
}

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

pub struct Bot;
impl Bot {
    pub async fn new(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(), js(), maps(), start(), end(), help()],
                prefix_options: poise::PrefixFrameworkOptions {
                    prefix: Some(">".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 = "ADMINISTRATION"
)]
/// send a raw command to the server
async fn raw(
    ctx: Context<'_>,
    #[description = "Command"]
    #[rest]
    cmd: String,
) -> Result<()> {
    ctx.data().stdin.send(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 = "ADMINISTRATION")]
/// say something as the server
async fn say(ctx: Context<'_>, #[description = "Message"] message: String) -> Result<()> {
    let _ = ctx.defer();
    ctx.data().stdin.send(format!("say {message}"))?;
    return_next!(ctx)
}

#[derive(EnumString, EnumVariantNames, AsRefStr)]
#[strum(serialize_all = "snake_case")]
enum BanType {
    Id,
    Ip,
    Name,
}

async fn autocomplete_ban<'a>(
    _ctx: Context<'_>,
    partial: &'a str,
) -> impl futures::Stream<Item = String> + 'a {
    futures::stream::iter(BanType::VARIANTS)
        .filter(move |name| futures::future::ready(name.starts_with(partial)))
        .map(|name| name.to_string())
}

#[poise::command(slash_command, category = "ADMINISTRATION")]
/// ban a player
async fn ban(
    ctx: Context<'_>,
    #[autocomplete = "autocomplete_ban"] ban_type: BanType,
    #[description = "Player (id/ip/name)"] player: String,
) -> Result<()> {
    let _ = ctx.defer();
    ctx.data()
        .stdin
        .send(format!("ban {} {player}", ban_type.as_ref()))?;
    return_next!(ctx)
}

#[poise::command(
    prefix_command,
    required_permissions = "USE_SLASH_COMMANDS",
    category = "ADMINISTRATION",
    track_edits
)]
/// run arbitrary javascript
async fn js(
    ctx: Context<'_>,
    #[description = "Script"]
    #[rest]
    script: String,
) -> Result<()> {
    static RE: OnceLock<Regex> = OnceLock::new();
    let _ = ctx.channel_id().start_typing(&ctx.serenity_context().http);
    let re = RE.get_or_init(|| Regex::new(r#"```(js|javascript)?([^`]+)```"#).unwrap());
    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 let Err(_) = minify_js::minify(TopLevelMode::Global, script.into(), &mut out) {
        std::borrow::Cow::from(script.replace('\n', ";")) // xd
    } else {
        String::from_utf8_lossy(&out)
    };
    ctx.data().stdin.send(format!("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
}

static MAPS: TokLock<Vec<String>> = TokLock::const_new();
async fn get_maps(stdin: &broadcast::Sender<String>) -> &Vec<String> {
    MAPS.get_or_init(|| async move {
        stdin.send(format!("listmaps")).unwrap();
        let res = get_nextblock().await;
        let mut vec = vec![];
        for line in res.lines() {
            if let Some((_, name)) = line.split_once(':') {
                vec.push(strip_colors(name));
            }
        }
        vec
    })
    .await
}

#[poise::command(slash_command, required_permissions = "USE_SLASH_COMMANDS")]
/// lists the maps.
pub async fn maps(ctx: Context<'_>) -> Result<()> {
    let _ = ctx.defer();
    let maps = get_maps(&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.")
        })
    })
    .await?;
    Ok(())
}

async fn autocomplete_map<'a>(
    ctx: Context<'a>,
    partial: &'a str,
) -> impl futures::Stream<Item = String> + 'a {
    futures::stream::iter(get_maps(&ctx.data().stdin).await)
        .filter(move |name| futures::future::ready(name.starts_with(partial)))
        .map(|name| name.to_string())
}

async fn mapi(map: &str, stdin: &broadcast::Sender<String>) -> usize {
    get_maps(stdin).await.iter().position(|r| r == map).unwrap()
}

#[poise::command(slash_command, required_permissions = "USE_SLASH_COMMANDS")]
/// start the game.
pub async fn start(
    ctx: Context<'_>,
    #[description = "the map"]
    #[autocomplete = "autocomplete_map"]
    map: String,
) -> Result<()> {
    let _ = ctx.defer();
    ctx.data()
        .stdin
        .send(format!("plague {}", mapi(&map, &ctx.data().stdin).await))
        .unwrap();
    return_next!(ctx)
}

#[poise::command(slash_command, required_permissions = "USE_SLASH_COMMANDS")]
/// end the game.
pub async fn end(
    ctx: Context<'_>,
    #[description = "the map to go to"]
    #[autocomplete = "autocomplete_map"]
    map: String,
) -> Result<()> {
    let _ = ctx.defer();
    ctx.data()
        .stdin
        .send(format!("endplague {}", mapi(&map, &ctx.data().stdin).await))
        .unwrap();
    return_next!(ctx)
}

#[poise::command(
    prefix_command,
    slash_command,
    required_permissions = "USE_SLASH_COMMANDS",
    track_edits
)]
/// 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(())
}