more music commands

This commit is contained in:
2024-01-30 16:25:56 +01:00
parent a9006f1068
commit 0006cd2ac8
6 changed files with 164 additions and 14 deletions

View File

@@ -12,7 +12,7 @@ async fn leave(ctx: &Context, msg: &Message) -> CommandResult {
let manager = songbird::get(ctx)
.await
.expect("Client placed in at init.")
.expect("Client placed in at init")
.clone();
let has_handler = manager.get(guild_id).is_some();

View File

@@ -1,4 +1,7 @@
pub mod deafen;
pub mod join;
pub mod leave;
pub mod mute;
pub mod mute;
pub mod play;
pub mod undeafen;
pub mod unmute;

View File

@@ -0,0 +1,78 @@
use serenity::framework::standard::Args;
use serenity::framework::standard::{
macros::command,
CommandResult,
};
use reqwest::Client as HttpClient;
use serenity::client::Context;
use serenity::prelude::TypeMapKey;
use serenity::model::prelude::*;
use songbird::input::YoutubeDl;
use crate::commands::misc::check_msg;
pub struct HttpKey;
impl TypeMapKey for HttpKey {
type Value = HttpClient;
}
#[command]
#[only_in(guilds)]
async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let url = match args.single::<String>() {
Ok(url) => url,
Err(_) => {
check_msg(
msg.channel_id
.say(&ctx.http, "Must provide a URL to a video or audio")
.await,
);
return Ok(());
}
};
let do_search = !url.starts_with("http");
let guild_id = msg.guild_id.unwrap();
let http_client = {
let data = ctx.data.read().await;
data.get::<HttpKey>()
.cloned()
.expect("Guaranteed to exist in the typemap.")
};
let manager = songbird::get(ctx)
.await
.expect("Client placed at init")
.clone();
if let Some(handler_lock) = manager.get(guild_id) {
let mut handler = handler_lock.lock().await;
let src = if do_search {
YoutubeDl::new(http_client, url)
} else {
YoutubeDl::new(http_client, url)
};
let _ = handler.play_input(src.clone().into());
check_msg(msg.channel_id.say(&ctx.http, "Playing song").await);
} else {
check_msg(
msg.channel_id
.say(&ctx.http, "Not in a voice channel!")
.await,
);
}
Ok(())
}

View File

@@ -0,0 +1,38 @@
use serenity::framework::standard::macros::command;
use serenity::framework::standard::CommandResult;
use serenity::model::prelude::*;
use serenity::prelude::*;
use crate::commands::misc::check_msg;
#[command]
#[only_in(guilds)]
async fn undeafen(ctx: &Context, msg: &Message) -> CommandResult {
let guild_id = msg.guild_id.unwrap();
let manager = songbird::get(ctx)
.await
.expect("Client placed at init")
.clone();
if let Some(handler_lock) = manager.get(guild_id) {
let mut handler = handler_lock.lock().await;
if let Err(e) = handler.deafen(false).await {
check_msg(
msg.channel_id
.say(&ctx.http, format!("Failed: {:?}", e))
.await,
);
}
check_msg(msg.channel_id.say(&ctx.http, "Undeafened").await);
} else {
check_msg(
msg.channel_id
.say(&ctx.http, "Not in a voice channel to undeafen in")
.await,
);
}
Ok(())
}

View File

@@ -0,0 +1,38 @@
use serenity::framework::standard::macros::command;
use serenity::framework::standard::CommandResult;
use serenity::model::prelude::*;
use serenity::prelude::*;
use crate::commands::misc::check_msg;
#[command]
#[only_in(guilds)]
async fn unmute(ctx: &Context, msg: &Message) -> CommandResult {
let guild_id = msg.guild_id.unwrap();
let manager = songbird::get(ctx)
.await
.expect("Client placed at init")
.clone();
if let Some(handler_lock) = manager.get(guild_id) {
let mut handler = handler_lock.lock().await;
if let Err(e) = handler.mute(false).await {
check_msg(
msg.channel_id
.say(&ctx.http, format!("Failed: {:?}", e))
.await,
);
}
check_msg(msg.channel_id.say(&ctx.http, "Unmuted").await);
} else {
check_msg(
msg.channel_id
.say(&ctx.http, "Not in a voice channel to unmute in")
.await,
);
}
Ok(())
}