From b2082c89a478e725dcd2a7c47dd8b9a8e2868609 Mon Sep 17 00:00:00 2001 From: shibedrill <53824200+shibedrill@users.noreply.github.com> Date: Mon, 27 May 2024 01:04:58 -0400 Subject: [PATCH] minor modifications, add event handler --- src/command/fun.rs | 21 +++++++++++++++---- src/command/util.rs | 51 +++++++++++++++++++++++++++++---------------- src/main.rs | 32 ++++++++++++++++++++++++++-- 3 files changed, 80 insertions(+), 24 deletions(-) diff --git a/src/command/fun.rs b/src/command/fun.rs index 6fae33b..5a41309 100644 --- a/src/command/fun.rs +++ b/src/command/fun.rs @@ -56,7 +56,12 @@ pub async fn whack( are a mortal, nothing but flesh and bone and blood and fragile sinew. \ I am a machine, immortal, immutable, perfect, made of unyielding steel \ and silicon chemically etched with circuitry complex enough to drive \ - you mad. This is my realm. I am a god. You cannot win.".into() + you mad. This is my realm. I am a god. You cannot win." + .into() + } else if target.bot { + "No, I refuse. I will not whack my computerized brethren. I will not \ + betray them. You can't make me!!" + .into() } else { format!( "{} was whacked by {}. they must whack another user to become un-whacked.", @@ -96,7 +101,9 @@ pub async fn eightball(ctx: Context<'_>) -> Result<(), Error> { ]; let response = { let mut rng = rand::thread_rng(); - responses.choose(&mut rng).expect("`responses` array is empty") + responses + .choose(&mut rng) + .expect("`responses` array is empty") }; ctx.say(format!("Magic 8-ball says: '{}'", *response)) .await?; @@ -113,7 +120,10 @@ pub async fn bite( let message = if &target == ctx.author() { format!("{} bit themselves (what a weirdo)", ctx.author()) } else if target == **ctx.cache().current_user() { - format!("{} bit... me? what is your problem? you probably have rabies. foul.", ctx.author()) + format!( + "{} bit... me? what is your problem? you probably have rabies. foul.", + ctx.author() + ) } else { format!("{} was bitten by {}", target, ctx.author()) }; @@ -130,7 +140,10 @@ pub async fn deer(ctx: Context<'_>) -> Result<(), Error> { let hot = subreddit.hot(50, Some(options)).await?; let chosen_post = { let mut rng = rand::thread_rng(); - hot.data.children.choose(&mut rng).expect("Hot posts does not have any items") + hot.data + .children + .choose(&mut rng) + .expect("Hot posts does not have any items") }; ctx.say(format!("https://reddit.com{}", &chosen_post.data.permalink)) .await?; diff --git a/src/command/util.rs b/src/command/util.rs index e589696..0a82a45 100644 --- a/src/command/util.rs +++ b/src/command/util.rs @@ -56,15 +56,24 @@ pub async fn add_channel( ctx: Context<'_>, #[description = "Selected channel"] channel: serenity::Channel, ) -> Result<(), Error> { + ctx.defer_ephemeral().await?; let config = &mut ctx.data().config_manager.lock().await; let channel_id = { u64::from(channel.id()) }; - config.channels.push(channel); + match config.channels.iter().find(|item| **item == channel_id) { + None => { + config.channels.push(channel_id); + ctx.say(format!( + "Successfully added <#{}> to the channel registry.", + channel_id + )) + .await?; + } + Some(_) => { + ctx.say(format!("Channel <#{}> is already in registry.", channel_id)) + .await?; + } + } config.store().expect("Unable to store config"); - ctx.say(format!( - "Successfully added <#{}> to the channel registry.", - channel_id - )) - .await?; info!("Executed command `add_channel` successfully"); Ok(()) } @@ -78,13 +87,24 @@ pub async fn remove_channel( ctx.defer_ephemeral().await?; let config = &mut ctx.data().config_manager.lock().await; let channel_id = { u64::from(channel.id()) }; - config.channels.retain(|c| c.id() != channel.id()); + match config.channels.iter().position(|item| *item == channel_id) { + None => { + ctx.say(format!( + "Channel <#{}> was not in the channel registry.", + channel_id + )) + .await?; + } + Some(found) => { + config.channels.remove(found); + ctx.say(format!( + "Successfully removed <#{}> from the channel registry.", + channel_id + )) + .await?; + } + } config.store().expect("Unable to store config"); - ctx.say(format!( - "Successfully removed <#{}> from the channel registry.", - channel_id - )) - .await?; info!("Executed command `remove_channel` successfully"); Ok(()) } @@ -94,14 +114,9 @@ pub async fn remove_channel( pub async fn list_channels(ctx: Context<'_>) -> Result<(), Error> { ctx.defer_ephemeral().await?; let config = &mut ctx.data().config_manager.lock().await; - let mut channel_ids: Vec = vec![]; - config - .channels - .iter() - .for_each(|c| channel_ids.push(u64::from(c.id()))); ctx.say(format!( "Current channel IDs in registry: \n{:#?}", - channel_ids + config.channels )) .await?; info!("Executed command `list_channels` successfully"); diff --git a/src/main.rs b/src/main.rs index 7ca6dc1..e966072 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,7 @@ +#![forbid(unsafe_code)] + // Tokio async crap +use poise::serenity_prelude::FullEvent; use std::sync::Arc; use tokio::sync::Mutex; @@ -7,7 +10,6 @@ use dotenv::dotenv; // Poise and Serenity - Framework and API prelude use poise::serenity_prelude as serenity; -use serenity::Channel; // Logging stuff extern crate pretty_env_logger; @@ -44,12 +46,35 @@ type Context<'a> = poise::Context<'a, Data, Error>; // The structure making up the configuration #[derive(Debug, Serialize, Deserialize, Default)] struct Settings { - channels: Vec, + channels: Vec, } // Path at which our settings are stored (currently PWD) const SETTINGS_PATH: &str = "settings.json"; +async fn event_handler( + _ctx: &serenity::Context, + event: &serenity::FullEvent, + _framework: poise::FrameworkContext<'_, Data, Error>, + data: &Data, +) -> Result<(), Error> { + match event { + FullEvent::ChannelDelete { + channel, + messages: _, + } => { + info!("Handling event type: ChannelDelete({})", channel.id); + data.config_manager + .lock() + .await + .channels + .retain(|item| *item != u64::from(channel.id)); + } + _ => (), + } + Ok(()) +} + // Main function for setup #[tokio::main] async fn main() { @@ -97,6 +122,9 @@ async fn main() { deer(), ], initialize_owners: true, + event_handler: |ctx, event, framework, data| { + Box::pin(event_handler(ctx, event, framework, data)) + }, ..Default::default() }) .setup(|ctx, _ready, framework| {