From 10b471f25a7260b86346f10f705de25ae5faab70 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Wed, 14 Dec 2022 22:43:39 -0700 Subject: [PATCH] Cursed bad apple change + Clippy + fmt --- src/config.rs | 2 ++ src/discord/joke.rs | 48 +++++++++++++++++++++++++++++++++++++++++++-- src/main.rs | 4 ++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index ff2a98e..3a37bcd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -56,6 +56,7 @@ pub struct BotState { pub accepted_nsfw: Option, pub fortune_templates: Tera, pub albums: HashMap>, + pub bad_apple_running: bool, } impl BotState { @@ -80,6 +81,7 @@ impl BotState { accepted_nsfw: None, fortune_templates, albums, + bad_apple_running: false, }) } diff --git a/src/discord/joke.rs b/src/discord/joke.rs index ea98053..da625f5 100644 --- a/src/discord/joke.rs +++ b/src/discord/joke.rs @@ -1,5 +1,5 @@ use crate::error::Error; -use crate::{command, group, GlobalData}; +use crate::{command, group, GlobalData, BAD_APPLE}; use rand::prelude::IteratorRandom; use rand::thread_rng; use reqwest::Client; @@ -7,10 +7,12 @@ use serde::{Deserialize, Serialize}; use serenity::client::Context; use serenity::framework::standard::{Args, CommandResult}; use serenity::model::channel::Message; +use serenity::utils::MessageBuilder; use std::collections::HashMap; +use std::time::Duration; #[group] -#[commands(dad_joke, fortune, roll)] +#[commands(dad_joke, fortune, roll, bad_apple)] pub struct Joke; #[derive(Clone, Serialize, Deserialize)] @@ -120,3 +122,45 @@ async fn roll(ctx: &Context, msg: &Message, args: Args) -> CommandResult { Ok(()) } + +#[command] +#[only_in(guilds)] +#[bucket = "bad_apple"] +async fn bad_apple(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { + let mut frames = BAD_APPLE.split('|'); + + let first_frame = frames.next().unwrap(); + + if first_frame.len() > 2000 { + msg.reply(&ctx.http, format!("Frame too big: {}", first_frame.len())) + .await?; + return Ok(()); + } + + let mut bad_apple_msg = msg + .reply( + &ctx.http, + MessageBuilder::default() + .push_codeblock_safe(first_frame, None) + .build(), + ) + .await?; + + for (idx, frame) in frames.enumerate() { + tokio::time::sleep(Duration::from_millis(100)).await; + + if (idx % 10) == 0 { + bad_apple_msg + .edit(&ctx.http, |m| { + m.content( + MessageBuilder::default() + .push_codeblock_safe(frame, None) + .build(), + ) + }) + .await?; + } + } + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 8f19037..0b6506d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,8 @@ use serenity::framework::standard::StandardFramework; use serenity::prelude::*; use structopt::StructOpt; +const BAD_APPLE: &str = include_str!("assets/bad_apple.txt"); + #[tokio::main] async fn main() { let args: Args = Args::from_args(); @@ -38,6 +40,8 @@ async fn main() { .group(&discord::joke::JOKE_GROUP) .group(&discord::admin::ADMIN_GROUP) .unrecognised_command(unrecognised_command_hook) + .bucket("bad_apple", |b| b.delay(60*10)) + .await .help(&discord::MY_HELP) .after(discord::after);