WIP: code compiles now, connects to VC, does not play audio yet
This commit is contained in:
parent
6685163c96
commit
880e81646d
7 changed files with 280 additions and 138 deletions
|
@ -1,6 +1,9 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use crate::music::preview::Preview;
|
||||
use crate::util::embed::Embed;
|
||||
use lavalink_rs::client::LavalinkClient;
|
||||
use serenity::all::{CommandInteraction, Context};
|
||||
use serenity::all::{CommandInteraction, Context, GuildId};
|
||||
use serenity::builder::{CreateCommand, CreateEmbed};
|
||||
|
||||
pub async fn run(ctx: &Context, command: &CommandInteraction, llc: &LavalinkClient) -> CreateEmbed {
|
||||
|
@ -17,47 +20,27 @@ pub async fn run(ctx: &Context, command: &CommandInteraction, llc: &LavalinkClie
|
|||
}
|
||||
};
|
||||
|
||||
let now_plaing = match music_queue::get_now_playing(&guild_id).await {
|
||||
Some(ytdl) => ytdl,
|
||||
None => {
|
||||
return Embed::create_error_respose(
|
||||
username,
|
||||
"Not playing",
|
||||
"I'm not playing anything!",
|
||||
);
|
||||
}
|
||||
let Some(player_context) = llc.get_player_context(guild_id.get()) else {
|
||||
return Embed::create_error_respose(username, "Not playing", "There is no player context and therefore there is nothing playing.");
|
||||
};
|
||||
|
||||
let now_handle = match music_queue::get_now_playing_track_handle(&guild_id).await {
|
||||
Some(handle) => handle,
|
||||
None => {
|
||||
return Embed::create_error_respose(
|
||||
username,
|
||||
"Cannot get TrackHandle",
|
||||
"The TrackHandle is empty.",
|
||||
);
|
||||
}
|
||||
let Ok(player) = player_context.get_player().await else {
|
||||
return Embed::create_error_respose(username, "Can't get player", "Cannot get player from player context.");
|
||||
};
|
||||
|
||||
let manager = songbird::get(ctx)
|
||||
.await
|
||||
.expect("Cannot get Songbird")
|
||||
.clone();
|
||||
|
||||
let _ = match manager.get(*guild_id) {
|
||||
Some(handler) => handler,
|
||||
None => {
|
||||
return Embed::create_error_respose(
|
||||
username,
|
||||
"Error",
|
||||
"Error while getting the audio handler.",
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let position = now_handle.get_info().await.unwrap().position;
|
||||
|
||||
Embed::create_playing(now_plaing, username, "Currently playing")
|
||||
if let Some(npt) = player.track {
|
||||
let npti = npt.info;
|
||||
let preview_data = Preview {
|
||||
title: npti.title,
|
||||
artist: Some(npti.author.to_string()),
|
||||
duration: Some(npti.length),
|
||||
thumbnail: npti.artwork_url,
|
||||
link: npti.uri
|
||||
};
|
||||
let position = Duration::from_millis(player_context.get_player().await.expect("Can't get player").state.position);
|
||||
|
||||
return Embed::create_playing(preview_data, username, "Now playing")
|
||||
.await
|
||||
.field(
|
||||
"Position",
|
||||
|
@ -66,8 +49,11 @@ pub async fn run(ctx: &Context, command: &CommandInteraction, llc: &LavalinkClie
|
|||
position.as_secs() / 60,
|
||||
position.as_secs() % 60
|
||||
),
|
||||
true,
|
||||
)
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
return Embed::create_error_respose(username, "Not playing", "I'm currently not playing anything.");
|
||||
}
|
||||
|
||||
pub fn register() -> CreateCommand {
|
||||
|
|
|
@ -8,22 +8,22 @@ pub async fn ready_event(client: LavalinkClient, _session_id: String, event: &ev
|
|||
}
|
||||
|
||||
#[hook]
|
||||
pub async fn track_start(client: LavalinkClient, _session_id: String, event: &events::TrackStart) {
|
||||
pub async fn track_start(_client: LavalinkClient, _session_id: String, _event: &events::TrackStart) {
|
||||
|
||||
}
|
||||
|
||||
#[hook]
|
||||
pub async fn track_end_event(client: LavalinkClient, _session_id: String, event: &events::TrackEnd) {
|
||||
pub async fn track_end_event(_client: LavalinkClient, _session_id: String, _event: &events::TrackEnd) {
|
||||
|
||||
}
|
||||
|
||||
#[hook]
|
||||
pub async fn track_exception(client: LavalinkClient, _session_id: String, event: &events::TrackException) {
|
||||
pub async fn track_exception(_client: LavalinkClient, _session_id: String, _event: &events::TrackException) {
|
||||
|
||||
}
|
||||
|
||||
#[hook]
|
||||
pub async fn track_stuck(client: LavalinkClient, _session_id: String, event: &events::TrackStuck) {
|
||||
pub async fn track_stuck(_client: LavalinkClient, _session_id: String, _event: &events::TrackStuck) {
|
||||
|
||||
}
|
||||
|
||||
|
|
32
src/main.rs
32
src/main.rs
|
@ -3,8 +3,12 @@ mod music;
|
|||
mod util;
|
||||
mod events;
|
||||
|
||||
use std::ops::Deref;
|
||||
|
||||
use lavalink_rs::client::LavalinkClient;
|
||||
use lavalink_rs::model::client::NodeDistributionStrategy;
|
||||
use lavalink_rs::model::events::Events;
|
||||
use lavalink_rs::model::UserId;
|
||||
use lavalink_rs::node::NodeBuilder;
|
||||
use serenity::all::{
|
||||
CommandInteraction, CreateInteractionResponseFollowup, OnlineStatus, VoiceState,
|
||||
|
@ -29,7 +33,7 @@ use crate::events::lavalink_event_handler;
|
|||
extern crate lazy_static;
|
||||
|
||||
struct Handler {
|
||||
llc: LavalinkClient
|
||||
pub llc: LavalinkClient
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
|
@ -120,26 +124,26 @@ async fn main() {
|
|||
let status = OnlineStatus::DoNotDisturb;
|
||||
let activity = ActivityData::streaming("music", "https://twitch.tv/moonleaytv").unwrap();
|
||||
|
||||
let event = Events {
|
||||
ready: Some(lavalink_event_handler::ready_event),
|
||||
// track_start: Some(lavalink_event_handler::track_start),
|
||||
// track_end: Some(lavalink_event_handler::track_end_event),
|
||||
// track_exception: Some(lavalink_event_handler::track_exception),
|
||||
// track_stuck: Some(lavalink_event_handler::track_stuck),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let node_builder = NodeBuilder {
|
||||
hostname: config.lavalink_address,
|
||||
password: config.lavalink_password,
|
||||
user_id: UserId(config.user_id),
|
||||
is_ssl: false,
|
||||
..Default::default()
|
||||
events: event.clone(),
|
||||
session_id: None
|
||||
};
|
||||
|
||||
let event = Events {
|
||||
ready: Some(lavalink_event_handler::ready_event),
|
||||
track_start: Some(lavalink_event_handler::track_start),
|
||||
track_end: Some(lavalink_event_handler::track_end_event),
|
||||
track_exception: Some(lavalink_event_handler::track_exception),
|
||||
track_stuck: Some(lavalink_event_handler::track_stuck),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let lavalink_client = LavalinkClient::new(event, vec![node_builder]);
|
||||
tokio::spawn(async move {
|
||||
lavalink_client.start().await;
|
||||
});
|
||||
let lavalink_client = LavalinkClient::new(event, vec![node_builder], NodeDistributionStrategy::round_robin()).await;
|
||||
|
||||
// Build the client
|
||||
let mut client = Client::builder(config.discord_token, GatewayIntents::empty())
|
||||
|
|
|
@ -2,11 +2,12 @@ use crate::util::embed::Embed;
|
|||
use crate::util::user_util;
|
||||
use crate::util::user_util::get_vc_id;
|
||||
use crate::music::preview::Preview;
|
||||
use lavalink_rs::client::LavalinkClient;
|
||||
use lavalink_rs::{client::LavalinkClient, player_context::QueueMessage};
|
||||
use lavalink_rs::model::search::SearchEngines;
|
||||
use lavalink_rs::model::track::TrackLoadData;
|
||||
use lavalink_rs::player_context::TrackInQueue;
|
||||
use serenity::all::{Context, CreateEmbed, GuildId, UserId};
|
||||
use songbird::error::JoinError;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Either queues the song, or start playing it instantly, depending on if there is already a song playing
|
||||
|
@ -56,7 +57,7 @@ pub async fn attempt_to_queue_song(
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
Err(why) => {
|
||||
Err(_) => {
|
||||
return Embed::create_error_respose(username, "Cannot join", "Could not join the channel.");
|
||||
}
|
||||
}
|
||||
|
@ -79,7 +80,7 @@ pub async fn attempt_to_queue_song(
|
|||
let search_query = if do_search {
|
||||
match SearchEngines::YouTube.to_query(&query) {
|
||||
Ok(x) => x,
|
||||
Err(why) => {
|
||||
Err(_) => {
|
||||
return Embed::create_error_respose(username, "Cannot generate query", "Could not generate a seach query..");
|
||||
}
|
||||
}
|
||||
|
@ -139,13 +140,17 @@ pub async fn attempt_to_queue_song(
|
|||
}
|
||||
};
|
||||
|
||||
let mut q = match player.get_queue().await {
|
||||
Ok(q) => q,
|
||||
Err(why) => {
|
||||
return Embed::create_error_respose(username, "Cannont get queue", "Could not get queue.");
|
||||
|
||||
let q = player.get_queue();
|
||||
q.append(tracks.into());
|
||||
|
||||
if let Ok(player_data) = player.get_player().await {
|
||||
if player_data.track.is_none() && q.get_track(0).await.is_ok_and(|x| x.is_some()) {
|
||||
player.skip();
|
||||
}
|
||||
};
|
||||
q.append(&mut tracks.into());
|
||||
}
|
||||
|
||||
|
||||
|
||||
Embed::create_playing(preview_data.unwrap(), username, &response_title).await
|
||||
}
|
||||
|
@ -166,54 +171,76 @@ pub async fn attempt_to_skip_current_song(
|
|||
);
|
||||
}
|
||||
|
||||
let connect_to = match get_vc_id(ctx, guild_id, user_id).await {
|
||||
let user_channel = match get_vc_id(ctx, guild_id, user_id).await {
|
||||
Some(channel_id) => channel_id,
|
||||
None => {
|
||||
return Embed::create_error_respose(username, "Error", "Cannot find channel_id.");
|
||||
}
|
||||
};
|
||||
|
||||
let manager: &Arc<songbird::Songbird> = &songbird::get(ctx) // TODO match
|
||||
.await
|
||||
.expect("Cannot get Songbird.")
|
||||
.clone();
|
||||
|
||||
let self_channel = user_util::get_self_vc_id(ctx, guild_id).await;
|
||||
if !user_util::is_self_connected_to_vc(ctx, guild_id).await {
|
||||
// self is connected to vc, check if user is in same vc
|
||||
// Self is not connected to vc, cannot skip.
|
||||
|
||||
if self_channel.is_none() {
|
||||
// Connect to VC
|
||||
manager // TODO match
|
||||
.join(*guild_id, connect_to)
|
||||
.await
|
||||
.expect("Cannot connect>...");
|
||||
}
|
||||
return Embed::create_error_respose(username, "Not connected", "I am not connected and I am not playing anything, therefore you cannot skip.");
|
||||
} else {
|
||||
let self_channel = self_channel.expect("Cannot get self channel"); // TODO: match
|
||||
let self_channel = self_channel.expect("Cannot get self channel");
|
||||
|
||||
// Check if user is in the same VC as the bot
|
||||
if self_channel != connect_to {
|
||||
if self_channel != user_channel {
|
||||
return Embed::create_error_respose(
|
||||
username,
|
||||
"You are not in my VC",
|
||||
"You have to be in my VC in order to controll the music.",
|
||||
"You have to be in my VC in order to control the music.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let head = music_queue::next(guild_id).await; // TODO match
|
||||
if head.is_none() {
|
||||
return Embed::create_error_respose(
|
||||
username,
|
||||
"Cannot find a song to play",
|
||||
"The queue is empty.",
|
||||
);
|
||||
}
|
||||
let head = head.unwrap();
|
||||
play_song(ctx, guild_id, llc, &head).await;
|
||||
let Some(player_context) = llc.get_player_context(guild_id.get()) else {
|
||||
return Embed::create_error_respose(username, "Not playing", "There is no player context and therefore there is nothing playing.");
|
||||
};
|
||||
|
||||
let Ok(player) = player_context.get_player().await else {
|
||||
return Embed::create_error_respose(username, "Can't get player", "Cannot get player from player context.");
|
||||
};
|
||||
|
||||
|
||||
if let Some(_) = player.track {
|
||||
let Ok(_) = player_context.skip() else {
|
||||
return Embed::create_error_respose(username, "Cannot skip", "Could not skip track.");
|
||||
};
|
||||
let ct = match player_context.get_player().await.expect("Can't get player").track {
|
||||
Some(data) => data.info,
|
||||
None => { // Disconnect
|
||||
let songbird = songbird::get(ctx).await.unwrap().clone();
|
||||
|
||||
let Ok(_) = llc.delete_player(guild_id.get()).await else {
|
||||
return Embed::create_error_respose(username, "Cannot delete player", "Could not delete player.");
|
||||
};
|
||||
if songbird.get(GuildId::new(guild_id.get())).is_some() {
|
||||
let Ok(_) = songbird.remove(GuildId::new(guild_id.get())).await else {
|
||||
return Embed::create_error_respose(username, "Cannot remove ref", "Cannot remove songbird ref.");
|
||||
};
|
||||
}
|
||||
|
||||
return Embed::create_error_respose(username, "No track to skip to", "Therefore I am leaving the channel.")
|
||||
}
|
||||
};
|
||||
|
||||
let preview_data = Preview {
|
||||
title: ct.title,
|
||||
artist: Some(ct.author.to_string()),
|
||||
duration: Some(ct.length),
|
||||
thumbnail: ct.artwork_url,
|
||||
link: ct.uri
|
||||
};
|
||||
|
||||
return Embed::create_playing(preview_data, username, "Skipped; Now playing").await;
|
||||
}
|
||||
|
||||
return Embed::create_error_respose(username, "Not playing", "I'm currently not playing anything.");
|
||||
|
||||
|
||||
Embed::create_playing(head, username, "Song skipped; Now playing").await
|
||||
}
|
||||
|
||||
/// Try to clear the queue and stop playing. Also leave the vc
|
||||
|
@ -248,6 +275,9 @@ pub async fn attempt_to_stop(
|
|||
);
|
||||
}
|
||||
|
||||
let player = llc.get_player_context(guild_id.get()).expect("Can't get player context.");
|
||||
player.get_queue().clear();
|
||||
|
||||
let stopped = match leave(ctx, llc, guild_id).await {
|
||||
Ok(stopped) => stopped,
|
||||
Err(e) => {
|
||||
|
@ -267,26 +297,26 @@ pub async fn attempt_to_stop(
|
|||
"I am not connected.\nI cant stop doing something, when I'm not doing it.",
|
||||
)
|
||||
} else {
|
||||
music_queue::delete_queue(guild_id).await; // Clear queue
|
||||
|
||||
Embed::create_success_response(username, "I stopped and left", "Just like you girlfriend.")
|
||||
}
|
||||
}
|
||||
|
||||
/// Make the bot leave the voice channel. Returns Ok(true) if bot was connected, returns Ok(false) if bot was not connected. Returns Err if something went wrong.
|
||||
pub async fn leave(ctx: &Context, llc: &LavalinkClient, guild_id: &GuildId) -> Result<bool, JoinError> {
|
||||
let manager = songbird::get(ctx)
|
||||
.await
|
||||
.expect("Cannot get Songbird")
|
||||
.clone();
|
||||
let Some(songbird) = songbird::get(ctx)
|
||||
.await.clone() else {
|
||||
return Err(JoinError::NoSender);
|
||||
};
|
||||
|
||||
let handler = manager.get(*guild_id);
|
||||
let has_handler = handler.is_some();
|
||||
let Ok(_) = llc.delete_player(guild_id.get()).await else {
|
||||
return Err(JoinError::Dropped);
|
||||
};
|
||||
|
||||
if has_handler {
|
||||
handler.unwrap().lock().await.stop();
|
||||
manager.remove(*guild_id).await?;
|
||||
return Ok(true); // Handler removed
|
||||
if songbird.get(GuildId::new(guild_id.get())).is_some() {
|
||||
let Ok(_) = songbird.remove(GuildId::new(guild_id.get())).await else {
|
||||
return Err(JoinError::Dropped);
|
||||
};
|
||||
}
|
||||
Ok(false) // No handler, so it's already stopped
|
||||
|
||||
return Ok(true)
|
||||
}
|
||||
|
|
|
@ -7,7 +7,8 @@ use std::error::Error;
|
|||
pub struct Config {
|
||||
pub discord_token: String,
|
||||
pub lavalink_address: String,
|
||||
pub lavalink_password: String
|
||||
pub lavalink_password: String,
|
||||
pub user_id: u64
|
||||
}
|
||||
|
||||
const CONFIG_FILE: &str = "./data/config.json";
|
||||
|
@ -26,7 +27,8 @@ fn create_empty() -> fs::File {
|
|||
let example_config = Config {
|
||||
discord_token: "paste_your_token".to_string(),
|
||||
lavalink_address: "paste_your_lavalink_address".to_string(),
|
||||
lavalink_password: "paste_your_lavalink_password".to_string()
|
||||
lavalink_password: "paste_your_lavalink_password".to_string(),
|
||||
user_id: 976119987330777168
|
||||
};
|
||||
|
||||
let mut config_file = fs::File::create(CONFIG_FILE).unwrap();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue