Cursed bad apple change

+ Clippy + fmt
This commit is contained in:
Joey Hines 2022-12-14 22:43:39 -07:00
parent 357c1f069a
commit 10b471f25a
Signed by: joeyahines
GPG Key ID: 995E531F7A569DDB
3 changed files with 52 additions and 2 deletions

View File

@ -56,6 +56,7 @@ pub struct BotState {
pub accepted_nsfw: Option<UserId>, pub accepted_nsfw: Option<UserId>,
pub fortune_templates: Tera, pub fortune_templates: Tera,
pub albums: HashMap<String, Vec<Image>>, pub albums: HashMap<String, Vec<Image>>,
pub bad_apple_running: bool,
} }
impl BotState { impl BotState {
@ -80,6 +81,7 @@ impl BotState {
accepted_nsfw: None, accepted_nsfw: None,
fortune_templates, fortune_templates,
albums, albums,
bad_apple_running: false,
}) })
} }

View File

@ -1,5 +1,5 @@
use crate::error::Error; use crate::error::Error;
use crate::{command, group, GlobalData}; use crate::{command, group, GlobalData, BAD_APPLE};
use rand::prelude::IteratorRandom; use rand::prelude::IteratorRandom;
use rand::thread_rng; use rand::thread_rng;
use reqwest::Client; use reqwest::Client;
@ -7,10 +7,12 @@ use serde::{Deserialize, Serialize};
use serenity::client::Context; use serenity::client::Context;
use serenity::framework::standard::{Args, CommandResult}; use serenity::framework::standard::{Args, CommandResult};
use serenity::model::channel::Message; use serenity::model::channel::Message;
use serenity::utils::MessageBuilder;
use std::collections::HashMap; use std::collections::HashMap;
use std::time::Duration;
#[group] #[group]
#[commands(dad_joke, fortune, roll)] #[commands(dad_joke, fortune, roll, bad_apple)]
pub struct Joke; pub struct Joke;
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Serialize, Deserialize)]
@ -120,3 +122,45 @@ async fn roll(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
Ok(()) 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(())
}

View File

@ -10,6 +10,8 @@ use serenity::framework::standard::StandardFramework;
use serenity::prelude::*; use serenity::prelude::*;
use structopt::StructOpt; use structopt::StructOpt;
const BAD_APPLE: &str = include_str!("assets/bad_apple.txt");
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let args: Args = Args::from_args(); let args: Args = Args::from_args();
@ -38,6 +40,8 @@ async fn main() {
.group(&discord::joke::JOKE_GROUP) .group(&discord::joke::JOKE_GROUP)
.group(&discord::admin::ADMIN_GROUP) .group(&discord::admin::ADMIN_GROUP)
.unrecognised_command(unrecognised_command_hook) .unrecognised_command(unrecognised_command_hook)
.bucket("bad_apple", |b| b.delay(60*10))
.await
.help(&discord::MY_HELP) .help(&discord::MY_HELP)
.after(discord::after); .after(discord::after);