From 7276e502afbe6c9d4292827f44ebbc818bf00824 Mon Sep 17 00:00:00 2001 From: Tabby <41929769+tabby-cat-nya@users.noreply.github.com> Date: Sun, 2 Nov 2025 01:38:37 +1100 Subject: [PATCH] spool refactor done, now supports multiple spools attached to a single printer, goodnight --- AutocompleteTools.gd | 73 +++++++++++++++++++++++++ AutocompleteTools.gd.uid | 1 + README.md | 3 +- application_cmds/printer-delete.gd | 1 + application_cmds/printer-load.gd | 43 +-------------- application_cmds/printer-unload.gd | 86 ++++++++++++++++++------------ project.godot | 1 + 7 files changed, 131 insertions(+), 77 deletions(-) create mode 100644 AutocompleteTools.gd create mode 100644 AutocompleteTools.gd.uid diff --git a/AutocompleteTools.gd b/AutocompleteTools.gd new file mode 100644 index 0000000..81aab40 --- /dev/null +++ b/AutocompleteTools.gd @@ -0,0 +1,73 @@ +extends Node + +func spool_autocomplete(text : String, interaction : DiscordInteraction): + var spool_part : String = text + print("received spool autocomplete: ", spool_part) + # The final Array of choices for the autocomplete response + var result = [] + for hint in Library.spool_choices_string(): + # If the user hasn't typed anything, add all the hints + if spool_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(spool_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) + +func printer_autocomplete(text : String, interaction : DiscordInteraction): + var part = text + 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) + +func loaded_spools_autocomplete(text : String, interaction : DiscordInteraction, spool_list : Array[Spool]): + var part = text + print("received autocomplete: ", part) + + var spool_choices_string : Array[String] = [] + for spool : Spool in spool_list: + spool_choices_string.append(spool.name) + + # The final Array of choices for the autocomplete response + var result = [] + for hint in spool_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) diff --git a/AutocompleteTools.gd.uid b/AutocompleteTools.gd.uid new file mode 100644 index 0000000..fb3df51 --- /dev/null +++ b/AutocompleteTools.gd.uid @@ -0,0 +1 @@ +uid://chjcu3ifury52 diff --git a/README.md b/README.md index 9956d89..8877471 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,5 @@ - [x] spool list should show loaded spools in printer, then unloaded and available spools - [X] unloading a spool returns it to the library - [ ] job management? -- [ ] shoot some of the printers can hold multiple fillaments: endermixer 2, ams: 4 +- [x] shoot some of the printers can hold multiple fillaments: endermixer 2, ams: 4 +- [ ] handling of spools when printer is deleted diff --git a/application_cmds/printer-delete.gd b/application_cmds/printer-delete.gd index 87ed9cf..a2241d6 100644 --- a/application_cmds/printer-delete.gd +++ b/application_cmds/printer-delete.gd @@ -82,6 +82,7 @@ func on_interaction_create(bot: DiscordBot, interaction : DiscordInteraction): #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") diff --git a/application_cmds/printer-load.gd b/application_cmds/printer-load.gd index 40e4305..4e8d1a2 100644 --- a/application_cmds/printer-load.gd +++ b/application_cmds/printer-load.gd @@ -13,48 +13,9 @@ func on_autocomplete(main, bot: DiscordBot, interaction: DiscordInteraction, opt # The part of string which the user is typing var spool_part : String if options.size() > 1: - spool_part = options[1].value - print("received spool autocomplete: ", spool_part) - # The final Array of choices for the autocomplete response - result = [] - for hint in Library.spool_choices_string(): - # If the user hasn't typed anything, add all the hints - if spool_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(spool_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) + AutocompleteTools.spool_autocomplete(options[1].value, interaction) else: - var part = options[0].value - print("received autocomplete: ", part) - - # The final Array of choices for the autocomplete response - 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) + AutocompleteTools.printer_autocomplete(options[0].value, interaction) pass func execute(main, bot: DiscordBot, interaction: DiscordInteraction, options: Array) -> void: diff --git a/application_cmds/printer-unload.gd b/application_cmds/printer-unload.gd index 3d3ba0a..59b821a 100644 --- a/application_cmds/printer-unload.gd +++ b/application_cmds/printer-unload.gd @@ -1,5 +1,6 @@ extends RefCounted +var printer_edit : Printer = null func on_ready(main, bot: DiscordBot) -> void: bot.interaction_create.connect(on_interaction_create) @@ -10,27 +11,7 @@ func on_autocomplete(main, bot: DiscordBot, interaction: DiscordInteraction, opt #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) + AutocompleteTools.printer_autocomplete(options[0].value, interaction) pass func execute(main, bot: DiscordBot, interaction: DiscordInteraction, options: Array) -> void: @@ -41,11 +22,40 @@ func execute(main, bot: DiscordBot, interaction: DiscordInteraction, options: Ar for printer in Library.save.printers: if printer.name == printer_name: - printer_exists = true - spool_name = printer.spool.name - Library.save.spools.append(printer.spool) - printer.spool = null - #Library.save.printers.erase(printer) + if printer.spools.size() == 0: + interaction.reply({ + "content": "Can't unload a spool from an empty printer", + #"embeds":[embed], + #"components":[row], + }) + + if printer.spools.size() == 1: + var unloading_spool : Spool = printer.spools[0] + printer_exists = true + spool_name = unloading_spool.name + Library.save.spools.append(unloading_spool) + printer.spools.erase(unloading_spool) + Library.save_data() + var response : String = "returned `" + spool_name + "` to the library" + interaction.reply({ + "content": response, + #"embeds":[embed], + #"components":[row], + }) + + else: #printer has multiple spools loaded: + printer_exists = true + var response : String = "This printer has multiple spools loaded, which would you like to unload?" + printer_edit = printer + var menu = SelectMenu.new().set_custom_id("spool-select") + for spool : Spool in printer.spools: + menu.add_option(spool.name, spool.name) + var row = MessageActionRow.new().add_component(menu) + interaction.reply({ + "content": response, + #"embeds":[embed], + "components":[row], + }) #Library.save_data() @@ -57,7 +67,7 @@ func execute(main, bot: DiscordBot, interaction: DiscordInteraction, options: Ar - var response : String = "returned `" + spool_name + "` to the library" + #var embed = Embed.new().set_description(Library.list_printers()) #var row = MessageActionRow.new() #var delete_button = MessageButton.new().set_style(MessageButton.STYLES.DANGER) @@ -69,19 +79,25 @@ func execute(main, bot: DiscordBot, interaction: DiscordInteraction, options: Ar #row.add_component(delete_button) #row.add_component(keep_button) - interaction.reply({ - "content": response, - #"embeds":[embed], - #"components":[row], - }) + pass func on_interaction_create(bot: DiscordBot, interaction : DiscordInteraction): - if not interaction.is_button(): + if not interaction.is_select_menu(): return - print(interaction.data.custom_id) - + print(interaction.data.values[0]) + var spool_unloading : String = interaction.data.values[0] + for spool : Spool in printer_edit.spools: + if spool.name == spool_unloading: + Library.save.spools.append(spool) + printer_edit.spools.erase(spool) + Library.save_data() + interaction.update({ + "content" : "returned `" + spool.name + "` to the library" , + "components" : [], + }) + break diff --git a/project.godot b/project.godot index b1b5bd0..02f80a7 100644 --- a/project.godot +++ b/project.godot @@ -18,6 +18,7 @@ config/icon="uid://dta0nr1cvl70v" [autoload] Library="*res://Library.tscn" +AutocompleteTools="*res://AutocompleteTools.gd" [editor_plugins]