Add macro install command stub

This commit is contained in:
jude 2022-09-12 17:20:43 +01:00
parent 8f8235a86e
commit e2bf23f194
6 changed files with 69 additions and 22 deletions

View File

@ -0,0 +1,38 @@
use poise::serenity_prelude::CommandType;
use crate::{
commands::autocomplete::macro_name_autocomplete, models::command_macro::guild_command_macro,
Context, Error,
};
/// Add a macro as a slash-command to this server. Enables controlling permissions per-macro.
#[poise::command(
slash_command,
rename = "install",
guild_only = true,
default_member_permissions = "MANAGE_GUILD",
identifying_name = "install_macro"
)]
pub async fn install_macro(
ctx: Context<'_>,
#[description = "Name of macro to install"]
#[autocomplete = "macro_name_autocomplete"]
name: String,
) -> Result<(), Error> {
let guild_id = ctx.guild_id().unwrap();
if let Some(command_macro) = guild_command_macro(&ctx, &name).await {
guild_id
.create_application_command(&ctx.discord(), |a| {
a.kind(CommandType::ChatInput)
.name(command_macro.name)
.description(command_macro.description.unwrap_or_else(|| "".to_string()))
})
.await?;
ctx.send(|r| r.ephemeral(true).content("Macro installed. Go to Server Settings 🠚 Integrations 🠚 Reminder Bot to configure permissions.")).await?;
} else {
ctx.send(|r| r.ephemeral(true).content("No macro found with that name")).await?;
}
Ok(())
}

View File

@ -1,6 +1,7 @@
use crate::{Context, Error};
pub mod delete;
pub mod install;
pub mod list;
pub mod migrate;
pub mod record;

View File

@ -1,4 +1,4 @@
mod autocomplete;
pub mod autocomplete;
pub mod command_macro;
pub mod info_cmds;
pub mod moderation_cmds;

View File

@ -1,36 +1,42 @@
use poise::serenity_prelude::model::channel::Channel;
use poise::{
serenity_prelude::model::channel::Channel, ApplicationCommandOrAutocompleteInteraction,
};
use crate::{consts::MACRO_MAX_COMMANDS, models::command_macro::RecordedCommand, Context, Error};
async fn macro_check(ctx: Context<'_>) -> bool {
async fn recording_macro_check(ctx: Context<'_>) -> bool {
if let Context::Application(app_ctx) = ctx {
if let Some(guild_id) = ctx.guild_id() {
if ctx.command().identifying_name != "finish_macro" {
let mut lock = ctx.data().recording_macros.write().await;
if let ApplicationCommandOrAutocompleteInteraction::ApplicationCommand(_) =
app_ctx.interaction
{
if let Some(guild_id) = ctx.guild_id() {
if ctx.command().identifying_name != "finish_macro" {
let mut lock = ctx.data().recording_macros.write().await;
if let Some(command_macro) = lock.get_mut(&(guild_id, ctx.author().id)) {
if command_macro.commands.len() >= MACRO_MAX_COMMANDS {
let _ = ctx.send(|m| {
if let Some(command_macro) = lock.get_mut(&(guild_id, ctx.author().id)) {
if command_macro.commands.len() >= MACRO_MAX_COMMANDS {
let _ = ctx.send(|m| {
m.ephemeral(true).content(
format!("{} commands already recorded. Please use `/macro finish` to end recording.", MACRO_MAX_COMMANDS),
)
})
.await;
} else {
let recorded = RecordedCommand {
action: None,
command_name: ctx.command().identifying_name.clone(),
options: Vec::from(app_ctx.args),
};
} else {
let recorded = RecordedCommand {
action: None,
command_name: ctx.command().identifying_name.clone(),
options: Vec::from(app_ctx.args),
};
command_macro.commands.push(recorded);
command_macro.commands.push(recorded);
let _ = ctx
.send(|m| m.ephemeral(true).content("Command recorded to macro"))
.await;
let _ = ctx
.send(|m| m.ephemeral(true).content("Command recorded to macro"))
.await;
}
return false;
}
return false;
}
}
}
@ -89,5 +95,5 @@ async fn check_self_permissions(ctx: Context<'_>) -> bool {
}
pub async fn all_checks(ctx: Context<'_>) -> Result<bool, Error> {
Ok(macro_check(ctx).await && check_self_permissions(ctx).await)
Ok(recording_macro_check(ctx).await && check_self_permissions(ctx).await)
}

View File

@ -117,6 +117,7 @@ async fn _main(tx: Sender<()>) -> Result<(), Box<dyn StdError + Send + Sync>> {
command_macro::record::record_macro(),
command_macro::run::run_macro(),
command_macro::migrate::migrate_macro(),
command_macro::install::install_macro(),
],
..command_macro::macro_base()
},

View File

@ -37,6 +37,7 @@ pub struct RawCommandMacro {
pub commands: Value,
}
/// Get a macro by name form a guild.
pub async fn guild_command_macro(
ctx: &Context<'_>,
name: &str,