use interactions instead of chat commands
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-02-17 14:34:55 +01:00
parent abba563d23
commit 872464bd31
11 changed files with 369 additions and 136 deletions

View File

@@ -1,43 +1,84 @@
use std::{env, error::Error, num::NonZeroU64, time::Duration};
use tokio::time::sleep;
use tracing::info;
use twilight_model::{channel::Message, id::Id};
use tracing::{debug, info};
use twilight_model::{
application::interaction::Interaction,
channel::message::MessageFlags,
http::interaction::{InteractionResponse, InteractionResponseType},
id::Id,
};
use twilight_util::builder::InteractionResponseDataBuilder;
use crate::state::State;
pub(crate) async fn delete(
msg: Message,
interaction: Interaction,
state: State,
count: i64,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
debug!(
"delete command in guild {:?} in channel {:?} by {:?}",
interaction.guild_id,
interaction.channel,
interaction.author(),
);
let admin = env::var("ADMIN")?.parse::<u64>()?;
if msg.author.id != Id::from(NonZeroU64::new(admin).unwrap()) {
if interaction.author_id() != Some(Id::from(NonZeroU64::new(admin).unwrap())) {
let interaction_response_data = InteractionResponseDataBuilder::new()
.content("You do not have permissions to delete messages.")
.flags(MessageFlags::EPHEMERAL)
.build();
let response = InteractionResponse {
kind: InteractionResponseType::ChannelMessageWithSource,
data: Some(interaction_response_data),
};
state
.http
.interaction(interaction.application_id)
.create_response(interaction.id, &interaction.token, &response)
.await?;
return Ok(());
}
let n = msg
.content
.split(' ')
.last()
.unwrap()
.parse::<u16>()
.unwrap_or(1);
if n > 100 {
let Some(channel) = interaction.channel else {
return Ok(());
}
};
let Some(message_id) = channel.last_message_id else {
return Ok(());
};
let count = count.max(1).min(100) as u16;
let interaction_response_data = InteractionResponseDataBuilder::new()
.content(format!("Deleting {count} messages."))
.flags(MessageFlags::EPHEMERAL)
.build();
let response = InteractionResponse {
kind: InteractionResponseType::ChannelMessageWithSource,
data: Some(interaction_response_data),
};
state
.http
.interaction(interaction.application_id)
.create_response(interaction.id, &interaction.token, &response)
.await?;
let messages = state
.http
.channel_messages(msg.channel_id)
.before(msg.id)
.limit(n)?
.channel_messages(channel.id)
.before(message_id.cast())
.limit(count)?
.await?
.model()
.await?;
state.http.delete_message(msg.channel_id, msg.id).await?;
for message in messages {
info!("Delete message: {:?}: {:?}", message.author.name, message);
state
.http
.delete_message(msg.channel_id, message.id)
.await?;
debug!("Delete message: {:?}: {:?}", message.author.name, message);
state.http.delete_message(channel.id, message.id).await?;
sleep(Duration::from_secs(5)).await;
}
Ok(())

View File

@@ -1,34 +1,81 @@
use std::{error::Error, num::NonZeroU64};
use twilight_model::channel::Message;
use crate::state::State;
use std::error::Error;
use tracing::debug;
use twilight_model::{
application::interaction::Interaction,
channel::message::MessageFlags,
http::interaction::{InteractionResponse, InteractionResponseType},
id::{
marker::{GuildMarker, UserMarker},
Id,
},
};
use twilight_util::builder::InteractionResponseDataBuilder;
pub(crate) async fn join(
msg: Message,
pub(crate) async fn join_channel(
state: State,
guild_id: Id<GuildMarker>,
user_id: Id<UserMarker>,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let guild_id = msg.guild_id.ok_or("No guild id attached to the message.")?;
let user_id = msg.author.id;
debug!("join user {:?} in guild {:?}", user_id, guild_id);
let channel_id = state
.cache
.voice_state(user_id, guild_id)
.ok_or("Cannot get voice state for user")?
.channel_id();
let channel_id =
NonZeroU64::new(channel_id.into()).ok_or("Joined voice channel must have nonzero ID.")?;
// join the voice channel
state
.songbird
.join(guild_id, channel_id)
.join(guild_id.cast(), channel_id)
.await
.map_err(|e| format!("Could not join voice channel: {:?}", e))?;
// signal that we are not listening
if let Some(call_lock) = state.songbird.get(guild_id) {
if let Some(call_lock) = state.songbird.get(guild_id.cast()) {
let mut call = call_lock.lock().await;
call.deafen(true).await?;
}
Ok(())
}
pub(crate) async fn join(
interaction: Interaction,
state: State,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
debug!(
"join command in guild {:?} in channel {:?} by {:?}",
interaction.guild_id,
interaction.channel,
interaction.author(),
);
let Some(guild_id) = interaction.guild_id else {
return Ok(());
};
let Some(author_id) = interaction.author_id() else {
return Ok(());
};
join_channel(state.clone(), guild_id, author_id).await?;
let interaction_response_data = InteractionResponseDataBuilder::new()
.content("Bin da Brudi!")
.flags(MessageFlags::EPHEMERAL)
.build();
let response = InteractionResponse {
kind: InteractionResponseType::ChannelMessageWithSource,
data: Some(interaction_response_data),
};
state
.http
.interaction(interaction.application_id)
.create_response(interaction.id, &interaction.token, &response)
.await?;
Ok(())
}

View File

@@ -1,17 +1,21 @@
use crate::state::State;
use std::error::Error;
use twilight_model::channel::Message;
use twilight_model::application::interaction::Interaction;
pub(crate) async fn leave(
msg: Message,
interaction: Interaction,
state: State,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
tracing::debug!(
"leave command in channel {} by {}",
msg.channel_id,
msg.author.name
"leave command n guild {:?} in channel {:?} by {:?}",
interaction.guild_id,
interaction.channel,
interaction.author(),
);
let guild_id = msg.guild_id.unwrap();
let Some(guild_id) = interaction.guild_id else {
return Ok(());
};
state.songbird.leave(guild_id).await?;
Ok(())
}

View File

@@ -23,7 +23,7 @@ mod delete;
pub(crate) use delete::delete;
use twilight_model::application::command::CommandType;
use twilight_util::builder::command::{CommandBuilder, StringBuilder};
use twilight_util::builder::command::{CommandBuilder, IntegerBuilder, StringBuilder};
pub(crate) fn get_chat_commands() -> Vec<twilight_model::application::command::Command> {
vec![
@@ -36,5 +36,8 @@ pub(crate) fn get_chat_commands() -> Vec<twilight_model::application::command::C
CommandBuilder::new("queue", "Print track queue", CommandType::ChatInput).build(),
CommandBuilder::new("resume", "Resume playing", CommandType::ChatInput).build(),
CommandBuilder::new("stop", "Stop playing", CommandType::ChatInput).build(),
CommandBuilder::new("delete", "Delete messages in chat", CommandType::ChatInput)
.option(IntegerBuilder::new("count", "How many messages to delete").required(true))
.build(),
]
}

View File

@@ -1,28 +1,46 @@
use crate::state::State;
use std::error::Error;
use twilight_model::channel::Message;
use twilight_model::{
application::interaction::Interaction,
channel::message::MessageFlags,
http::interaction::{InteractionResponse, InteractionResponseType},
};
use twilight_util::builder::InteractionResponseDataBuilder;
pub(crate) async fn pause(
msg: Message,
interaction: Interaction,
state: State,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
tracing::debug!(
"pause command in channel {} by {}",
msg.channel_id,
msg.author.name
"pause command in guild {:?} in channel {:?} by {:?}",
interaction.guild_id,
interaction.channel,
interaction.author(),
);
let guild_id = msg.guild_id.unwrap();
let Some(guild_id) = interaction.guild_id else {
return Ok(());
};
if let Some(call_lock) = state.songbird.get(guild_id) {
let call = call_lock.lock().await;
call.queue().pause()?;
}
let interaction_response_data = InteractionResponseDataBuilder::new()
.content("Paused the track")
.flags(MessageFlags::EPHEMERAL)
.build();
let response = InteractionResponse {
kind: InteractionResponseType::ChannelMessageWithSource,
data: Some(interaction_response_data),
};
state
.http
.create_message(msg.channel_id)
.content("Paused the track")?
.interaction(interaction.application_id)
.create_response(interaction.id, &interaction.token, &response)
.await?;
Ok(())

View File

@@ -1,4 +1,4 @@
use crate::commands::join;
use crate::commands::join::join_channel;
use crate::metadata::{Metadata, MetadataMap};
use crate::state::State;
use serde_json::Value;
@@ -7,23 +7,31 @@ use std::io::{BufRead, BufReader};
use std::{error::Error, ops::Sub, time::Duration};
use tokio::process::Command;
use tracing::debug;
use twilight_model::channel::Message;
use twilight_model::application::interaction::Interaction;
use twilight_model::channel::message::MessageFlags;
use twilight_model::http::interaction::{InteractionResponse, InteractionResponseType};
use twilight_util::builder::InteractionResponseDataBuilder;
use url::Url;
pub(crate) async fn play(
msg: Message,
interaction: Interaction,
state: State,
query: String,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
tracing::debug!(
"play command in channel {} by {}",
msg.channel_id,
msg.author.name
debug!(
"play command in channel {:?} by {:?}",
interaction.channel,
interaction.author(),
);
join(msg.clone(), state.clone()).await?;
let Some(user_id) = interaction.author_id() else {
return Ok(());
};
let Some(guild_id) = interaction.guild_id else {
return Ok(());
};
let guild_id = msg.guild_id.unwrap();
join_channel(state.clone(), guild_id, user_id).await?;
// handle keyword queries
let query = if Url::parse(&query).is_err() {
@@ -32,6 +40,24 @@ pub(crate) async fn play(
query
};
debug!("query: {:?}", query);
let interaction_response_data = InteractionResponseDataBuilder::new()
.content("Adding tracks to the queue ...")
.flags(MessageFlags::EPHEMERAL)
.build();
let response = InteractionResponse {
kind: InteractionResponseType::ChannelMessageWithSource,
data: Some(interaction_response_data),
};
state
.http
.interaction(interaction.application_id)
.create_response(interaction.id, &interaction.token, &response)
.await?;
// handle playlist links
let urls = if query.contains("list=") {
get_playlist_urls(query).await?
@@ -62,12 +88,6 @@ pub(crate) async fn play(
duration: metadata.duration,
});
}
} else {
state
.http
.create_message(msg.channel_id)
.content("Cannot find any results")?
.await?;
}
}

View File

@@ -1,18 +1,26 @@
use twilight_model::http::interaction::InteractionResponseType;
use twilight_model::{
application::interaction::Interaction, channel::message::MessageFlags,
http::interaction::InteractionResponse,
};
use twilight_util::builder::InteractionResponseDataBuilder;
use crate::{metadata::MetadataMap, state::State};
use std::error::Error;
use twilight_model::channel::Message;
pub(crate) async fn queue(
msg: Message,
interaction: Interaction,
state: State,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
tracing::debug!(
"queue command in channel {} by {}",
msg.channel_id,
msg.author.name
"queue command in guild {:?} in channel {:?} by {:?}",
interaction.guild_id,
interaction.channel,
interaction.author(),
);
let guild_id = msg.guild_id.unwrap();
let Some(guild_id) = interaction.guild_id else {
return Ok(());
};
if let Some(call_lock) = state.songbird.get(guild_id) {
let call = call_lock.lock().await;
let queue = call.queue().current_queue();
@@ -48,10 +56,21 @@ pub(crate) async fn queue(
}
message.push_str("`\n");
}
let interaction_response_data = InteractionResponseDataBuilder::new()
.content(&message)
.flags(MessageFlags::EPHEMERAL)
.build();
let response = InteractionResponse {
kind: InteractionResponseType::ChannelMessageWithSource,
data: Some(interaction_response_data),
};
state
.http
.create_message(msg.channel_id)
.content(&message)?
.interaction(interaction.application_id)
.create_response(interaction.id, &interaction.token, &response)
.await?;
}
Ok(())

View File

@@ -1,28 +1,45 @@
use crate::state::State;
use std::error::Error;
use twilight_model::channel::Message;
use twilight_model::{
application::interaction::Interaction,
channel::message::MessageFlags,
http::interaction::{InteractionResponse, InteractionResponseType},
};
use twilight_util::builder::InteractionResponseDataBuilder;
pub(crate) async fn resume(
msg: Message,
interaction: Interaction,
state: State,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
tracing::debug!(
"resume command in channel {} by {}",
msg.channel_id,
msg.author.name
"resume command in guild {:?} in channel {:?} by {:?}",
interaction.guild_id,
interaction.channel,
interaction.author(),
);
let guild_id = msg.guild_id.unwrap();
let Some(guild_id) = interaction.guild_id else {
return Ok(());
};
if let Some(call_lock) = state.songbird.get(guild_id) {
let call = call_lock.lock().await;
call.queue().resume()?;
}
let interaction_response_data = InteractionResponseDataBuilder::new()
.content("Resumed the track")
.flags(MessageFlags::EPHEMERAL)
.build();
let response = InteractionResponse {
kind: InteractionResponseType::ChannelMessageWithSource,
data: Some(interaction_response_data),
};
state
.http
.create_message(msg.channel_id)
.content("Resumed the track")?
.interaction(interaction.application_id)
.create_response(interaction.id, &interaction.token, &response)
.await?;
Ok(())

View File

@@ -1,28 +1,47 @@
use twilight_model::{
application::interaction::Interaction,
channel::message::MessageFlags,
http::interaction::{InteractionResponse, InteractionResponseType},
};
use twilight_util::builder::InteractionResponseDataBuilder;
use crate::state::State;
use std::error::Error;
use twilight_model::channel::Message;
pub(crate) async fn stop(
msg: Message,
interaction: Interaction,
state: State,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
tracing::debug!(
"stop command in channel {} by {}",
msg.channel_id,
msg.author.name
"stop command in guild {:?} in channel {:?} by {:?}",
interaction.guild_id,
interaction.channel,
interaction.author(),
);
let guild_id = msg.guild_id.unwrap();
let Some(guild_id) = interaction.guild_id else {
return Ok(());
};
if let Some(call_lock) = state.songbird.get(guild_id) {
let call = call_lock.lock().await;
call.queue().stop();
}
let interaction_response_data = InteractionResponseDataBuilder::new()
.content("Stopped the track and cleared the queue")
.flags(MessageFlags::EPHEMERAL)
.build();
let response = InteractionResponse {
kind: InteractionResponseType::ChannelMessageWithSource,
data: Some(interaction_response_data),
};
state
.http
.create_message(msg.channel_id)
.content("Stopped the track and cleared the queue")?
.interaction(interaction.application_id)
.create_response(interaction.id, &interaction.token, &response)
.await?;
Ok(())