fix intents, small refactor

This commit is contained in:
Gabriel Fontes
2023-06-11 02:07:35 -03:00
parent 076763a9c9
commit 486e8b5306
2 changed files with 38 additions and 41 deletions

View File

@@ -1,4 +1,4 @@
use anyhow::Result; use anyhow::{Context as ErrContext, Result};
use serenity::{ use serenity::{
client::Client as DiscordClient, client::Client as DiscordClient,
framework::standard::StandardFramework, framework::standard::StandardFramework,
@@ -47,27 +47,32 @@ impl Client {
} }
pub async fn discord(&self, ss: SubsonicClient) -> Result<DiscordClient> { pub async fn discord(&self, ss: SubsonicClient) -> Result<DiscordClient> {
Ok( let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT;
DiscordClient::builder(&self.discord_token, GatewayIntents::default()) let framework = StandardFramework::new()
.event_handler(Handler) .configure(|c| c.prefix("~"))
.framework( .group(&GENERAL_GROUP)
StandardFramework::new() .after(after_hook);
.configure(|c| c.prefix("~"))
.group(&GENERAL_GROUP) let client = DiscordClient::builder(&self.discord_token, intents)
.after(after_hook), .event_handler(Handler)
) .framework(framework)
.type_map_insert::<MusicClient>(ss) .type_map_insert::<MusicClient>(ss)
.register_songbird() .register_songbird()
.await?, .await?;
)
Ok(client)
} }
pub async fn subsonic(&self) -> Result<SubsonicClient> { pub async fn subsonic(&self) -> Result<SubsonicClient> {
Ok(SubsonicClient::new( let client = SubsonicClient::new(&self.ss_url, &self.ss_user, &self.ss_password)?;
&self.ss_url, log::info!("Created subsonic client: {client:?}");
&self.ss_user, // Check that connection works
&self.ss_password, client
)?) .ping()
.await
.with_context(|| "Could not connect to subsonic server.")?;
Ok(client)
} }
} }

View File

@@ -1,5 +1,4 @@
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Context as ErrContext, Result};
use log::warn;
use serenity::{ use serenity::{
async_trait, async_trait,
client::{Context, EventHandler}, client::{Context, EventHandler},
@@ -93,7 +92,8 @@ async fn song(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let result = music_client let result = music_client
.search(args.rest(), ignore, ignore, search_size) .search(args.rest(), ignore, ignore, search_size)
.await? .await
.with_context(|| anyhow!("Could not search for song"))?
.songs; .songs;
let song = result let song = result
@@ -156,7 +156,7 @@ async fn random(ctx: &Context, msg: &Message) -> CommandResult {
#[command] #[command]
/// Skip current song /// Skip current song
async fn skip(ctx: &Context, msg: &Message) -> CommandResult { async fn skip(ctx: &Context, msg: &Message) -> CommandResult {
let call = get_handler(ctx, msg).await?; let call = get_call(ctx, msg).await?;
let handler = call.lock().await; let handler = call.lock().await;
let queue = handler.queue(); let queue = handler.queue();
@@ -170,7 +170,7 @@ async fn skip(ctx: &Context, msg: &Message) -> CommandResult {
#[command] #[command]
/// Clear queue and stop playing /// Clear queue and stop playing
async fn stop(ctx: &Context, msg: &Message) -> CommandResult { async fn stop(ctx: &Context, msg: &Message) -> CommandResult {
let call = get_handler(ctx, msg).await?; let call = get_call(ctx, msg).await?;
let handler = call.lock().await; let handler = call.lock().await;
let queue = handler.queue(); let queue = handler.queue();
@@ -184,7 +184,7 @@ async fn stop(ctx: &Context, msg: &Message) -> CommandResult {
#[command] #[command]
/// Pause playing current song /// Pause playing current song
async fn pause(ctx: &Context, msg: &Message) -> CommandResult { async fn pause(ctx: &Context, msg: &Message) -> CommandResult {
let call = get_handler(ctx, msg).await?; let call = get_call(ctx, msg).await?;
let handler = call.lock().await; let handler = call.lock().await;
let queue = handler.queue(); let queue = handler.queue();
@@ -202,7 +202,7 @@ async fn pause(ctx: &Context, msg: &Message) -> CommandResult {
#[aliases(resume)] #[aliases(resume)]
/// Resume playing current song /// Resume playing current song
async fn resume(ctx: &Context, msg: &Message) -> CommandResult { async fn resume(ctx: &Context, msg: &Message) -> CommandResult {
let call = get_handler(ctx, msg).await?; let call = get_call(ctx, msg).await?;
let handler = call.lock().await; let handler = call.lock().await;
let queue = handler.queue(); let queue = handler.queue();
@@ -217,7 +217,7 @@ async fn resume(ctx: &Context, msg: &Message) -> CommandResult {
#[aliases(nowplaying, now, np, playing)] #[aliases(nowplaying, now, np, playing)]
/// Show currently playing song /// Show currently playing song
async fn nowplaying(ctx: &Context, msg: &Message) -> CommandResult { async fn nowplaying(ctx: &Context, msg: &Message) -> CommandResult {
let call = get_handler(ctx, msg).await?; let call = get_call(ctx, msg).await?;
let handler = call.lock().await; let handler = call.lock().await;
let queue = handler.queue(); let queue = handler.queue();
@@ -235,7 +235,7 @@ async fn nowplaying(ctx: &Context, msg: &Message) -> CommandResult {
#[aliases(q)] #[aliases(q)]
/// Show song queue /// Show song queue
async fn queue(ctx: &Context, msg: &Message) -> CommandResult { async fn queue(ctx: &Context, msg: &Message) -> CommandResult {
let call = get_handler(ctx, msg).await?; let call = get_call(ctx, msg).await?;
let handler = call.lock().await; let handler = call.lock().await;
let current_queue = handler.queue().current_queue(); let current_queue = handler.queue().current_queue();
@@ -244,6 +244,7 @@ async fn queue(ctx: &Context, msg: &Message) -> CommandResult {
"No songs queued".into() "No songs queued".into()
} else { } else {
let mut text = String::new(); let mut text = String::new();
text.push_str("Next songs in queue:\n");
for (i, track) in current_queue.iter().enumerate() { for (i, track) in current_queue.iter().enumerate() {
text.push_str(&song_message(Some(i), track.metadata())); text.push_str(&song_message(Some(i), track.metadata()));
text.push('\n'); text.push('\n');
@@ -258,7 +259,7 @@ async fn queue(ctx: &Context, msg: &Message) -> CommandResult {
#[command] #[command]
/// Remove song from queue, given id /// Remove song from queue, given id
async fn remove(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { async fn remove(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let call = get_handler(ctx, msg).await?; let call = get_call(ctx, msg).await?;
let handler = call.lock().await; let handler = call.lock().await;
let index = args.single()?; let index = args.single()?;
@@ -284,7 +285,7 @@ async fn remove(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
fn song_message(index: Option<usize>, metadata: &Metadata) -> String { fn song_message(index: Option<usize>, metadata: &Metadata) -> String {
let prefix = match index { let prefix = match index {
Some(i) => format!("- {}: ", i), Some(i) => format!("{}. ", i),
None => "Current: ".into(), None => "Current: ".into(),
}; };
@@ -311,17 +312,8 @@ async fn queue_song(
song: &Song, song: &Song,
client: &sunk::Client, client: &sunk::Client,
) -> Result<()> { ) -> Result<()> {
let guild = msg
.guild(&ctx.cache)
.ok_or_else(|| anyhow!("Couldn't get guild id"))?;
let manager = songbird::get(ctx)
.await
.ok_or_else(|| anyhow!("Couldn't start manager"))?;
let call = manager
.get(guild.id)
.ok_or_else(|| anyhow!("Not currently in a channel"))?;
log::info!("Queueing song {song:?}"); log::info!("Queueing song {song:?}");
let call = get_call(ctx, msg).await?;
log::info!("Will play in call {call:?}"); log::info!("Will play in call {call:?}");
let mut handler = call.lock().await; let mut handler = call.lock().await;
let input = load_song(song, client).await?; let input = load_song(song, client).await?;
@@ -348,7 +340,7 @@ async fn queue_song(
Ok(()) Ok(())
} }
async fn get_handler(ctx: &Context, msg: &Message) -> Result<Arc<Mutex<Call>>> { async fn get_call(ctx: &Context, msg: &Message) -> Result<Arc<Mutex<Call>>> {
let guild = msg let guild = msg
.guild(&ctx.cache) .guild(&ctx.cache)
.ok_or_else(|| anyhow!("Couldn't get guild id"))?; .ok_or_else(|| anyhow!("Couldn't get guild id"))?;