From c26b350e49982d85d382e0e251219339a9396624 Mon Sep 17 00:00:00 2001 From: Tabby <41929769+tabby-cat-nya@users.noreply.github.com> Date: Thu, 30 Oct 2025 15:10:21 +1100 Subject: [PATCH] printer deletion flow with confirmatin --- application_cmds/printer-delete.gd | 92 +++++++++++++++++++++++++++--- library.gd | 6 ++ library.gd.uid | 1 - main.gd | 18 +++--- 4 files changed, 100 insertions(+), 17 deletions(-) delete mode 100644 library.gd.uid diff --git a/application_cmds/printer-delete.gd b/application_cmds/printer-delete.gd index 398c8d4..87ed9cf 100644 --- a/application_cmds/printer-delete.gd +++ b/application_cmds/printer-delete.gd @@ -1,32 +1,108 @@ extends RefCounted -#func on_ready(main, bot: DiscordBot) -> void: -# pass +var endangered_printer : String = "" + +func on_ready(main, bot: DiscordBot) -> void: + bot.interaction_create.connect(on_interaction_create) + pass # func on_autocomplete(main, bot: DiscordBot, interaction: DiscordInteraction, options: Array) -> void: - print(options) - interaction.respond_autocomplete(Library.printer_choies()) + #print(options) + #interaction.respond_autocomplete(Library.printer_choies()) + + # The part of string which the user is typing + var part = options[0].value + #print("received autocomplete: ", part) + + # The final Array of choices for the autocomplete response + var result = [] + for hint in Library.printer_choices_string(): + # If the user hasn't typed anything, add all the hints + if part == "": + result.append(ApplicationCommand.choice(hint, hint)) + else: + # If the user has typed some part of string, + # add only those hints which have the part as a substring + if hint.findn(part) > -1: + result.append(ApplicationCommand.choice(hint, hint)) + + # Limit the number of results to 25 (Discord's limit is 25) + if result.size() > 25: + result = result.slice(0, 24) + + # Respond with the results + interaction.respond_autocomplete(result) pass func execute(main, bot: DiscordBot, interaction: DiscordInteraction, options: Array) -> void: print(options) var printer_name = options[0].value + var printer_exists : bool = false for printer in Library.save.printers: if printer.name == printer_name: - Library.save.printers.erase(printer) + printer_exists = true + endangered_printer = printer.name + #Library.save.printers.erase(printer) - Library.save_data() + #Library.save_data() - var response : String = "erased printer: `" + printer_name + "`\n" + if not printer_exists: + interaction.reply({ + "content" : "unable to find " + printer_name + }) + return + + var response : String = "Are you sure you want to delete: `" + printer_name + "`?\n" var embed = Embed.new().set_description(Library.list_printers()) + var row = MessageActionRow.new() + var delete_button = MessageButton.new().set_style(MessageButton.STYLES.DANGER) + delete_button.set_custom_id('delete-printer') + delete_button.set_label("Yes, delete " + printer_name) + var keep_button = MessageButton.new().set_style(MessageButton.STYLES.DEFAULT) + keep_button.set_custom_id('keep-printer') + keep_button.set_label("No, keep the printer") + row.add_component(delete_button) + row.add_component(keep_button) interaction.reply({ "content": response, - "embeds":[embed] + "embeds":[embed], + "components":[row], }) pass +func on_interaction_create(bot: DiscordBot, interaction : DiscordInteraction): + if not interaction.is_button(): + return + + print(interaction.data.custom_id) + + if(interaction.data.custom_id == "delete-printer"): + #print("deleting: " + endangered_printer) + for printer in Library.save.printers: + if printer.name == endangered_printer: + Library.save.printers.erase(printer) + Library.save_data() + var embed = Embed.new().set_description(endangered_printer + " has been deleted") + var new_embeds = interaction.message.embeds + [embed] + interaction.update({ + "content": interaction.message.content, + "embeds": new_embeds, + "components": [] + }) + + elif(interaction.data.custom_id == "keep-printer"): + endangered_printer = "" + var embed = Embed.new().set_description("cancelled deletion") + var new_embeds = interaction.message.embeds + [embed] + interaction.update({ + "content": interaction.message.content, + "embeds": new_embeds, + "components": [] + }) + + var data = ApplicationCommand.new()\ .set_name("printer-delete")\ diff --git a/library.gd b/library.gd index 53acfc9..95e828e 100644 --- a/library.gd +++ b/library.gd @@ -49,3 +49,9 @@ func printer_choies() -> Array[Dictionary]: for printer : Printer in save.printers: printer_names.append(ApplicationCommand.choice(printer.name, printer.name)) return printer_names + +func printer_choices_string() -> Array[String]: + var printer_names : Array[String] = [] + for printer : Printer in save.printers: + printer_names.append(printer.name) + return printer_names diff --git a/library.gd.uid b/library.gd.uid deleted file mode 100644 index 5fa26b1..0000000 --- a/library.gd.uid +++ /dev/null @@ -1 +0,0 @@ -uid://hkh1ewsuji8m diff --git a/main.gd b/main.gd index c0c5211..d864358 100644 --- a/main.gd +++ b/main.gd @@ -165,13 +165,14 @@ func _register_application_commands(bot, guild_id: String = "") -> void: #cmd.on_message(self, bot, message, channel, args) func remove_components_from_interaction(interaction: DiscordInteraction, msg = ":robot: Components have timed out!") -> void: - var embed = Embed.new().set_description(msg) - var new_embeds = interaction.message.embeds + [embed] - interaction.update({ - "content": interaction.message.content, - "embeds": new_embeds, - "components": [] - }) + #var embed = Embed.new().set_description(msg) + #var new_embeds = interaction.message.embeds ## + [embed] TODO? + #interaction.update({ + #"content": interaction.message.content, + #"embeds": new_embeds, + #"components": [] + #}) + pass func _on_interaction_create(bot: DiscordBot, interaction: DiscordInteraction): # Handle ApplicationCommand @@ -189,12 +190,13 @@ func _on_interaction_create(bot: DiscordBot, interaction: DiscordInteraction): print("APP_CMD: " + app_cmd.data.name + " by " + interaction.member.user.username + "#" + interaction.member.user.discriminator + " (" + interaction.member.user.id + ")") app_cmd.execute(self, bot, interaction, options) - else: + else: # The application command was not found interaction.reply({ "ephemeral": true, "content": ":electric_plug: The requested command was not found." }) + pass return var msg_id = interaction.message.id