Joey Hines 5a65fdf54d
New memes
+ clippy + fmt
2022-11-13 20:14:46 -07:00

119 lines
3.4 KiB
Rust

pub mod album;
pub mod celeryman;
pub mod color;
use crate::error::Error;
use crate::{help, hook, imgur, GlobalData};
use rand::prelude::SliceRandom;
use serenity::async_trait;
use serenity::client::Context;
use serenity::framework::standard::{
help_commands, Args, CommandGroup, CommandResult, HelpOptions,
};
use serenity::model::channel::Message;
use serenity::model::id::UserId;
use serenity::model::prelude::Ready;
use serenity::prelude::EventHandler;
use std::collections::HashSet;
pub struct Handler;
#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, new_message: Message) {
if new_message.author.bot {
return;
}
if new_message.content.eq_ignore_ascii_case("yes")
|| new_message.content.eq_ignore_ascii_case("mhmm")
{
let mut data = ctx.data.write().await;
let global_data = data.get_mut::<GlobalData>().unwrap();
if let Some(u) = global_data.bot_state.accepted_nsfw {
if new_message.author.id == u {
new_message.reply(&ctx.http, "||https://cdn.discordapp.com/attachments/614891432079130625/1041545254362423368/unknown.png||").await.unwrap();
global_data.bot_state.accepted_nsfw = None;
}
}
}
}
async fn ready(&self, _: Context, ready: Ready) {
println!("Connected as {}", ready.user.name);
}
}
#[hook]
pub async fn after(
_ctx: &Context,
_msg: &Message,
command_name: &str,
command_result: CommandResult,
) {
match command_result {
Ok(()) => println!("Processed command '{}'", command_name),
Err(why) => println!("Command '{}' returned error {:?}", command_name, why),
}
}
pub async fn parse_album(ctx: &Context, msg: &Message, album_name: &str) -> Result<(), Error> {
let data = ctx.data.read().await;
let global_data = data.get::<GlobalData>().unwrap();
let album = global_data
.cfg
.albums
.iter()
.find(|album| album.name.to_lowercase() == album_name);
if let Some(album) = album {
match imgur::get_album_images(&global_data.cfg.imgur_client_id, &album.album_id).await {
Ok(album) => {
let image = {
let mut rng = rand::thread_rng();
album.choose(&mut rng)
};
if let Some(image) = image {
msg.reply(&ctx.http, &image.link).await?;
} else {
msg.reply(&ctx.http, "Album empty, try and add some images.")
.await?;
}
}
Err(_) => {
msg.reply(&ctx.http, "Unable to get album, try again later.")
.await?;
}
}
};
Ok(())
}
#[hook]
pub async fn unrecognised_command_hook(
ctx: &Context,
msg: &Message,
unrecognised_command_name: &str,
) {
if let Err(e) = parse_album(ctx, msg, unrecognised_command_name).await {
println!("Error processing album command: {}", e)
}
}
#[help]
pub async fn my_help(
context: &Context,
msg: &Message,
args: Args,
help_options: &'static HelpOptions,
groups: &[&'static CommandGroup],
owners: HashSet<UserId>,
) -> CommandResult {
let _ = help_commands::with_embeds(context, msg, args, help_options, groups, owners).await;
Ok(())
}