Play api request
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
use reqwest::Url;
|
||||
use serenity::all::UserId;
|
||||
use serenity::client::Cache;
|
||||
use serenity::model::prelude::{GuildId, ChannelId};
|
||||
use serenity::model::user::User;
|
||||
@@ -17,12 +18,6 @@ pub mod skip;
|
||||
pub mod stop;
|
||||
pub mod volume;
|
||||
|
||||
pub async fn get_songbird(ctx: &Context) -> Arc<Songbird> {
|
||||
songbird::get(ctx)
|
||||
.await
|
||||
.expect("Songbird Voice client placed in at initialization")
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a voice channel that the user is currently in, and attempts to join it.
|
||||
*/
|
||||
@@ -30,9 +25,9 @@ pub async fn join_voice_channel(
|
||||
cache: &Arc<Cache>,
|
||||
manager: &Arc<Songbird>,
|
||||
guild_id: &GuildId,
|
||||
user: &User,
|
||||
user_id: &UserId,
|
||||
) -> SirenResult<ChannelId> {
|
||||
let channel_id = find_voice_channel(cache, guild_id, user)?;
|
||||
let channel_id = find_voice_channel(cache, guild_id, user_id)?;
|
||||
log::debug!("<{}> Joining channel {}", guild_id.get(), channel_id.get());
|
||||
manager
|
||||
.join(guild_id.to_owned(), channel_id.to_owned())
|
||||
@@ -66,7 +61,7 @@ fn is_valid_url(url: &str) -> bool {
|
||||
fn find_voice_channel(
|
||||
cache: &Arc<Cache>,
|
||||
guild_id: &GuildId,
|
||||
user: &User,
|
||||
user_id: &UserId,
|
||||
) -> SirenResult<ChannelId> {
|
||||
let guild = match guild_id.to_guild_cached(cache) {
|
||||
Some(g) => g,
|
||||
@@ -75,7 +70,7 @@ fn find_voice_channel(
|
||||
|
||||
match guild
|
||||
.voice_states
|
||||
.get(&user.id)
|
||||
.get(&user_id)
|
||||
.and_then(|voice_state| voice_state.channel_id)
|
||||
{
|
||||
Some(channel) => Ok(channel),
|
||||
|
||||
@@ -3,15 +3,14 @@ use serenity::{
|
||||
prelude::*,
|
||||
};
|
||||
use crate::bot::chat::{edit_response, process_message};
|
||||
|
||||
use super::get_songbird;
|
||||
use crate::bot::handler::get_songbird;
|
||||
|
||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
// Create the initial response
|
||||
process_message(&ctx, &command, false).await;
|
||||
|
||||
// Get the songbird manager
|
||||
let manager = get_songbird(ctx).await;
|
||||
let manager = get_songbird();
|
||||
|
||||
// Extract the guild ID
|
||||
let guild_id = match &command.guild_id {
|
||||
|
||||
@@ -4,15 +4,14 @@ use serenity::{
|
||||
};
|
||||
|
||||
use crate::bot::chat::{edit_response, process_message};
|
||||
|
||||
use super::get_songbird;
|
||||
use crate::bot::handler::get_songbird;
|
||||
|
||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
// Create the initial response
|
||||
process_message(&ctx, &command, false).await;
|
||||
|
||||
// Get the songbird manager
|
||||
let manager = get_songbird(ctx).await;
|
||||
let manager = get_songbird();
|
||||
|
||||
// Extract the guild ID
|
||||
let guild_id = match &command.guild_id {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use serenity::all::{CommandInteraction, CommandOptionType, CreateCommand, CreateCommandOption};
|
||||
use serenity::all::{
|
||||
Cache, CommandInteraction, CommandOptionType, CreateCommand, CreateCommandOption, Http,
|
||||
};
|
||||
use serenity::model::prelude::GuildId;
|
||||
use serenity::{prelude::*, async_trait};
|
||||
use songbird::input::{Input, YoutubeDl};
|
||||
@@ -12,9 +14,10 @@ use crate::bot::ytdlp::{YtDlp, YtDlpItem};
|
||||
use crate::error::{SirenResult, Error as SirenError};
|
||||
use crate::{signal_shutdown, HttpKey};
|
||||
|
||||
use super::{get_songbird, is_valid_url, join_voice_channel};
|
||||
use super::{is_valid_url, join_voice_channel};
|
||||
|
||||
use crate::bot::chat::{create_message_response, edit_response, process_message};
|
||||
use crate::bot::handler::{get_client, get_songbird};
|
||||
|
||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
// Process the command options
|
||||
@@ -34,7 +37,7 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
process_message(&ctx, &command, false).await;
|
||||
|
||||
// Get the songbird manager
|
||||
let manager = get_songbird(ctx).await;
|
||||
let manager = get_songbird();
|
||||
|
||||
// Extract the guild ID
|
||||
let guild_id = match &command.guild_id {
|
||||
@@ -51,13 +54,13 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
};
|
||||
|
||||
// Join the user's voice channel
|
||||
match join_voice_channel(&ctx.cache, &manager, guild_id, &command.user).await {
|
||||
match join_voice_channel(&ctx.cache, &manager, guild_id, &command.user.id).await {
|
||||
Ok(channel_id) => {
|
||||
log::debug!(
|
||||
"<{guild_id}> Play command executed on channel {channel_id} with track: {track_url:?}"
|
||||
);
|
||||
// Handle the track url
|
||||
match enqueue_track(ctx, manager, guild_id.to_owned(), track_url).await {
|
||||
match enqueue_track(manager, guild_id.to_owned(), track_url).await {
|
||||
Ok(items) => {
|
||||
let mut message = format!("Added {} tracks", items.len());
|
||||
if items.len() == 0 {
|
||||
@@ -81,8 +84,7 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
}
|
||||
|
||||
pub async fn enqueue_track(
|
||||
ctx: &Context,
|
||||
manager: Arc<Songbird>,
|
||||
manager: &Arc<Songbird>,
|
||||
guild_id: GuildId,
|
||||
track_url: &str,
|
||||
) -> SirenResult<Vec<YtDlpItem>> {
|
||||
@@ -112,15 +114,9 @@ pub async fn enqueue_track(
|
||||
// Add each track to the queue
|
||||
for item in &playlist_items {
|
||||
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 http_client = get_client();
|
||||
|
||||
let source = YoutubeDl::new(http_client, item.get_url().to_owned());
|
||||
let source = YoutubeDl::new(http_client.to_owned(), item.get_url().to_owned());
|
||||
let input: Input = source.into();
|
||||
let track_title = item.get_title().to_owned();
|
||||
|
||||
|
||||
@@ -4,15 +4,14 @@ use serenity::{
|
||||
};
|
||||
|
||||
use crate::bot::chat::{edit_response, process_message};
|
||||
|
||||
use super::get_songbird;
|
||||
use crate::bot::handler::get_songbird;
|
||||
|
||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
// Create the initial response
|
||||
process_message(&ctx, &command, false).await;
|
||||
|
||||
// Get the songbird manager
|
||||
let manager = get_songbird(ctx).await;
|
||||
let manager = get_songbird();
|
||||
|
||||
// Extract the guild ID
|
||||
let guild_id = match &command.guild_id {
|
||||
|
||||
@@ -4,15 +4,14 @@ use serenity::{
|
||||
};
|
||||
|
||||
use crate::bot::chat::{edit_response, process_message};
|
||||
|
||||
use super::get_songbird;
|
||||
use crate::bot::handler::get_songbird;
|
||||
|
||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
// Create the initial response
|
||||
process_message(&ctx, &command, false).await;
|
||||
|
||||
// Get the songbird manager
|
||||
let manager = get_songbird(ctx).await;
|
||||
let manager = get_songbird();
|
||||
|
||||
// Extract the guild ID
|
||||
let guild_id = match &command.guild_id {
|
||||
|
||||
@@ -4,15 +4,14 @@ use serenity::{
|
||||
};
|
||||
|
||||
use crate::bot::chat::{edit_response, process_message};
|
||||
|
||||
use super::get_songbird;
|
||||
use crate::bot::handler::get_songbird;
|
||||
|
||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
// Create the initial response
|
||||
process_message(&ctx, &command, false).await;
|
||||
|
||||
// Get the songbird manager
|
||||
let manager = get_songbird(ctx).await;
|
||||
let manager = get_songbird();
|
||||
|
||||
// Extract the guild ID
|
||||
let guild_id = match command.guild_id {
|
||||
|
||||
@@ -10,8 +10,7 @@ use songbird::Songbird;
|
||||
use crate::data::guilds::GuildCache;
|
||||
|
||||
use crate::bot::chat::{create_message_response, edit_response, process_message};
|
||||
|
||||
use super::get_songbird;
|
||||
use crate::bot::handler::get_songbird;
|
||||
|
||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
// Process the command options
|
||||
@@ -37,7 +36,7 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||
process_message(&ctx, &command, false).await;
|
||||
|
||||
// Get the songbird manager
|
||||
let manager = get_songbird(ctx).await;
|
||||
let manager = get_songbird();
|
||||
|
||||
// Extract the guild ID
|
||||
let guild_id = match &command.guild_id {
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
use std::env;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
use serenity::all::{Interaction, ResumedEvent};
|
||||
use serenity::async_trait;
|
||||
use serenity::model::gateway::Ready;
|
||||
use serenity::model::channel::Message;
|
||||
use serenity::prelude::*;
|
||||
use songbird::Songbird;
|
||||
use crate::bot::commands::chat::generate_response;
|
||||
use crate::bot::oai::OAI;
|
||||
use crate::data::guilds::GuildCache;
|
||||
use crate::HttpKey;
|
||||
use super::{commands};
|
||||
use super::chat::{create_modal_response};
|
||||
|
||||
@@ -14,6 +18,43 @@ pub struct BotHandler {
|
||||
pub oai: Option<OAI>,
|
||||
}
|
||||
|
||||
static SONGBIRD: OnceLock<Arc<Songbird>> = OnceLock::new();
|
||||
static CLIENT: OnceLock<reqwest::Client> = OnceLock::new();
|
||||
|
||||
pub fn get_songbird() -> &'static Arc<Songbird> {
|
||||
SONGBIRD.get().unwrap()
|
||||
}
|
||||
|
||||
pub fn get_client() -> &'static reqwest::Client {
|
||||
CLIENT.get().unwrap()
|
||||
}
|
||||
|
||||
impl BotHandler {
|
||||
pub fn new() -> Self {
|
||||
match env::var("OPENAI_TOKEN") {
|
||||
Ok(token) => {
|
||||
log::debug!("OpenAI functionality enabled");
|
||||
let default_model = env::var("OPENAI_MODEL").unwrap_or_else(|_| "gpt-4o-mini".to_string());
|
||||
let base_url = env::var("OPENAI_BASE_URL").unwrap();
|
||||
Self {
|
||||
oai: Some(OAI {
|
||||
client: reqwest::Client::new(),
|
||||
base_url,
|
||||
token,
|
||||
max_conversation_history: 30,
|
||||
max_tokens: 8192,
|
||||
default_model,
|
||||
}),
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
log::warn!("OpenAI functionality disabled");
|
||||
Self { oai: None }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl EventHandler for BotHandler {
|
||||
async fn message(&self, ctx: Context, msg: Message) {
|
||||
@@ -40,6 +81,20 @@ impl EventHandler for BotHandler {
|
||||
if ready.guilds.is_empty() {
|
||||
log::warn!("No ready guilds found");
|
||||
}
|
||||
|
||||
let songbird = songbird::get(&ctx).await.unwrap();
|
||||
SONGBIRD
|
||||
.set(songbird.clone())
|
||||
.expect("Songbird value could not be set");
|
||||
let http_client = {
|
||||
let data = ctx.data.read().await;
|
||||
data
|
||||
.get::<HttpKey>()
|
||||
.cloned()
|
||||
.expect("Guaranteed to exist in the typemap.")
|
||||
};
|
||||
CLIENT.set(http_client).ok();
|
||||
|
||||
log::trace!("Handling {} guilds", ready.guilds.len());
|
||||
for guild in ready.guilds {
|
||||
// Check if guild exists in database
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
mod chat;
|
||||
pub mod chat;
|
||||
pub mod commands;
|
||||
pub mod handler;
|
||||
pub mod oai;
|
||||
|
||||
Reference in New Issue
Block a user