mirror of
https://github.com/eRgo35/lyra.git
synced 2026-02-04 04:16:11 +01:00
music commands plus activity
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -895,6 +895,7 @@ name = "lyra"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"dotenv",
|
||||
"regex",
|
||||
"reqwest",
|
||||
"serenity",
|
||||
"songbird",
|
||||
|
||||
@@ -5,6 +5,7 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
dotenv = "0.15.0"
|
||||
regex = "1.10.3"
|
||||
reqwest = "0.11.23"
|
||||
serenity = "0.12.0"
|
||||
songbird = { version = "0.4.0", features = ["builtin-queue", "serenity"] }
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
use serenity::async_trait;
|
||||
use serenity::framework::standard::macros::command;
|
||||
use serenity::framework::standard::CommandResult;
|
||||
use serenity::model::prelude::*;
|
||||
use serenity::prelude::*;
|
||||
use songbird::events::{Event, EventContext, EventHandler as VoiceEventHandler, TrackEvent};
|
||||
use songbird::events::TrackEvent;
|
||||
|
||||
use crate::commands::misc::check_msg;
|
||||
use crate::commands::{misc::check_msg, music::misc::TrackErrorNotifier};
|
||||
|
||||
#[command]
|
||||
#[only_in(guilds)]
|
||||
@@ -40,22 +39,4 @@ async fn join(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct TrackErrorNotifier;
|
||||
|
||||
#[async_trait]
|
||||
impl VoiceEventHandler for TrackErrorNotifier {
|
||||
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
|
||||
if let EventContext::Track(track_list) = ctx {
|
||||
for (state, handle) in *track_list {
|
||||
println!(
|
||||
"Track {:?} had an error: {:?}",
|
||||
handle.uuid(),
|
||||
state.playing
|
||||
);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
20
src/commands/music/misc.rs
Normal file
20
src/commands/music/misc.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use serenity::async_trait;
|
||||
use songbird::events::{Event, EventContext, EventHandler as VoiceEventHandler};
|
||||
|
||||
pub struct TrackErrorNotifier;
|
||||
|
||||
#[async_trait]
|
||||
impl VoiceEventHandler for TrackErrorNotifier {
|
||||
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
|
||||
if let EventContext::Track(track_list) = ctx {
|
||||
for (state, handle) in *track_list {
|
||||
println!(
|
||||
"Track {:?} had an error: {:?}",
|
||||
handle.uuid(),
|
||||
state.playing
|
||||
);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
pub mod deafen;
|
||||
pub mod join;
|
||||
pub mod leave;
|
||||
pub mod misc;
|
||||
pub mod mute;
|
||||
pub mod play;
|
||||
pub mod queue;
|
||||
pub mod skip;
|
||||
pub mod stop;
|
||||
pub mod undeafen;
|
||||
pub mod unmute;
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
use serenity::framework::standard::Args;
|
||||
use serenity::framework::standard::{
|
||||
macros::command,
|
||||
CommandResult,
|
||||
};
|
||||
use serenity::framework::standard::{macros::command, CommandResult};
|
||||
|
||||
use reqwest::Client as HttpClient;
|
||||
|
||||
@@ -13,8 +10,10 @@ use serenity::prelude::TypeMapKey;
|
||||
use serenity::model::prelude::*;
|
||||
|
||||
use songbird::input::YoutubeDl;
|
||||
use songbird::TrackEvent;
|
||||
|
||||
use crate::commands::misc::check_msg;
|
||||
use crate::commands::music::misc::TrackErrorNotifier;
|
||||
|
||||
pub struct HttpKey;
|
||||
|
||||
@@ -25,6 +24,25 @@ impl TypeMapKey for HttpKey {
|
||||
#[command]
|
||||
#[only_in(guilds)]
|
||||
async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||
let (guild_id, channel_id) = {
|
||||
let guild = msg.guild(&ctx.cache).unwrap();
|
||||
let channel_id = guild
|
||||
.voice_states
|
||||
.get(&msg.author.id)
|
||||
.and_then(|voice_state| voice_state.channel_id);
|
||||
|
||||
(guild.id, channel_id)
|
||||
};
|
||||
|
||||
let connect_to = match channel_id {
|
||||
Some(channel) => channel,
|
||||
None => {
|
||||
check_msg(msg.reply(ctx, "Not in a voice channel").await);
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
let url = match args.single::<String>() {
|
||||
Ok(url) => url,
|
||||
Err(_) => {
|
||||
@@ -40,8 +58,6 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||
|
||||
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>()
|
||||
@@ -54,6 +70,11 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||
.expect("Client placed at init")
|
||||
.clone();
|
||||
|
||||
if let Ok(handler_lock) = manager.join(guild_id, connect_to).await {
|
||||
let mut handler = handler_lock.lock().await;
|
||||
handler.add_global_event(TrackEvent::Error.into(), TrackErrorNotifier);
|
||||
}
|
||||
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let mut handler = handler_lock.lock().await;
|
||||
|
||||
|
||||
12
src/commands/music/queue.rs
Normal file
12
src/commands/music/queue.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
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 queue(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
Ok(())
|
||||
}
|
||||
12
src/commands/music/skip.rs
Normal file
12
src/commands/music/skip.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
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 skip(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
Ok(())
|
||||
}
|
||||
40
src/commands/music/stop.rs
Normal file
40
src/commands/music/stop.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
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 stop(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;
|
||||
let queue = handler.queue();
|
||||
|
||||
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, "Playback stopped!").await);
|
||||
} else {
|
||||
check_msg(
|
||||
msg.channel_id
|
||||
.say(&ctx.http, "Not in a voice channel to undeafen in")
|
||||
.await,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
32
src/main.rs
32
src/main.rs
@@ -1,3 +1,5 @@
|
||||
use serenity::gateway::ActivityData;
|
||||
use serenity::model::prelude::Message;
|
||||
use songbird::SerenityInit;
|
||||
|
||||
use reqwest::Client as HttpClient;
|
||||
@@ -8,13 +10,15 @@ use serenity::{
|
||||
async_trait,
|
||||
client::{Client, EventHandler},
|
||||
framework::{
|
||||
standard::{macros::group, Configuration},
|
||||
standard::{macros::group, macros::hook, Configuration},
|
||||
StandardFramework,
|
||||
},
|
||||
model::gateway::Ready,
|
||||
prelude::GatewayIntents,
|
||||
};
|
||||
|
||||
use tracing::{info, instrument};
|
||||
|
||||
mod commands;
|
||||
|
||||
// music management commands
|
||||
@@ -23,6 +27,9 @@ use crate::commands::music::join::*;
|
||||
use crate::commands::music::leave::*;
|
||||
use crate::commands::music::mute::*;
|
||||
use crate::commands::music::play::*;
|
||||
use crate::commands::music::queue::*;
|
||||
use crate::commands::music::skip::*;
|
||||
use crate::commands::music::stop::*;
|
||||
use crate::commands::music::undeafen::*;
|
||||
use crate::commands::music::unmute::*;
|
||||
|
||||
@@ -36,13 +43,26 @@ struct Handler;
|
||||
|
||||
#[async_trait]
|
||||
impl EventHandler for Handler {
|
||||
async fn ready(&self, _: Context, ready: Ready) {
|
||||
println!("{} is connected!", ready.user.name);
|
||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
||||
info!("{} [{}] connected successfully!", ready.user.name, ready.user.id);
|
||||
let prefix = std::env::var("PREFIX").expect("Environment variable `PREFIX` not found!");
|
||||
ctx.set_activity(Some(ActivityData::listening(prefix + "help")));
|
||||
}
|
||||
}
|
||||
|
||||
#[hook]
|
||||
async fn before(_: &Context, msg: &Message, command_name: &str) -> bool {
|
||||
info!(
|
||||
"Received command [{}] from user [{}]",
|
||||
command_name, msg.author.name
|
||||
);
|
||||
true
|
||||
}
|
||||
|
||||
#[group]
|
||||
#[commands(join, deafen, leave, mute, play, unmute, undeafen, ping, kashi)]
|
||||
#[commands(
|
||||
join, deafen, leave, mute, play, unmute, undeafen, ping, kashi, queue, stop, skip
|
||||
)]
|
||||
struct General;
|
||||
|
||||
#[tokio::main]
|
||||
@@ -54,15 +74,15 @@ async fn main() {
|
||||
std::env::var("DISCORD_TOKEN").expect("Environment variable `DISCORD_TOKEN` not found!");
|
||||
let prefix = std::env::var("PREFIX").expect("Environment variable `PREFIX` not found!");
|
||||
|
||||
let framework = StandardFramework::new().group(&GENERAL_GROUP);
|
||||
let framework = StandardFramework::new().before(before).group(&GENERAL_GROUP);
|
||||
framework.configure(Configuration::new().prefix(prefix));
|
||||
|
||||
let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT;
|
||||
|
||||
let mut client = Client::builder(&token, intents)
|
||||
.event_handler(Handler)
|
||||
.framework(framework)
|
||||
.register_songbird()
|
||||
.event_handler(Handler)
|
||||
.type_map_insert::<HttpKey>(HttpClient::new())
|
||||
.await
|
||||
.expect("Error creating client");
|
||||
|
||||
Reference in New Issue
Block a user