mostlybot 0.3.0

mostlybot is the twitch bot that I use for my stream on twitch
Documentation
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
410
411
412
413
414
415
416
417
418
419
420
//! Responds with random UwU kaomoji
//!
//! Broadcaster, moderators, & VIPs automatically have privilege to add new kaomoji
//!
//! I have no clue if the thing this is running on has persistent storage,
//! so there is a somewhat big list of default kaomoji.
//!
//! usage: ```!uwu ?[index]``` ```!uwu [kaomoji]``` ```!uwu remove ?[kaomoji / index]```
//!
//! author: vulae

use std::path::PathBuf;

use rand::seq::SliceRandom;
use serde::{Deserialize, Serialize};
use tracing::instrument;

use super::ChatCommand;

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Kaomoji {
    string: String,
    user_id: String,
}

impl Kaomoji {
    pub fn new<S: Into<String>>(string: S, user_id: S) -> Self {
        Self {
            string: string.into(),
            user_id: user_id.into(),
        }
    }

    pub fn new_dummy<S: Into<String>>(string: S) -> Self {
        Self {
            string: string.into(),
            user_id: "938429017".to_string(), // mostlymaxi user_id
        }
    }
}

impl Default for Kaomoji {
    fn default() -> Self {
        Self::new_dummy("UwU".to_owned())
    }
}

#[derive(Debug, Serialize, Deserialize)]
struct KaomojiList(Vec<Kaomoji>);

impl KaomojiList {
    fn count(&self) -> usize {
        self.0.len()
    }

    fn random(&self) -> Kaomoji {
        self.0
            .choose(&mut rand::thread_rng())
            .map_or(Kaomoji::default(), |v| v.clone())
    }

    fn get_index(&self, index: isize) -> Option<Kaomoji> {
        let index = if index >= 0 {
            index as usize
        } else {
            (self.0.len() as isize - isize::abs(index)) as usize
        };
        self.0.get(index).cloned()
    }

    fn contains(&self, kaomoji: &str) -> bool {
        self.0.iter().any(|k| k.string == kaomoji)
    }

    fn add(&mut self, kaomoji: Kaomoji) {
        if !self.contains(&kaomoji.string) {
            self.0.push(kaomoji);
        }
    }

    fn remove_kaomoji(&mut self, kaomoji: &str) -> Option<Kaomoji> {
        if let Some(index) = self.0.iter().position(|k| k.string == kaomoji) {
            self.remove_index(index as isize)
        } else {
            None
        }
    }

    /// Remove by index with negative indexing
    fn remove_index(&mut self, index: isize) -> Option<Kaomoji> {
        let index = if index >= 0 {
            index as usize
        } else {
            (self.0.len() as isize - isize::abs(index)) as usize
        };
        if index < self.0.len() {
            Some(self.0.remove(index))
        } else {
            None
        }
    }

    fn remove_last(&mut self) -> Option<Kaomoji> {
        self.0.pop()
    }
}

impl Default for KaomojiList {
    fn default() -> Self {
        Self(vec![
            Kaomoji::new_dummy("UwU"),
            Kaomoji::new_dummy("OwO"),
            Kaomoji::new_dummy("0-0"),
            Kaomoji::new_dummy("-_-"),
            Kaomoji::new_dummy(":3"),
            Kaomoji::new_dummy("(˶˃ ᵕ ˂˶) .ᐟ.ᐟ"),
            Kaomoji::new_dummy("⸜(。˃ ᵕ ˂ )⸝♡"),
            Kaomoji::new_dummy("(^_^)"),
            Kaomoji::new_dummy("(^>-<^)"),
            Kaomoji::new_dummy("∩(·ω·)∩"),
            Kaomoji::new_dummy("^ω^"),
            Kaomoji::new_dummy("^_^"),
            Kaomoji::new_dummy("=^_^="),
            Kaomoji::new_dummy("(^v^)"),
            Kaomoji::new_dummy("(*´▽`*)"),
            Kaomoji::new_dummy("(´・ω・`)"),
            Kaomoji::new_dummy("(´;ω;`)"),
            Kaomoji::new_dummy("(˶◜ᵕ◝˶)"),
            Kaomoji::new_dummy("∘ ∘ ∘ ( °ヮ° ) ?"),
            Kaomoji::new_dummy("( ˶°ㅁ°) !!"),
            Kaomoji::new_dummy("(๑-﹏-๑)"),
            Kaomoji::new_dummy(" ˶ᵔ ᵕ ᵔ˶ "),
        ])
    }
}

/// This just parses the command arguments to make my life easier
#[derive(Debug, PartialEq)]
enum MostlyUwUArgs {
    DisplayRandom,
    DisplayIndex { index: isize },
    AddKaomoji { kaomoji: String },
    RemoveKaomoji { kaomoji: String },
    RemoveIndex { index: isize },
    RemoveLast,
}

impl TryFrom<&twitcheventsub::MessageData> for MostlyUwUArgs {
    type Error = anyhow::Error;

    fn try_from(ctx: &twitcheventsub::MessageData) -> Result<Self, Self::Error> {
        let content = ctx
            .message
            .text
            .split(' ')
            .skip(1)
            .collect::<Vec<_>>()
            .join(" ")
            .trim()
            .to_owned();

        if content.is_empty() {
            Ok(MostlyUwUArgs::DisplayRandom)
        } else {
            let first = content.split(' ').nth(0).unwrap();
            if first.to_lowercase() == "remove" {
                let rest = content.split(' ').skip(1).collect::<Vec<_>>().join(" ");
                if rest.is_empty() {
                    Ok(MostlyUwUArgs::RemoveLast)
                } else if let Ok(index) = rest.parse::<isize>() {
                    Ok(MostlyUwUArgs::RemoveIndex { index })
                } else {
                    Ok(MostlyUwUArgs::RemoveKaomoji {
                        kaomoji: rest.trim().to_owned(),
                    })
                }
            } else if let Ok(index) = first.parse::<isize>() {
                Ok(MostlyUwUArgs::DisplayIndex { index })
            } else {
                Ok(MostlyUwUArgs::AddKaomoji {
                    kaomoji: content.trim().to_owned(),
                })
            }
        }
    }
}

pub struct MostlyUwU {
    kaomoji_file_path: PathBuf,
    /// If to allow any user to add a kaomoji
    kaomoji_add_permissions_user: bool,
}

impl MostlyUwU {
    fn load_kaomoji_list(&self) -> anyhow::Result<KaomojiList> {
        if !std::fs::exists(&self.kaomoji_file_path)? {
            return Ok(KaomojiList::default());
        }
        Ok(serde_json::from_str(&std::fs::read_to_string(
            &self.kaomoji_file_path,
        )?)?)
    }

    fn save_kaomoji_list(&self, list: &KaomojiList) -> anyhow::Result<()> {
        std::fs::write(&self.kaomoji_file_path, serde_json::to_string(list)?)?;
        Ok(())
    }

    #[allow(unused)]
    fn delete_kaomoji_list(&self) -> anyhow::Result<()> {
        if std::fs::exists(&self.kaomoji_file_path)? {
            std::fs::remove_file(&self.kaomoji_file_path)?;
        }
        Ok(())
    }
}

impl ChatCommand for MostlyUwU {
    fn new() -> Self {
        Self {
            kaomoji_file_path: "kaomoji.json".into(),
            kaomoji_add_permissions_user: false,
        }
    }

    fn names() -> Vec<String> {
        vec![
            "uwu".to_string(),
            "UwU".to_string(),
            "owo".to_string(),
            "OwO".to_string(),
            "kaomoji".to_string(),
        ]
    }

    fn help(&self) -> String {
        "Responds UwU kaomoji\nusage: \"!uwu ?[index]\" - Random kaomoji\n\"!uwu [kaomoji]\" - Add kaomoji\n\"!uwu remove ?[kaomoji / index]\" - Remove kaomoji".to_string()
    }

    #[instrument(skip(self, api))]
    fn handle(
        &mut self,
        api: &mut super::TwitchApiWrapper,
        ctx: &twitcheventsub::MessageData,
    ) -> anyhow::Result<()> {
        // Collect Args -> Load kaomoji -> Validate permissions -> Execute command

        // Parse command arguments
        let Ok(args) = MostlyUwUArgs::try_from(ctx) else {
            let _ = api.send_chat_message_with_reply("Invalid arguments.", Some(&ctx.message_id));
            return Ok(());
        };

        let mut invalid_permissions = || -> anyhow::Result<()> {
            let _ = api
                .send_chat_message_with_reply("You can't do that. (•̀⤙•́ )", Some(&ctx.message_id));
            Ok(())
        };

        let mut kaomoji_list = self.load_kaomoji_list()?;

        match args {
            MostlyUwUArgs::DisplayRandom => {
                let kaomoji = kaomoji_list.random();
                let _ = api.send_chat_message_with_reply(&kaomoji.string, Some(&ctx.message_id));
            }
            MostlyUwUArgs::DisplayIndex { index } => {
                if let Some(kaomoji) = kaomoji_list.get_index(index) {
                    let _ =
                        api.send_chat_message_with_reply(&kaomoji.string, Some(&ctx.message_id));
                } else {
                    let _ = api.send_chat_message_with_reply(
                        &format!(
                            "Index out of bounds ({}), oh no (╥﹏╥)",
                            kaomoji_list.count()
                        ),
                        Some(&ctx.message_id),
                    );
                }
            }
            MostlyUwUArgs::AddKaomoji { kaomoji } => {
                if !(self.kaomoji_add_permissions_user
                    || ctx.moderator
                    || (ctx.chatter.id == ctx.broadcaster.id && !ctx.chatter.id.is_empty())
                    || ctx.badges.iter().any(|badge| badge.set_id == "vip"))
                {
                    return invalid_permissions();
                }

                let kaomoji = Kaomoji::new(&kaomoji, &ctx.chatter.id);
                if !kaomoji_list.contains(&kaomoji.string) {
                    let _ = api.send_chat_message_with_reply(
                        &format!("{} was added ( ˶ˆᗜˆ˵ )", &kaomoji.string),
                        Some(&ctx.message_id),
                    );
                    kaomoji_list.add(kaomoji);
                    self.save_kaomoji_list(&kaomoji_list)?;
                } else {
                    let _ = api.send_chat_message_with_reply(
                        &format!("{} Already exists (◔_◔)", &kaomoji.string),
                        Some(&ctx.message_id),
                    );
                }
            }
            MostlyUwUArgs::RemoveKaomoji { kaomoji } => {
                if !(ctx.moderator
                    || (ctx.chatter.id == ctx.broadcaster.id && !ctx.chatter.id.is_empty()))
                {
                    return invalid_permissions();
                }

                if let Some(kaomoji) = kaomoji_list.remove_kaomoji(&kaomoji) {
                    let _ = api.send_chat_message_with_reply(
                        &format!("{} was removed ꃋᴖꃋ", &kaomoji.string),
                        Some(&ctx.message_id),
                    );
                    self.save_kaomoji_list(&kaomoji_list)?;
                } else {
                    let _ = api.send_chat_message_with_reply(
                        "Couldn't find kaomoji to remove (ㅠ‸ㅠ)",
                        Some(&ctx.message_id),
                    );
                }
            }
            MostlyUwUArgs::RemoveIndex { index } => {
                if !(ctx.moderator
                    || (ctx.chatter.id == ctx.broadcaster.id && !ctx.chatter.id.is_empty()))
                {
                    return invalid_permissions();
                }

                if let Some(kaomoji) = kaomoji_list.remove_index(index) {
                    let _ = api.send_chat_message_with_reply(
                        &format!("{} was removed (ㅠ﹏ㅠ)", &kaomoji.string),
                        Some(&ctx.message_id),
                    );
                    self.save_kaomoji_list(&kaomoji_list)?;
                } else {
                    let _ = api.send_chat_message_with_reply(
                        &format!(
                            "Index out of bounds ({}), oh no (╥﹏╥)",
                            kaomoji_list.count()
                        ),
                        Some(&ctx.message_id),
                    );
                }
            }
            MostlyUwUArgs::RemoveLast => {
                if !(ctx.moderator
                    || (ctx.chatter.id == ctx.broadcaster.id && !ctx.chatter.id.is_empty()))
                {
                    return invalid_permissions();
                }

                if let Some(kaomoji) = kaomoji_list.remove_last() {
                    let _ = api.send_chat_message_with_reply(
                        &format!("{} was removed .‸.", &kaomoji.string),
                        Some(&ctx.message_id),
                    );
                    self.save_kaomoji_list(&kaomoji_list)?;
                } else {
                    let _ = api.send_chat_message_with_reply(
                        "There's no kaomoji, what did you do? (ó﹏ò。)",
                        Some(&ctx.message_id),
                    );
                }
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod test {
    use twitcheventsub::MessageData;

    use super::*;
    use crate::api::{MockTwitchEventSubApi, TwitchApiWrapper};

    fn create_test_msg(content: &str) -> MessageData {
        serde_json::from_str(&format!(
            "{{\"broadcaster_user_id\":\"938429017\",\"broadcaster_user_name\":\"mostlymaxi\",\"broadcaster_user_login\":\"mostlymaxi\",\"chatter_user_id\":\"938429017\",\"chatter_user_name\":\"mostlymaxi\",\"chatter_user_login\":\"mostlymaxi\",\"message_id\":\"3104f083-2bdb-4d6a-bb5d-30b407876ea4\",\"message\":{{\"text\":\"{}\",\"fragments\":[{{\"type\":\"text\",\"text\":\"{}\",\"cheermote\":null,\"emote\":null,\"mention\":null}}]}},\"color\":\"#FF0000\",\"badges\":[{{\"set_id\":\"broadcaster\",\"id\":\"1\",\"info\":\"\"}},{{\"set_id\":\"subscriber\",\"id\":\"0\",\"info\":\"3\"}}],\"message_type\":\"text\",\"cheer\":null,\"reply\":null,\"channel_points_custom_reward_id\":null,\"channel_points_animation_id\":null}}",
            content, content
        )).unwrap()
    }

    fn create_test_user_msg(content: &str) -> MessageData {
        serde_json::from_str(&format!(
            "{{\"broadcaster_user_id\":\"938429017\",\"broadcaster_user_name\":\"mostlymaxi\",\"broadcaster_user_login\":\"mostlymaxi\",\"chatter_user_id\":\"\",\"chatter_user_name\":\"\",\"chatter_user_login\":\"\",\"message_id\":\"\",\"message\":{{\"text\":\"{}\",\"fragments\":[{{\"type\":\"text\",\"text\":\"{}\",\"cheermote\":null,\"emote\":null,\"mention\":null}}]}},\"color\":\"#000000\",\"badges\":[],\"message_type\":\"text\",\"cheer\":null,\"reply\":null,\"channel_points_custom_reward_id\":null,\"channel_points_animation_id\":null}}",
            content, content
        )).unwrap()
    }

    #[test]
    fn handle() -> anyhow::Result<()> {
        let mut api = TwitchApiWrapper::Test(MockTwitchEventSubApi::init_twitch_api());
        let mut cmd = MostlyUwU::new();
        cmd.kaomoji_file_path = "kaomoji_test.json".into();

        // Use `cargo test commands::uwu::test::handle -- --nocapture` to validate response output

        cmd.handle(&mut api, &create_test_msg("!uwu"))?; // {}
        cmd.handle(&mut api, &create_test_msg("!uwu remove UwU"))?; // UwU was removed
        cmd.handle(&mut api, &create_test_msg("!uwu remove UwU"))?; // Could not find
        cmd.handle(&mut api, &create_test_msg("!uwu UwU"))?; // UwU was added
        cmd.handle(&mut api, &create_test_msg("!uwu UwU"))?; // UwU already exists

        cmd.handle(&mut api, &create_test_msg("!uwu 0"))?; // OwO
        cmd.handle(&mut api, &create_test_msg("!uwu -1"))?; // UwU

        cmd.handle(&mut api, &create_test_user_msg("!uwu"))?; // {}
        cmd.handle(&mut api, &create_test_user_msg("!uwu remove UwU"))?; // You can't do that.
        cmd.handle(&mut api, &create_test_user_msg("!uwu UwU"))?; // You can't do that.

        cmd.delete_kaomoji_list()?;

        Ok(())
    }
}