Compare commits
	
		
			1 Commits
		
	
	
		
			jellywx/gu
			...
			jellywx/ma
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| e2bf23f194 | 
							
								
								
									
										38
									
								
								src/commands/command_macro/install.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								src/commands/command_macro/install.rs
									
									
									
									
									
										Normal 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(())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
use crate::{Context, Error};
 | 
					use crate::{Context, Error};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub mod delete;
 | 
					pub mod delete;
 | 
				
			||||||
 | 
					pub mod install;
 | 
				
			||||||
pub mod list;
 | 
					pub mod list;
 | 
				
			||||||
pub mod migrate;
 | 
					pub mod migrate;
 | 
				
			||||||
pub mod record;
 | 
					pub mod record;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,4 @@
 | 
				
			|||||||
mod autocomplete;
 | 
					pub mod autocomplete;
 | 
				
			||||||
pub mod command_macro;
 | 
					pub mod command_macro;
 | 
				
			||||||
pub mod info_cmds;
 | 
					pub mod info_cmds;
 | 
				
			||||||
pub mod moderation_cmds;
 | 
					pub mod moderation_cmds;
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										48
									
								
								src/hooks.rs
									
									
									
									
									
								
							
							
						
						
									
										48
									
								
								src/hooks.rs
									
									
									
									
									
								
							@@ -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};
 | 
					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 Context::Application(app_ctx) = ctx {
 | 
				
			||||||
        if let Some(guild_id) = ctx.guild_id() {
 | 
					        if let ApplicationCommandOrAutocompleteInteraction::ApplicationCommand(_) =
 | 
				
			||||||
            if ctx.command().identifying_name != "finish_macro" {
 | 
					            app_ctx.interaction
 | 
				
			||||||
                let mut lock = ctx.data().recording_macros.write().await;
 | 
					        {
 | 
				
			||||||
 | 
					            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 let Some(command_macro) = lock.get_mut(&(guild_id, ctx.author().id)) {
 | 
				
			||||||
                    if command_macro.commands.len() >= MACRO_MAX_COMMANDS {
 | 
					                        if command_macro.commands.len() >= MACRO_MAX_COMMANDS {
 | 
				
			||||||
                        let _ = ctx.send(|m| {
 | 
					                            let _ = ctx.send(|m| {
 | 
				
			||||||
                            m.ephemeral(true).content(
 | 
					                            m.ephemeral(true).content(
 | 
				
			||||||
                                format!("{} commands already recorded. Please use `/macro finish` to end recording.", MACRO_MAX_COMMANDS),
 | 
					                                format!("{} commands already recorded. Please use `/macro finish` to end recording.", MACRO_MAX_COMMANDS),
 | 
				
			||||||
                            )
 | 
					                            )
 | 
				
			||||||
                        })
 | 
					                        })
 | 
				
			||||||
                            .await;
 | 
					                            .await;
 | 
				
			||||||
                    } else {
 | 
					                        } else {
 | 
				
			||||||
                        let recorded = RecordedCommand {
 | 
					                            let recorded = RecordedCommand {
 | 
				
			||||||
                            action: None,
 | 
					                                action: None,
 | 
				
			||||||
                            command_name: ctx.command().identifying_name.clone(),
 | 
					                                command_name: ctx.command().identifying_name.clone(),
 | 
				
			||||||
                            options: Vec::from(app_ctx.args),
 | 
					                                options: Vec::from(app_ctx.args),
 | 
				
			||||||
                        };
 | 
					                            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        command_macro.commands.push(recorded);
 | 
					                            command_macro.commands.push(recorded);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        let _ = ctx
 | 
					                            let _ = ctx
 | 
				
			||||||
                            .send(|m| m.ephemeral(true).content("Command recorded to macro"))
 | 
					                                .send(|m| m.ephemeral(true).content("Command recorded to macro"))
 | 
				
			||||||
                            .await;
 | 
					                                .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> {
 | 
					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)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -117,6 +117,7 @@ async fn _main(tx: Sender<()>) -> Result<(), Box<dyn StdError + Send + Sync>> {
 | 
				
			|||||||
                    command_macro::record::record_macro(),
 | 
					                    command_macro::record::record_macro(),
 | 
				
			||||||
                    command_macro::run::run_macro(),
 | 
					                    command_macro::run::run_macro(),
 | 
				
			||||||
                    command_macro::migrate::migrate_macro(),
 | 
					                    command_macro::migrate::migrate_macro(),
 | 
				
			||||||
 | 
					                    command_macro::install::install_macro(),
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
                ..command_macro::macro_base()
 | 
					                ..command_macro::macro_base()
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -37,6 +37,7 @@ pub struct RawCommandMacro {
 | 
				
			|||||||
    pub commands: Value,
 | 
					    pub commands: Value,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Get a macro by name form a guild.
 | 
				
			||||||
pub async fn guild_command_macro(
 | 
					pub async fn guild_command_macro(
 | 
				
			||||||
    ctx: &Context<'_>,
 | 
					    ctx: &Context<'_>,
 | 
				
			||||||
    name: &str,
 | 
					    name: &str,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user