Skip to content

Commit

Permalink
porting more commands
Browse files Browse the repository at this point in the history
  • Loading branch information
cycle-five committed Jun 14, 2023
1 parent 85645e2 commit 945945b
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 104 deletions.
4 changes: 2 additions & 2 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub mod seek;
pub mod shuffle;
pub mod skip;
pub mod stop;
pub mod summon;
//pub mod summon;
pub mod version;
pub mod volume;
//pub mod volume;
pub mod voteskip;
5 changes: 3 additions & 2 deletions src/commands/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use self::serenity::{
Mutex,
};
use crate::{
commands::{skip::force_skip_top_track, summon::summon},
commands::skip::force_skip_top_track,
errors::{verify, ParrotError},
guild::settings::{GuildSettings, GuildSettingsMap},
handlers::track_end::update_queue_messages,
messaging::message::ParrotMessage,
messaging::messages::{
PLAY_QUEUE, PLAY_TOP, SPOTIFY_AUTH_FAILED, TRACK_DURATION, TRACK_TIME_TO_PLAY,
},
//poise_commands::summon::summon,
sources::{
spotify::{Spotify, SPOTIFY},
youtube::{YouTube, YouTubeRestartable},
Expand Down Expand Up @@ -78,7 +79,7 @@ pub async fn play(
let manager = songbird::get(ctx).await.unwrap();

// try to join a voice channel if not in one just yet
summon(ctx, interaction, false).await?;
//summon().slash_action.unwrap().await?;
let call = manager.get(guild_id).unwrap();

// determine whether this is a link or a query string
Expand Down
55 changes: 0 additions & 55 deletions src/commands/volume.rs

This file was deleted.

16 changes: 5 additions & 11 deletions src/handlers/idle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use self::serenity::{
async_trait, http::Http,
model::application::interaction::application_command::ApplicationCommandInteraction,
};
use self::serenity::{async_trait, http::Http};
use poise::serenity_prelude as serenity;
use songbird::{tracks::PlayMode, Event, EventContext, EventHandler, Songbird};
use std::sync::{
Expand All @@ -14,7 +11,8 @@ use crate::messaging::messages::IDLE_ALERT;
pub struct IdleHandler {
pub http: Arc<Http>,
pub manager: Arc<Songbird>,
pub interaction: ApplicationCommandInteraction,
pub channel_id: serenity::ChannelId,
pub guild_id: Option<serenity::GuildId>,
pub limit: usize,
pub count: Arc<AtomicUsize>,
}
Expand All @@ -39,14 +37,10 @@ impl EventHandler for IdleHandler {
}

if self.count.fetch_add(1, Ordering::Relaxed) >= self.limit {
let guild_id = self.interaction.guild_id?;
let guild_id = self.guild_id?;

if self.manager.remove(guild_id).await.is_ok() {
self.interaction
.channel_id
.say(&self.http, IDLE_ALERT)
.await
.unwrap();
self.channel_id.say(&self.http, IDLE_ALERT).await.unwrap();
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/handlers/serenity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ use crate::{
commands::{
autopause::*, clear::*, grab::*, leave::*, manage_sources::*, now_playing::*, pause::*,
play::*, queue::*, remove::*, repeat::*, resume::*, seek::*, shuffle::*, skip::*, stop::*,
summon::*, version::*, volume::*, voteskip::*,
version::*, voteskip::*,
},
connection::{check_voice_connections, Connection},
errors::ParrotError,
guild::settings::{GuildSettings, GuildSettingsMap},
handlers::track_end::update_queue_messages,
//poise_commands::volume::volume,
sources::spotify::{Spotify, SPOTIFY},
utils::create_response_text,
Error,
Expand Down Expand Up @@ -445,7 +446,10 @@ impl SerenityHandler {

match command_name {
"grab" => grab(ctx, command).await,
"volume" => volume(ctx, command).await,
"volume" => {
tracing::error!("volume not implemented here");
Ok(())
}
"autopause" => autopause(ctx, command).await,
"clear" => clear(ctx, command).await,
"leave" => leave(ctx, command).await,
Expand All @@ -461,7 +465,10 @@ impl SerenityHandler {
"shuffle" => shuffle(ctx, command).await,
"skip" => skip(ctx, command).await,
"stop" => stop(ctx, command).await,
"summon" => summon(ctx, command, true).await,
"summon" => {
tracing::error!("summon not implemented here");
Ok(())
}
"version" => version(ctx, command).await,
"voteskip" => voteskip(ctx, command).await,
_ => unreachable!(),
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ fn poise_framework() -> FrameworkBuilder<cracktunes::Data, Error> {
// FrameworkOptions contains all of poise's configuration option in one struct
// Every option can be omitted to use its default value
let options = poise::FrameworkOptions {
commands: vec![poise_commands::help(), poise_commands::chatgpt::chatgpt()],
commands: vec![
poise_commands::volume(),
poise_commands::summon(),
poise_commands::help(),
poise_commands::chatgpt(),
],
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("~".into()),
edit_tracker: Some(poise::EditTracker::for_timespan(Duration::from_secs(3600))),
Expand Down
6 changes: 5 additions & 1 deletion src/poise_commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
pub mod chatgpt;
pub mod help;
pub mod summon;
pub mod volume;

pub use help::help;
pub use self::chatgpt::*;
pub use help::*;
pub use summon::*;
pub use volume::*;
57 changes: 32 additions & 25 deletions src/commands/summon.rs → src/poise_commands/summon.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
use self::serenity::{
model::{
application::interaction::application_command::ApplicationCommandInteraction, id::ChannelId,
},
Context, Mentionable,
};
use self::serenity::{model::id::ChannelId, Mentionable};
use crate::{
connection::get_voice_channel_for_user,
errors::ParrotError,
handlers::{IdleHandler, TrackEndHandler},
messaging::message::ParrotMessage,
utils::create_response,
Error,
utils::{get_guild_id, get_user_id},
Context, Error,
};
use poise::serenity_prelude as serenity;
use songbird::{Event, TrackEvent};
use std::time::Duration;

#[poise::command(slash_command, prefix_command)]
pub async fn summon(
ctx: &Context,
interaction: &mut ApplicationCommandInteraction,
send_reply: bool,
ctx: Context<'_>,
#[description = "Channel id to join"] channel_id_str: Option<String>,
#[description = "Send a reply to the user"] send_reply: bool,
) -> Result<(), Error> {
let guild_id = interaction.guild_id.unwrap();
let guild = ctx.cache.guild(guild_id).unwrap();
let guild_id = get_guild_id(&ctx).unwrap();
let guild = ctx.serenity_context().cache.guild(guild_id).unwrap();
let manager = songbird::get(ctx.serenity_context()).await.unwrap();
let user_id = get_user_id(&ctx);

let manager = songbird::get(ctx).await.unwrap();
let channel_opt = get_voice_channel_for_user(&guild, &interaction.user.id);
let channel_id = channel_opt.unwrap();
let channel_id = match channel_id_str {
Some(id) => ChannelId(id.parse::<u64>().unwrap()),
None => match get_voice_channel_for_user(&guild, &user_id) {
Some(channel_id) => channel_id,
None => {
if send_reply {
ctx.say("You are not in a voice channel!").await?;
}
return Err(ParrotError::WrongVoiceChannel.into());
}
},
};

if let Some(call) = manager.get(guild.id) {
let handler = call.lock().await;
Expand All @@ -51,9 +58,10 @@ pub async fn summon(
handler.add_global_event(
Event::Periodic(Duration::from_secs(1), None),
IdleHandler {
http: ctx.http.clone(),
manager,
interaction: interaction.clone(),
http: ctx.serenity_context().http.clone(),
manager: manager.clone(),
channel_id,
guild_id: Some(guild_id),
limit: 60 * 10,
count: Default::default(),
},
Expand All @@ -64,20 +72,19 @@ pub async fn summon(
TrackEndHandler {
guild_id: guild.id,
call: call.clone(),
ctx_data: ctx.data.clone(),
ctx_data: ctx.serenity_context().data.clone(),
},
);
}

if send_reply {
return create_response(
&ctx.http,
interaction,
ctx.say(
ParrotMessage::Summon {
mention: channel_id.mention(),
},
}
.to_string(),
)
.await;
.await?;
}

Ok(())
Expand Down
15 changes: 13 additions & 2 deletions src/poise_commands/volume.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use self::serenity::builder::CreateEmbed;
use crate::utils::create_embed_response;
use crate::utils::{create_embed_response, create_embed_response_poise};
use crate::{Context, Error};
use ::serenity::http::CacheHttp;
use poise::{serenity_prelude as serenity, ApplicationCommandOrAutocompleteInteraction};
Expand All @@ -12,7 +12,18 @@ pub async fn volume(
#[description = "The volume to set the player to"] level: Option<u32>,
) -> Result<(), Error> {
tracing::info!("volume");
let guild_id = ctx.guild_id().unwrap();
let guild_id = match ctx.guild_id() {
Some(id) => id,
None => {
create_embed_response_poise(
&ctx,
"I need to be in a voice channel before you can do that.".to_string(),
)
.await?;
return Ok(());
}
};

let manager = songbird::get(ctx.serenity_context()).await.unwrap();
let call = manager.get(guild_id).unwrap();
let old_ctx = ctx.serenity_context();
Expand Down
Loading

0 comments on commit 945945b

Please sign in to comment.