Format and cleanup code

This commit is contained in:
2024-12-18 20:13:12 -05:00
parent aa7bad945a
commit 83ef124c41
15 changed files with 106 additions and 109 deletions

View File

@@ -1,13 +1,13 @@
#!/bin/sh #!/bin/bash
# Enable exporting variables # Enable exporting variables
set -a set -a
# Source the default env variables # Source the default env variables
echo "Sourcing build environment" echo "Sourcing build environment..."
source .env source .env
# If there is a .env.local present, source it # If there is a .env.local present, source it
echo "Sourcing custom environment" echo "Sourcing custom environment..."
if [ -f .env.local ]; then if [ -f .env.local ]; then
source ./.env.local source ./.env.local
fi fi

59
src/bot/chat/mod.rs Normal file
View File

@@ -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<Message> {
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<Message> {
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}");
}
}
}

View File

@@ -52,12 +52,12 @@ pub async fn leave_voice_channel(manager: &Arc<Songbird>, guild_id: &GuildId) ->
} }
/** /**
* Checks if a URL is valid and if it is a playlist. * Validates whether the given string is a properly formatted URL.
* 1st tuple value is if the URL is valid. *
* 2nd tuple value is if the URL is a playlist. * Returns `true` if the input string is a valid URL, otherwise `false`.
*/ */
fn is_valid_url(url: &str) -> bool { fn is_valid_url(url: &str) -> bool {
Url::parse(url).ok().map_or(false, |valid_url| true) Url::parse(url).is_ok()
} }
/** /**

View File

@@ -2,8 +2,7 @@ use serenity::{
all::{CommandInteraction, CreateCommand}, all::{CommandInteraction, CreateCommand},
prelude::*, prelude::*,
}; };
use crate::bot::chat::{edit_response, process_message};
use crate::bot::commands::{edit_response, process_message};
use super::get_songbird; use super::get_songbird;

View File

@@ -3,7 +3,7 @@ use serenity::{
prelude::*, prelude::*,
}; };
use crate::bot::commands::{edit_response, process_message}; use crate::bot::chat::{edit_response, process_message};
use super::get_songbird; use super::get_songbird;

View File

@@ -7,7 +7,6 @@ use songbird::input::{Input, YoutubeDl};
use songbird::tracks::TrackHandle; use songbird::tracks::TrackHandle;
use songbird::{Event, EventHandler, Songbird, TrackEvent}; use songbird::{Event, EventHandler, Songbird, TrackEvent};
use crate::bot::commands::audio::leave_voice_channel;
use crate::data::guilds::GuildCache; use crate::data::guilds::GuildCache;
use crate::bot::ytdlp::{YtDlp, YtDlpItem}; use crate::bot::ytdlp::{YtDlp, YtDlpItem};
use crate::error::{SirenResult, Error as SirenError}; 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 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) { pub async fn run(ctx: &Context, command: &CommandInteraction) {
// Process the command options // Process the command options

View File

@@ -3,7 +3,7 @@ use serenity::{
prelude::*, prelude::*,
}; };
use crate::bot::commands::{edit_response, process_message}; use crate::bot::chat::{edit_response, process_message};
use super::get_songbird; use super::get_songbird;

View File

@@ -3,7 +3,7 @@ use serenity::{
prelude::*, prelude::*,
}; };
use crate::bot::commands::{edit_response, process_message}; use crate::bot::chat::{edit_response, process_message};
use super::get_songbird; use super::get_songbird;

View File

@@ -3,7 +3,7 @@ use serenity::{
prelude::*, prelude::*,
}; };
use crate::bot::commands::{edit_response, process_message}; use crate::bot::chat::{edit_response, process_message};
use super::get_songbird; use super::get_songbird;

View File

@@ -9,7 +9,7 @@ use songbird::Songbird;
use crate::data::guilds::GuildCache; 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; use super::get_songbird;

View File

@@ -5,15 +5,15 @@ use serenity::all::{
CreateEmbed, CreateEmbedFooter, EditInteractionResponse, Timestamp, 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) { pub async fn run(ctx: &Context, command: &CommandInteraction) {
// Create the initial response // Create the initial response
create_response(&ctx, &command, format!("Processing..."), true).await; process_message(&ctx, &command, true).await;
// Process the command options // Process the command options
let title = command.data.options.get(0).unwrap().value.as_str().unwrap(); 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 let description = command
.data .data
.options .options

View File

@@ -4,7 +4,7 @@ use serenity::all::{
UserId, 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) { pub async fn run(ctx: &Context, command: &CommandInteraction) {
// Check if the roll result is private // Check if the roll result is private
@@ -24,7 +24,7 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) {
.find(|opt| opt.name == "user") .find(|opt| opt.name == "user")
.and_then(|o| o.value.as_mentionable()); .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 let dice_string = match command
.data .data

View File

@@ -1,66 +1,5 @@
use serenity::prelude::*;
use serenity::all::{
CommandInteraction, CreateInteractionResponse, CreateInteractionResponseMessage, CreateMessage,
EditInteractionResponse, InteractionResponseFlags, Message, User, UserId,
};
pub mod audio; pub mod audio;
pub mod chat; pub mod chat;
pub mod event; pub mod event;
pub mod fun; pub mod fun;
pub mod utility; 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<Message> {
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<Message> {
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}");
}
}
}

View File

@@ -6,7 +6,7 @@ use serenity::prelude::*;
use crate::data::guilds::GuildCache; use crate::data::guilds::GuildCache;
use super::{commands, oai}; use super::{commands, oai};
use super::commands::create_response; use super::chat::create_response;
pub struct Handler { pub struct Handler {
// Open AI Config // 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) { async fn ready(&self, ctx: Context, ready: Ready) {
if ready.guilds.is_empty() { if ready.guilds.is_empty() {
log::warn!("No ready guilds found"); 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;
}
}
}
}
} }

View File

@@ -1,3 +1,4 @@
mod chat;
pub mod commands; pub mod commands;
pub mod handler; pub mod handler;
pub mod oai; pub mod oai;