From 83ef124c4159928b363cb946862ebd02067a5719 Mon Sep 17 00:00:00 2001 From: Benjamin Sherriff Date: Wed, 18 Dec 2024 20:13:12 -0500 Subject: [PATCH] Format and cleanup code --- scripts/apply_env.sh | 6 +-- src/bot/chat/mod.rs | 59 +++++++++++++++++++++++++++++ src/bot/commands/audio/mod.rs | 8 ++-- src/bot/commands/audio/mute.rs | 3 +- src/bot/commands/audio/pause.rs | 2 +- src/bot/commands/audio/play.rs | 3 +- src/bot/commands/audio/resume.rs | 2 +- src/bot/commands/audio/skip.rs | 2 +- src/bot/commands/audio/stop.rs | 2 +- src/bot/commands/audio/volume.rs | 2 +- src/bot/commands/event/schedule.rs | 6 +-- src/bot/commands/fun/roll.rs | 4 +- src/bot/commands/mod.rs | 61 ------------------------------ src/bot/handler.rs | 54 +++++++++++++------------- src/bot/mod.rs | 1 + 15 files changed, 106 insertions(+), 109 deletions(-) create mode 100644 src/bot/chat/mod.rs diff --git a/scripts/apply_env.sh b/scripts/apply_env.sh index 990fab9..3fa511f 100755 --- a/scripts/apply_env.sh +++ b/scripts/apply_env.sh @@ -1,13 +1,13 @@ -#!/bin/sh +#!/bin/bash # Enable exporting variables set -a # Source the default env variables -echo "Sourcing build environment" +echo "Sourcing build environment..." source .env # If there is a .env.local present, source it -echo "Sourcing custom environment" +echo "Sourcing custom environment..." if [ -f .env.local ]; then source ./.env.local fi diff --git a/src/bot/chat/mod.rs b/src/bot/chat/mod.rs new file mode 100644 index 0000000..330533d --- /dev/null +++ b/src/bot/chat/mod.rs @@ -0,0 +1,59 @@ +use serenity::all::{ + CommandInteraction, Context, CreateInteractionResponse, CreateInteractionResponseMessage, + CreateMessage, EditInteractionResponse, InteractionResponseFlags, Message, User, UserId, +}; + +pub async fn process_message(ctx: &Context, command: &CommandInteraction, private: bool) { + create_response(&ctx, &command, "Processing...".to_string(), private).await; +} + +pub async fn user_id_dm(ctx: &Context, user_id: &UserId, content: String) -> Option { + let data = CreateMessage::new().content(content.to_owned()); + return match user_id.dm(ctx, data).await { + Ok(message) => Some(message), + Err(err) => { + log::error!("Failed to create direct message for {content}\n{err}"); + None + } + }; +} + +pub async fn user_dm(ctx: &Context, user: &User, content: String) -> Option { + let data = CreateMessage::new().content(content.to_owned()); + return match user.direct_message(ctx, data).await { + Ok(message) => Some(message), + Err(err) => { + log::error!("Failed to create direct message for {content}\n{err}"); + None + } + }; +} + +pub async fn create_response( + ctx: &Context, + command: &CommandInteraction, + content: String, + private: bool, +) { + let mut data = CreateInteractionResponseMessage::new().content(content.to_owned()); + if private { + data = data.flags(InteractionResponseFlags::EPHEMERAL); + } + let builder = CreateInteractionResponse::Message(data); + match command.create_response(&ctx.http, builder).await { + Ok(_) => {} + Err(err) => { + log::error!("Failed to create response for {content}\n{err}"); + } + }; +} + +pub async fn edit_response(ctx: &Context, command: &CommandInteraction, content: String) { + let builder = EditInteractionResponse::new().content(content.to_owned()); + match command.edit_response(&ctx.http, builder).await { + Ok(_) => {} + Err(err) => { + log::error!("Failed to create response for {content}\n{err}"); + } + } +} diff --git a/src/bot/commands/audio/mod.rs b/src/bot/commands/audio/mod.rs index 6dbc101..095472e 100644 --- a/src/bot/commands/audio/mod.rs +++ b/src/bot/commands/audio/mod.rs @@ -52,12 +52,12 @@ pub async fn leave_voice_channel(manager: &Arc, guild_id: &GuildId) -> } /** - * Checks if a URL is valid and if it is a playlist. - * 1st tuple value is if the URL is valid. - * 2nd tuple value is if the URL is a playlist. + * Validates whether the given string is a properly formatted URL. + * + * Returns `true` if the input string is a valid URL, otherwise `false`. */ fn is_valid_url(url: &str) -> bool { - Url::parse(url).ok().map_or(false, |valid_url| true) + Url::parse(url).is_ok() } /** diff --git a/src/bot/commands/audio/mute.rs b/src/bot/commands/audio/mute.rs index 163ff00..80688ec 100644 --- a/src/bot/commands/audio/mute.rs +++ b/src/bot/commands/audio/mute.rs @@ -2,8 +2,7 @@ use serenity::{ all::{CommandInteraction, CreateCommand}, prelude::*, }; - -use crate::bot::commands::{edit_response, process_message}; +use crate::bot::chat::{edit_response, process_message}; use super::get_songbird; diff --git a/src/bot/commands/audio/pause.rs b/src/bot/commands/audio/pause.rs index e16e6c2..f3a541d 100644 --- a/src/bot/commands/audio/pause.rs +++ b/src/bot/commands/audio/pause.rs @@ -3,7 +3,7 @@ use serenity::{ prelude::*, }; -use crate::bot::commands::{edit_response, process_message}; +use crate::bot::chat::{edit_response, process_message}; use super::get_songbird; diff --git a/src/bot/commands/audio/play.rs b/src/bot/commands/audio/play.rs index 766fdd2..b3902ef 100644 --- a/src/bot/commands/audio/play.rs +++ b/src/bot/commands/audio/play.rs @@ -7,7 +7,6 @@ use songbird::input::{Input, YoutubeDl}; use songbird::tracks::TrackHandle; use songbird::{Event, EventHandler, Songbird, TrackEvent}; -use crate::bot::commands::audio::leave_voice_channel; use crate::data::guilds::GuildCache; use crate::bot::ytdlp::{YtDlp, YtDlpItem}; use crate::error::{SirenResult, Error as SirenError}; @@ -15,7 +14,7 @@ use crate::HttpKey; use super::{get_songbird, is_valid_url, join_voice_channel}; -use crate::bot::commands::{create_response, edit_response, process_message}; +use crate::bot::chat::{create_response, edit_response, process_message}; pub async fn run(ctx: &Context, command: &CommandInteraction) { // Process the command options diff --git a/src/bot/commands/audio/resume.rs b/src/bot/commands/audio/resume.rs index d503a46..1a5870b 100644 --- a/src/bot/commands/audio/resume.rs +++ b/src/bot/commands/audio/resume.rs @@ -3,7 +3,7 @@ use serenity::{ prelude::*, }; -use crate::bot::commands::{edit_response, process_message}; +use crate::bot::chat::{edit_response, process_message}; use super::get_songbird; diff --git a/src/bot/commands/audio/skip.rs b/src/bot/commands/audio/skip.rs index 76f7066..61880a3 100644 --- a/src/bot/commands/audio/skip.rs +++ b/src/bot/commands/audio/skip.rs @@ -3,7 +3,7 @@ use serenity::{ prelude::*, }; -use crate::bot::commands::{edit_response, process_message}; +use crate::bot::chat::{edit_response, process_message}; use super::get_songbird; diff --git a/src/bot/commands/audio/stop.rs b/src/bot/commands/audio/stop.rs index ccedbb4..a542f50 100644 --- a/src/bot/commands/audio/stop.rs +++ b/src/bot/commands/audio/stop.rs @@ -3,7 +3,7 @@ use serenity::{ prelude::*, }; -use crate::bot::commands::{edit_response, process_message}; +use crate::bot::chat::{edit_response, process_message}; use super::get_songbird; diff --git a/src/bot/commands/audio/volume.rs b/src/bot/commands/audio/volume.rs index b55a84c..237c5e4 100644 --- a/src/bot/commands/audio/volume.rs +++ b/src/bot/commands/audio/volume.rs @@ -9,7 +9,7 @@ use songbird::Songbird; use crate::data::guilds::GuildCache; -use crate::bot::commands::{create_response, edit_response, process_message}; +use crate::bot::chat::{create_response, edit_response, process_message}; use super::get_songbird; diff --git a/src/bot/commands/event/schedule.rs b/src/bot/commands/event/schedule.rs index ec132ca..2da267c 100644 --- a/src/bot/commands/event/schedule.rs +++ b/src/bot/commands/event/schedule.rs @@ -5,15 +5,15 @@ use serenity::all::{ CreateEmbed, CreateEmbedFooter, EditInteractionResponse, Timestamp, }; -use crate::{bot::commands::create_response, data::events::Event}; +use crate::{bot::chat::process_message, data::events::Event}; pub async fn run(ctx: &Context, command: &CommandInteraction) { // Create the initial response - create_response(&ctx, &command, format!("Processing..."), true).await; + process_message(&ctx, &command, true).await; // Process the command options let title = command.data.options.get(0).unwrap().value.as_str().unwrap(); - let datetime_string = command.data.options.get(1).unwrap().value.as_str().unwrap(); + // let datetime_string = command.data.options.get(1).unwrap().value.as_str().unwrap(); let description = command .data .options diff --git a/src/bot/commands/fun/roll.rs b/src/bot/commands/fun/roll.rs index 7ce3c9b..df9dcea 100644 --- a/src/bot/commands/fun/roll.rs +++ b/src/bot/commands/fun/roll.rs @@ -4,7 +4,7 @@ use serenity::all::{ UserId, }; -use crate::bot::commands::{create_response, edit_response, user_id_dm}; +use crate::bot::chat::{create_response, edit_response, user_id_dm}; pub async fn run(ctx: &Context, command: &CommandInteraction) { // Check if the roll result is private @@ -24,7 +24,7 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) { .find(|opt| opt.name == "user") .and_then(|o| o.value.as_mentionable()); - create_response(&ctx, &command, format!("Rolling..."), private).await; + create_response(&ctx, &command, "Rolling...".to_string(), private).await; let dice_string = match command .data diff --git a/src/bot/commands/mod.rs b/src/bot/commands/mod.rs index 21c0703..0c6d1e5 100644 --- a/src/bot/commands/mod.rs +++ b/src/bot/commands/mod.rs @@ -1,66 +1,5 @@ -use serenity::prelude::*; -use serenity::all::{ - CommandInteraction, CreateInteractionResponse, CreateInteractionResponseMessage, CreateMessage, - EditInteractionResponse, InteractionResponseFlags, Message, User, UserId, -}; - pub mod audio; pub mod chat; pub mod event; pub mod fun; pub mod utility; - -pub async fn process_message(ctx: &Context, command: &CommandInteraction, private: bool) { - create_response(&ctx, &command, format!("Processing..."), private).await; -} - -pub async fn user_id_dm(ctx: &Context, user_id: &UserId, content: String) -> Option { - let data = CreateMessage::new().content(content.to_owned()); - return match user_id.dm(ctx, data).await { - Ok(message) => Some(message), - Err(err) => { - log::error!("Failed to create direct message for {content}\n{err}"); - None - } - }; -} - -pub async fn user_dm(ctx: &Context, user: &User, content: String) -> Option { - let data = CreateMessage::new().content(content.to_owned()); - return match user.direct_message(ctx, data).await { - Ok(message) => Some(message), - Err(err) => { - log::error!("Failed to create direct message for {content}\n{err}"); - None - } - }; -} - -pub async fn create_response( - ctx: &Context, - command: &CommandInteraction, - content: String, - private: bool, -) { - let mut data = CreateInteractionResponseMessage::new().content(content.to_owned()); - if private { - data = data.flags(InteractionResponseFlags::EPHEMERAL); - } - let builder = CreateInteractionResponse::Message(data); - match command.create_response(&ctx.http, builder).await { - Ok(_) => {} - Err(err) => { - log::error!("Failed to create response for {content}\n{err}"); - } - }; -} - -pub async fn edit_response(ctx: &Context, command: &CommandInteraction, content: String) { - let builder = EditInteractionResponse::new().content(content.to_owned()); - match command.edit_response(&ctx.http, builder).await { - Ok(_) => {} - Err(err) => { - log::error!("Failed to create response for {content}\n{err}"); - } - } -} diff --git a/src/bot/handler.rs b/src/bot/handler.rs index 535f197..3990b2b 100644 --- a/src/bot/handler.rs +++ b/src/bot/handler.rs @@ -6,7 +6,7 @@ use serenity::prelude::*; use crate::data::guilds::GuildCache; use super::{commands, oai}; -use super::commands::create_response; +use super::chat::create_response; pub struct Handler { // Open AI Config @@ -42,32 +42,6 @@ impl EventHandler for Handler { } } - async fn interaction_create(&self, ctx: Context, interaction: Interaction) { - if let Interaction::Command(command) = interaction { - log::trace!("Received command interaction: {command:#?}"); - match command.data.name.as_str() { - // Match commands without returns - "play" => commands::audio::play::run(&ctx, &command).await, - "stop" => commands::audio::stop::run(&ctx, &command).await, - "pause" => commands::audio::pause::run(&ctx, &command).await, - "resume" => commands::audio::resume::run(&ctx, &command).await, - "mute" => commands::audio::mute::run(&ctx, &command).await, - "skip" => commands::audio::skip::run(&ctx, &command).await, - "volume" => commands::audio::volume::run(&ctx, &command).await, - "schedule" => commands::event::schedule::run(&ctx, &command).await, - "roll" => commands::fun::roll::run(&ctx, &command).await, - _ => { - let content: String = match command.data.name.as_str() { - // Match commands with string returns - "ping" => commands::utility::ping::run(&command.data.options), - _ => "Unknown command".to_string(), - }; - create_response(&ctx, &command, content, false).await; - } - } - } - } - async fn ready(&self, ctx: Context, ready: Ready) { if ready.guilds.is_empty() { log::warn!("No ready guilds found"); @@ -116,4 +90,30 @@ impl EventHandler for Handler { }; } } + + async fn interaction_create(&self, ctx: Context, interaction: Interaction) { + if let Interaction::Command(command) = interaction { + log::trace!("Received command interaction: {command:#?}"); + match command.data.name.as_str() { + // Match commands without returns + "play" => commands::audio::play::run(&ctx, &command).await, + "stop" => commands::audio::stop::run(&ctx, &command).await, + "pause" => commands::audio::pause::run(&ctx, &command).await, + "resume" => commands::audio::resume::run(&ctx, &command).await, + "mute" => commands::audio::mute::run(&ctx, &command).await, + "skip" => commands::audio::skip::run(&ctx, &command).await, + "volume" => commands::audio::volume::run(&ctx, &command).await, + "schedule" => commands::event::schedule::run(&ctx, &command).await, + "roll" => commands::fun::roll::run(&ctx, &command).await, + _ => { + let content: String = match command.data.name.as_str() { + // Match commands with string returns + "ping" => commands::utility::ping::run(&command.data.options), + _ => "Unknown command".to_string(), + }; + create_response(&ctx, &command, content, false).await; + } + } + } + } } diff --git a/src/bot/mod.rs b/src/bot/mod.rs index 39e6d3f..993392b 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -1,3 +1,4 @@ +mod chat; pub mod commands; pub mod handler; pub mod oai;