Skip to content

Commit

Permalink
add skip command
Browse files Browse the repository at this point in the history
  • Loading branch information
mii443 committed Dec 8, 2022
1 parent f93701a commit 60770f6
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod config;
pub mod setup;
pub mod skip;
pub mod stop;
90 changes: 90 additions & 0 deletions src/commands/skip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use serenity::{
model::prelude::{
interaction::{application_command::ApplicationCommandInteraction, MessageFlags},
UserId,
},
prelude::Context,
};

use crate::data::TTSData;

pub async fn skip_command(
ctx: &Context,
command: &ApplicationCommandInteraction,
) -> Result<(), Box<dyn std::error::Error>> {
if let None = command.guild_id {
command
.create_interaction_response(&ctx.http, |f| {
f.interaction_response_data(|d| {
d.content("このコマンドはサーバーでのみ使用可能です.")
.flags(MessageFlags::EPHEMERAL)
})
})
.await?;
return Ok(());
}

let guild = command.guild_id.unwrap().to_guild_cached(&ctx.cache);
if let None = guild {
command
.create_interaction_response(&ctx.http, |f| {
f.interaction_response_data(|d| {
d.content("ギルドキャッシュを取得できませんでした.")
.flags(MessageFlags::EPHEMERAL)
})
})
.await?;
return Ok(());
}
let guild = guild.unwrap();

let channel_id = guild
.voice_states
.get(&UserId(command.user.id.0))
.and_then(|state| state.channel_id);

if let None = channel_id {
command
.create_interaction_response(&ctx.http, |f| {
f.interaction_response_data(|d| {
d.content("ボイスチャンネルに参加してから実行してください.")
.flags(MessageFlags::EPHEMERAL)
})
})
.await?;
return Ok(());
}

let storage_lock = {
let data_read = ctx.data.read().await;
data_read
.get::<TTSData>()
.expect("Cannot get TTSStorage")
.clone()
};

{
let mut storage = storage_lock.write().await;
if !storage.contains_key(&guild.id) {
command
.create_interaction_response(&ctx.http, |f| {
f.interaction_response_data(|d| {
d.content("読み上げしていません")
.flags(MessageFlags::EPHEMERAL)
})
})
.await?;
return Ok(());
}

storage.get_mut(&guild.id).unwrap().skip(&ctx).await;
}

command
.create_interaction_response(&ctx.http, |f| {
f.interaction_response_data(|d| d.content("スキップしました"))
})
.await?;

Ok(())
}
7 changes: 5 additions & 2 deletions src/event_handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{
commands::{config::config_command, setup::setup_command, stop::stop_command},
commands::{
config::config_command, setup::setup_command, skip::skip_command, stop::stop_command,
},
data::DatabaseClientData,
database::dictionary::Rule,
events,
Expand All @@ -12,7 +14,7 @@ use serenity::{
channel::Message,
gateway::Ready,
prelude::{
component::{ActionRowComponent, ButtonStyle, InputText, InputTextStyle},
component::{ActionRowComponent, ButtonStyle, InputTextStyle},
interaction::{Interaction, InteractionResponseType, MessageFlags},
},
voice::VoiceState,
Expand All @@ -38,6 +40,7 @@ impl EventHandler for Handler {
"setup" => setup_command(&ctx, &command).await.unwrap(),
"stop" => stop_command(&ctx, &command).await.unwrap(),
"config" => config_command(&ctx, &command).await.unwrap(),
"skip" => skip_command(&ctx, &command).await.unwrap(),
_ => {}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/events/ready.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub async fn ready(ctx: Context, ready: Ready) {
})
})
.create_application_command(|command| command.name("config").description("Config"))
.create_application_command(|command| {
command.name("skip").description("skip tts message")
})
})
.await;
}
8 changes: 8 additions & 0 deletions src/tts/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ impl TTSInstance {
call.enqueue_source(input);
}
}

pub async fn skip(&mut self, ctx: &Context) {
let manager = songbird::get(&ctx).await.unwrap();
let call = manager.get(self.guild).unwrap();
let call = call.lock().await;
let queue = call.queue();
let _ = queue.skip();
}
}

0 comments on commit 60770f6

Please sign in to comment.