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) let manager = songbird::get(ctx)
.await .await
.expect("Client placed in at init.") .expect("Client placed in at init")
.clone(); .clone();
let has_handler = manager.get(guild_id).is_some(); let has_handler = manager.get(guild_id).is_some();

View File

@@ -1,4 +1,7 @@
pub mod deafen; pub mod deafen;
pub mod join; pub mod join;
pub mod leave; 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(())
}

View File

@@ -12,7 +12,7 @@ use serenity::{
StandardFramework, StandardFramework,
}, },
model::gateway::Ready, model::gateway::Ready,
prelude::{GatewayIntents, TypeMapKey}, prelude::GatewayIntents,
}; };
mod commands; mod commands;
@@ -22,6 +22,9 @@ use crate::commands::music::deafen::*;
use crate::commands::music::join::*; use crate::commands::music::join::*;
use crate::commands::music::leave::*; use crate::commands::music::leave::*;
use crate::commands::music::mute::*; use crate::commands::music::mute::*;
use crate::commands::music::play::*;
use crate::commands::music::undeafen::*;
use crate::commands::music::unmute::*;
// tools // tools
use crate::commands::tools::ping::*; use crate::commands::tools::ping::*;
@@ -29,12 +32,6 @@ use crate::commands::tools::ping::*;
// kashi // kashi
use crate::commands::kashi::kashi::*; use crate::commands::kashi::kashi::*;
struct HttpKey;
impl TypeMapKey for HttpKey {
type Value = HttpClient;
}
struct Handler; struct Handler;
#[async_trait] #[async_trait]
@@ -45,11 +42,7 @@ impl EventHandler for Handler {
} }
#[group] #[group]
#[commands( #[commands(join, deafen, leave, mute, play, unmute, undeafen, ping, kashi)]
join, deafen, leave, mute,
ping,
kashi
)]
struct General; struct General;
#[tokio::main] #[tokio::main]