refactor, cleanup
This commit is contained in:
@@ -31,7 +31,7 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
let handler = handler_lock.lock().await;
|
||||
match handler.queue().pause() {
|
||||
Ok(_) => {
|
||||
log::debug!("Paused the track");
|
||||
log::debug!("<{guild_id}> Paused the track");
|
||||
edit_response(&ctx, &command, format!("Pausing the track")).await;
|
||||
}
|
||||
Err(err) => {
|
||||
|
||||
@@ -3,19 +3,17 @@ use std::sync::Arc;
|
||||
use serenity::all::{CommandInteraction, CommandOptionType, CreateCommand, CreateCommandOption};
|
||||
use serenity::model::prelude::GuildId;
|
||||
use serenity::{prelude::*, async_trait};
|
||||
use songbird::input::{AuxMetadata, Input, YoutubeDl};
|
||||
use songbird::input::{Input, YoutubeDl};
|
||||
use songbird::tracks::TrackHandle;
|
||||
use songbird::{Call, Event, EventHandler, Songbird, TrackEvent};
|
||||
use songbird::{Event, EventHandler, Songbird, TrackEvent};
|
||||
|
||||
use crate::database::guilds::GuildCache;
|
||||
use crate::bot::commands::audio::leave_voice_channel;
|
||||
use crate::data::guilds::GuildCache;
|
||||
use crate::bot::ytdlp::{PlaylistItem, YtDlp};
|
||||
use crate::error::{SirenResult, Error as SirenError};
|
||||
use crate::HttpKey;
|
||||
|
||||
use super::{
|
||||
create_response, edit_response, get_songbird, is_valid_url, join_voice_channel,
|
||||
leave_voice_channel,
|
||||
};
|
||||
use super::{create_response, edit_response, get_songbird, is_valid_url, join_voice_channel};
|
||||
|
||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
// Process the command options
|
||||
@@ -87,10 +85,7 @@ pub async fn play_track(
|
||||
) -> SirenResult<i32> {
|
||||
let mut track_count = 0;
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let is_queue_empty = {
|
||||
let call_handler = handler_lock.lock().await;
|
||||
call_handler.queue().is_empty()
|
||||
};
|
||||
let mut handler = handler_lock.lock().await;
|
||||
let guild = GuildCache::get_by_id(guild_id.get() as i64).await?.unwrap();
|
||||
let valid = is_valid_url(&track_url);
|
||||
// Check if the URL is valid
|
||||
@@ -123,71 +118,49 @@ pub async fn play_track(
|
||||
}
|
||||
// Add each track to the queue
|
||||
for item in playlist_items {
|
||||
match add_song(
|
||||
ctx,
|
||||
handler_lock.clone(),
|
||||
&item.url,
|
||||
is_queue_empty,
|
||||
guild.volume as f32 / 100.0,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(metadata) => {
|
||||
let track_title = metadata.title.unwrap();
|
||||
log::debug!("Added track: {}", track_title);
|
||||
let mut handler = handler_lock.lock().await;
|
||||
// handler.remove_all_global_events();
|
||||
handler.add_global_event(
|
||||
Event::Track(TrackEvent::End),
|
||||
TrackEndNotifier {
|
||||
guild_id,
|
||||
call: manager.clone(),
|
||||
},
|
||||
);
|
||||
track_count += 1;
|
||||
}
|
||||
let volume = guild.volume as f32 / 100.0;
|
||||
let http_client = {
|
||||
let data = ctx.data.read().await;
|
||||
data
|
||||
.get::<HttpKey>()
|
||||
.cloned()
|
||||
.expect("Guaranteed to exist in the typemap.")
|
||||
};
|
||||
let source = YoutubeDl::new(http_client, item.url.to_owned());
|
||||
let mut input: Input = source.into();
|
||||
let metadata = match input.aux_metadata().await {
|
||||
Ok(metadata) => metadata,
|
||||
Err(err) => {
|
||||
log::warn!("Failed to add song: {}", err);
|
||||
if let Err(why) = leave_voice_channel(&manager, &guild_id).await {
|
||||
log::error!("Failed to leave voice channel: {}", why);
|
||||
}
|
||||
log::warn!("Failed to get metadata for track: {err}");
|
||||
let _ = leave_voice_channel(&manager, &guild_id).await;
|
||||
return Err(SirenError::new(422, err.to_string()));
|
||||
}
|
||||
};
|
||||
let track_handle: TrackHandle;
|
||||
let is_queue_empty = handler.queue().is_empty();
|
||||
if is_queue_empty {
|
||||
track_handle = handler.play_input(input);
|
||||
} else {
|
||||
track_handle = handler.enqueue_input(input).await;
|
||||
}
|
||||
// Set the volume
|
||||
let _ = track_handle.set_volume(volume);
|
||||
let track_title = metadata.title.unwrap();
|
||||
log::debug!("<{guild_id}> Added track: {}", track_title);
|
||||
handler.remove_all_global_events();
|
||||
handler.add_global_event(
|
||||
Event::Track(TrackEvent::End),
|
||||
TrackEndNotifier {
|
||||
guild_id,
|
||||
call: manager.clone(),
|
||||
},
|
||||
);
|
||||
track_count += 1;
|
||||
}
|
||||
}
|
||||
Ok(track_count)
|
||||
}
|
||||
|
||||
async fn add_song(
|
||||
ctx: &Context,
|
||||
call: Arc<Mutex<Call>>,
|
||||
url: &str,
|
||||
lazy: bool,
|
||||
volume: f32,
|
||||
) -> SirenResult<AuxMetadata> {
|
||||
let http_client = {
|
||||
let data = ctx.data.read().await;
|
||||
data
|
||||
.get::<HttpKey>()
|
||||
.cloned()
|
||||
.expect("Guaranteed to exist in the typemap.")
|
||||
};
|
||||
let source = YoutubeDl::new(http_client, url.to_owned());
|
||||
let mut handler = call.lock().await;
|
||||
let mut input: Input = source.into();
|
||||
let metadata = input.aux_metadata().await.unwrap();
|
||||
let track_handle: TrackHandle;
|
||||
if lazy {
|
||||
track_handle = handler.play_input(input);
|
||||
} else {
|
||||
track_handle = handler.enqueue_input(input).await;
|
||||
}
|
||||
// Set the volume
|
||||
let _ = track_handle.set_volume(volume);
|
||||
Ok(metadata)
|
||||
}
|
||||
|
||||
pub fn get_playlist_urls(url: &str) -> SirenResult<Vec<PlaylistItem>> {
|
||||
let output = YtDlp::new()
|
||||
.arg("--flat-playlist")
|
||||
|
||||
@@ -31,7 +31,7 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
let handler = handler_lock.lock().await;
|
||||
match handler.queue().resume() {
|
||||
Ok(_) => {
|
||||
log::debug!("Resumed the track");
|
||||
log::debug!("<{guild_id}> Resumed the track");
|
||||
edit_response(&ctx, &command, format!("Resuming the track")).await;
|
||||
}
|
||||
Err(err) => {
|
||||
|
||||
@@ -31,7 +31,7 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
let handler = handler_lock.lock().await;
|
||||
match handler.queue().skip() {
|
||||
Ok(_) => {
|
||||
log::debug!("Skipped the track");
|
||||
log::debug!("<{guild_id}> Skipped the track");
|
||||
edit_response(&ctx, &command, format!("Skipping the track")).await;
|
||||
}
|
||||
Err(err) => {
|
||||
|
||||
@@ -30,7 +30,7 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let handler = handler_lock.lock().await;
|
||||
handler.queue().stop();
|
||||
log::debug!("Stopped the track");
|
||||
log::debug!("<{guild_id}> Stopped the track");
|
||||
edit_response(&ctx, &command, format!("Stopping the tracks")).await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ use serenity::{
|
||||
};
|
||||
use songbird::Songbird;
|
||||
|
||||
use crate::database::guilds::GuildCache;
|
||||
use crate::data::guilds::GuildCache;
|
||||
|
||||
use super::{get_songbird, create_response, edit_response};
|
||||
|
||||
@@ -47,7 +47,12 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
|
||||
// Set the volume
|
||||
set_volume(&manager, guild_id, volume).await;
|
||||
edit_response(&ctx, &command, format!("Setting the volume to {}", volume)).await;
|
||||
edit_response(
|
||||
&ctx,
|
||||
&command,
|
||||
format!("<{guild_id}> Setting the volume to {}", volume),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
pub async fn set_volume(manager: &Arc<Songbird>, guild_id: &GuildId, volume: i32) {
|
||||
|
||||
@@ -4,7 +4,7 @@ use serenity::model::channel::Message;
|
||||
use serenity::model::prelude::{ChannelType, PermissionOverwrite, PermissionOverwriteType};
|
||||
use serenity::prelude::*;
|
||||
|
||||
use crate::database::messages::MessageCache;
|
||||
use crate::data::messages::MessageCache;
|
||||
use crate::bot::oai::{ChatCompletionMessage, ChatCompletionRequest, GPTRole, OAI};
|
||||
|
||||
pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI) {
|
||||
|
||||
@@ -2,10 +2,10 @@ use chrono::{DateTime, NaiveDate, TimeZone, Utc};
|
||||
use regex::Regex;
|
||||
use serenity::all::{
|
||||
Color, CommandInteraction, CommandOptionType, Context, CreateCommand, CreateCommandOption,
|
||||
CreateEmbed, CreateEmbedFooter, CreateScheduledEvent, EditInteractionResponse, Timestamp,
|
||||
CreateEmbed, CreateEmbedFooter, EditInteractionResponse, Timestamp,
|
||||
};
|
||||
|
||||
use crate::{bot::commands::audio::create_response, database::events::Event};
|
||||
use crate::{bot::commands::audio::create_response, data::events::Event};
|
||||
|
||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
// Create the initial response
|
||||
|
||||
@@ -5,7 +5,7 @@ use serenity::model::gateway::Ready;
|
||||
use serenity::model::channel::Message;
|
||||
use serenity::prelude::*;
|
||||
|
||||
use crate::database::guilds::GuildCache;
|
||||
use crate::data::guilds::GuildCache;
|
||||
use super::{commands, oai};
|
||||
use super::commands::audio::create_response;
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ pub struct Event {
|
||||
|
||||
impl Event {
|
||||
pub async fn insert(&self) -> SirenResult<()> {
|
||||
let pool = crate::database::pool();
|
||||
let pool = crate::data::pool();
|
||||
sqlx::query(&format!(
|
||||
"INSERT INTO {} (
|
||||
id,
|
||||
@@ -47,7 +47,7 @@ impl Event {
|
||||
}
|
||||
|
||||
pub async fn get_by_id(id: i64) -> SirenResult<Option<Self>> {
|
||||
let pool = crate::database::pool();
|
||||
let pool = crate::data::pool();
|
||||
let item = sqlx::query_as::<_, Self>(&format!("SELECT * FROM {} WHERE id = $1", TABLE_NAME))
|
||||
.bind(id)
|
||||
.fetch_optional(pool)
|
||||
@@ -12,7 +12,7 @@ pub struct GuildCache {
|
||||
|
||||
impl GuildCache {
|
||||
pub async fn insert(&self) -> SirenResult<()> {
|
||||
let pool = crate::database::pool();
|
||||
let pool = crate::data::pool();
|
||||
sqlx::query(&format!(
|
||||
"INSERT INTO {} (
|
||||
id,
|
||||
@@ -32,7 +32,7 @@ impl GuildCache {
|
||||
}
|
||||
|
||||
pub async fn get_by_id(id: i64) -> SirenResult<Option<Self>> {
|
||||
let pool = crate::database::pool();
|
||||
let pool = crate::data::pool();
|
||||
let item = sqlx::query_as::<_, Self>(&format!("SELECT * FROM {} WHERE id = $1", TABLE_NAME))
|
||||
.bind(id)
|
||||
.fetch_optional(pool)
|
||||
@@ -42,7 +42,7 @@ impl GuildCache {
|
||||
}
|
||||
|
||||
pub async fn update(&self) -> SirenResult<()> {
|
||||
let pool = crate::database::pool();
|
||||
let pool = crate::data::pool();
|
||||
sqlx::query(&format!(
|
||||
"UPDATE {} SET
|
||||
bot_id = $2,
|
||||
@@ -19,7 +19,7 @@ pub struct MessageCache {
|
||||
|
||||
impl MessageCache {
|
||||
pub async fn insert(&self) -> SirenResult<()> {
|
||||
let pool = crate::database::pool();
|
||||
let pool = crate::data::pool();
|
||||
sqlx::query(&format!(
|
||||
"INSERT INTO {} (
|
||||
id,
|
||||
@@ -58,9 +58,9 @@ impl MessageCache {
|
||||
author_id: i64,
|
||||
limit: i64,
|
||||
) -> SirenResult<Vec<MessageCache>> {
|
||||
let pool = crate::database::pool();
|
||||
let pool = crate::data::pool();
|
||||
let messages = sqlx::query_as::<_, MessageCache>(&format!(
|
||||
"SELECT * FROM {} WHERE guild_id = $1 AND channel_id = $2 AND author_id = $3 ORDER BY created DESC LIMIT $4",
|
||||
"SELECT * FROM {} WHERE guild_id = $1 AND channel_id = $2 AND author_id = $3 ORDER BY created ASC LIMIT $4",
|
||||
TABLE_NAME
|
||||
))
|
||||
.bind(guild_id)
|
||||
@@ -9,7 +9,7 @@ use reqwest::Client as HttpClient;
|
||||
use crate::bot::handler::Handler;
|
||||
|
||||
mod bot;
|
||||
mod database;
|
||||
mod data;
|
||||
mod dnd;
|
||||
mod error;
|
||||
|
||||
@@ -23,7 +23,7 @@ impl TypeMapKey for HttpKey {
|
||||
async fn main() {
|
||||
dotenv::dotenv().ok();
|
||||
env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "warn,siren=info"));
|
||||
if let Err(err) = database::initialize().await {
|
||||
if let Err(err) = data::initialize().await {
|
||||
log::error!("Failed to initialize database: {err}");
|
||||
return;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user