start of soundboard and help rework

This commit is contained in:
jellywx 2021-06-24 15:52:45 +01:00
parent 1aed25a74c
commit f0590328b0
4 changed files with 530 additions and 312 deletions

596
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -5,8 +5,8 @@ authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
songbird = { git = "https://github.com/serenity-rs/songbird", branch = "next" } songbird = { path = "/home/jude/songbird" }
serenity = { git = "https://github.com/serenity-rs/serenity", branch = "next", features = ["voice", "collector", "unstable_discord_api"] } serenity = { path = "/home/jude/serenity", features = ["voice", "collector", "unstable_discord_api"] }
sqlx = { version = "0.5", default-features = false, features = ["runtime-tokio-rustls", "macros", "mysql", "bigdecimal"] } sqlx = { version = "0.5", default-features = false, features = ["runtime-tokio-rustls", "macros", "mysql", "bigdecimal"] }
dotenv = "0.15" dotenv = "0.15"
tokio = { version = "1", features = ["fs", "process", "io-util"] } tokio = { version = "1", features = ["fs", "process", "io-util"] }

View File

@ -10,7 +10,7 @@ use serenity::{
channel::{Channel, GuildChannel, Message}, channel::{Channel, GuildChannel, Message},
guild::{Guild, Member}, guild::{Guild, Member},
id::{ChannelId, GuildId, UserId}, id::{ChannelId, GuildId, UserId},
interactions::{ApplicationCommand, Interaction, InteractionType}, interactions::{ApplicationCommand, Interaction, InteractionData, InteractionType},
prelude::{ApplicationCommandOptionType, InteractionResponseType}, prelude::{ApplicationCommandOptionType, InteractionResponseType},
}, },
prelude::TypeMapKey, prelude::TypeMapKey,
@ -249,7 +249,7 @@ impl CommandInvoke for Interaction {
d.content(generic_response.content); d.content(generic_response.content);
if let Some(embed) = generic_response.embed { if let Some(embed) = generic_response.embed {
d.set_embed(embed.clone()); d.add_embed(embed.clone());
} }
d d
@ -268,7 +268,7 @@ impl CommandInvoke for Interaction {
d.content(generic_response.content); d.content(generic_response.content);
if let Some(embed) = generic_response.embed { if let Some(embed) = generic_response.embed {
d.set_embed(embed.clone()); d.add_embed(embed.clone());
} }
d d
@ -408,7 +408,7 @@ impl fmt::Debug for Command {
} }
pub struct RegexFramework { pub struct RegexFramework {
commands: HashMap<String, &'static Command>, pub commands: HashMap<String, &'static Command>,
commands_: HashSet<&'static Command>, commands_: HashSet<&'static Command>,
command_matcher: Regex, command_matcher: Regex,
default_prefix: String, default_prefix: String,
@ -615,7 +615,7 @@ impl RegexFramework {
pub async fn execute(&self, ctx: Context, interaction: Interaction) { pub async fn execute(&self, ctx: Context, interaction: Interaction) {
if interaction.kind == InteractionType::ApplicationCommand && interaction.guild_id.is_some() if interaction.kind == InteractionType::ApplicationCommand && interaction.guild_id.is_some()
{ {
if let Some(data) = interaction.data.clone() { if let Some(InteractionData::ApplicationCommand(data)) = interaction.data.clone() {
let command = { let command = {
let name = data.name; let name = data.name;

View File

@ -19,12 +19,14 @@ use log::info;
use regex_command_attr::command; use regex_command_attr::command;
use serenity::{ use serenity::{
builder::CreateActionRow,
client::{bridge::gateway::GatewayIntents, Client, Context}, client::{bridge::gateway::GatewayIntents, Client, Context},
framework::standard::CommandResult, framework::standard::CommandResult,
http::Http, http::Http,
model::{ model::{
guild::Guild, guild::Guild,
id::{ChannelId, GuildId, RoleId, UserId}, id::{ChannelId, GuildId, RoleId, UserId},
interactions::ButtonStyle,
}, },
prelude::*, prelude::*,
}; };
@ -46,6 +48,7 @@ use dashmap::DashMap;
use std::{collections::HashMap, convert::TryFrom, env, sync::Arc, time::Duration}; use std::{collections::HashMap, convert::TryFrom, env, sync::Arc, time::Duration};
use serenity::model::prelude::InteractionResponseType;
use tokio::sync::{MutexGuard, RwLock}; use tokio::sync::{MutexGuard, RwLock};
struct MySQL; struct MySQL;
@ -193,6 +196,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.add_command(&PLAY_COMMAND) .add_command(&PLAY_COMMAND)
.add_command(&STOP_PLAYING_COMMAND) .add_command(&STOP_PLAYING_COMMAND)
.add_command(&DISCONNECT_COMMAND) .add_command(&DISCONNECT_COMMAND)
.add_command(&SOUNDBOARD_COMMAND)
// sound management commands // sound management commands
.add_command(&UPLOAD_NEW_SOUND_COMMAND) .add_command(&UPLOAD_NEW_SOUND_COMMAND)
.add_command(&DELETE_SOUND_COMMAND) .add_command(&DELETE_SOUND_COMMAND)
@ -298,8 +302,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
#[command] #[command]
#[description("Get information on the commands of the bot")] #[description("Get information on the commands of the bot")]
#[arg( #[arg(
name = "category", name = "command",
description = "Get help for a specific category", description = "Get help for a specific command",
kind = "String", kind = "String",
required = false required = false
)] )]
@ -308,125 +312,82 @@ async fn help(
invoke: &(dyn CommandInvoke + Sync + Send), invoke: &(dyn CommandInvoke + Sync + Send),
args: Args, args: Args,
) -> CommandResult { ) -> CommandResult {
if let Some(category) = args.named("category") { if let Some(command_name) = args.named("command") {
let body = match category.to_lowercase().as_str() { let framework = ctx
"info" => { .data
"__Info Commands__ .read()
`help` - view all commands .await
`help [category]` - view help for the commands in a category .get::<RegexFramework>()
.cloned()
`info` - view information about the bot .unwrap();
`invite` - get an invite link for the bot
`donate` - view information about the Patreon
"
}
"play" => {
"__Play Commands__
`play [sound]` - play a sound matching the name \"sound\"
`play [id]` - play the sound with numerical ID `id`
`p` - an alias for `play`
`stop` - stop the bot from playing
`dc` - disconnect the bot from the current channel
`loop [sound]` - play a sound matching the name \"sound\" on loop
`loop [id]` - play a sound matching the numerical ID `id` on loop
"
}
"manage" => {
"__Manage Commands__
`upload [name]` - upload a new sound effect to the name \"name\"
`delete [name]` - delete a sound you have uploaded under the name \"name\"
`list` - list sounds uploaded on the server you are on
`list me` - list sounds you have uploaded to any server
`public [name]` - make a sound you have uploaded public or private
"
}
"settings" => {
"__Settings Commands__
`prefix [new prefix]` - change the prefix of the bot
`roles [role list]` - set which roles can use the bot
`roles off` - allow all users to use the bot
`volume [new volume]` - change the volume of the bot
`allow_greet` - toggle whether users in your server can use greet sounds
"
}
"search" => {
"__Search Commands__
`search [term]` - search for sounds matching \"term\"
`random` - find some random sounds on the bot
`popular` - find the most played sounds on the bot
"
}
"other" => {
"__Other Commands__
`greet [name]` - set your greet sound (join sound) to the sound called \"name\"
`greet [id]` - set your greet sound (join sound) to the sound with numerical ID `id`
`ambience` - view a list of ambience sounds
`ambience [name]` - set an ambience sound playing
"
}
_ => {
"__Unrecognised Category__
Please select a category from the following:
`info`
`play`
`manage`
`settings`
`search`
`other`
"
}
};
invoke
.respond(
ctx.http.clone(),
CreateGenericResponse::new()
.embed(|e| e.title("Help").color(THEME_COLOR).description(body)),
)
.await?;
} else {
let description = {
let guild_data = ctx.guild_data(invoke.guild_id().unwrap()).await.unwrap();
let read_lock = guild_data.read().await;
format!(
"Type `{}help category` to view help for a command category below:",
read_lock.prefix
)
};
if let Some(command) = framework.commands.get(command_name) {
invoke invoke
.respond( .respond(
ctx.http.clone(), ctx.http.clone(),
CreateGenericResponse::new().embed(|e| { CreateGenericResponse::new().embed(|e| {
e.title("Help") e.title(format!("{} Help", command_name))
.color(THEME_COLOR) .color(THEME_COLOR)
.description(description) .description(format!(
.field("Info", "`help` `info` `invite` `donate`", false) "**Aliases**
.field("Play", "`play` `p` `stop` `dc` `loop`", false) {}
.field("Manage", "`upload` `delete` `list` `public`", false)
.field("Settings", "`prefix` `roles` `volume` `allow_greet`", false) **Overview**
.field("Search", "`search` `random` `popular`", false) {}
.field("Other", "`greet` `ambience`", false)
**Arguments**
{}
**Examples**
{}",
command
.names
.iter()
.map(|n| format!("`{}`", n))
.collect::<Vec<String>>()
.join(" "),
command.desc,
command
.args
.iter()
.map(|a| format!(
" • `{}` {} - {}",
a.name,
if a.required { "" } else { "[optional]" },
a.description
))
.collect::<Vec<String>>()
.join("\n"),
command
.examples
.iter()
.map(|e| format!("{}", e))
.collect::<Vec<String>>()
.join("\n")
))
}), }),
) )
.await?; .await?;
} else {
invoke
.respond(
ctx.http.clone(),
CreateGenericResponse::new().embed(|e| {
e.title("Invalid Command")
.color(THEME_COLOR)
.description("")
}),
)
.await?;
}
} else {
invoke
.respond(
ctx.http.clone(),
CreateGenericResponse::new()
.embed(|e| e.title("Help").color(THEME_COLOR).description("")),
)
.await?;
} }
Ok(()) Ok(())
@ -499,8 +460,6 @@ async fn play_cmd(ctx: &Context, guild: Guild, user_id: UserId, args: Args, loop
Some(user_channel) => { Some(user_channel) => {
let search_term = args.named("query").unwrap(); let search_term = args.named("query").unwrap();
println!("{}", search_term);
let pool = ctx let pool = ctx
.data .data
.read() .read()
@ -1177,6 +1136,53 @@ async fn list_sounds(
Ok(()) Ok(())
} }
#[command("soundboard")]
#[aliases("board")]
#[description("Get a menu of sounds in this server with buttons to play them")]
async fn soundboard(
ctx: &Context,
invoke: &(dyn CommandInvoke + Sync + Send),
_args: Args,
) -> CommandResult {
let pool = ctx
.data
.read()
.await
.get::<MySQL>()
.cloned()
.expect("Could not get SQLPool from data");
let sounds = Sound::get_guild_sounds(invoke.guild_id().unwrap(), pool).await?;
if let Some(interaction) = invoke.interaction() {
interaction
.create_interaction_response(&ctx, |r| r.kind(InteractionResponseType::Pong))
.await?;
}
invoke
.channel_id()
.send_message(&ctx, |m| {
m.components(|c| {
for row in sounds.as_slice().chunks(5) {
let mut action_row: CreateActionRow = Default::default();
for sound in row {
action_row.create_button(|b| {
b.style(ButtonStyle::Primary)
.label(&sound.name)
.custom_id(sound.id)
});
}
}
c
})
})
.await?;
Ok(())
}
#[command("public")] #[command("public")]
#[description("Change a sound between public and private")] #[description("Change a sound between public and private")]
#[arg( #[arg(