spool refactor done, now supports multiple spools attached to a single printer, goodnight

This commit is contained in:
Tabby 2025-11-02 01:38:37 +11:00
parent b83ef6b77b
commit 7276e502af
7 changed files with 131 additions and 77 deletions

73
AutocompleteTools.gd Normal file
View file

@ -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)

1
AutocompleteTools.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://chjcu3ifury52

View file

@ -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

View file

@ -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")

View file

@ -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))
AutocompleteTools.spool_autocomplete(options[1].value, interaction)
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)
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:

View file

@ -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:
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 = printer.spool.name
Library.save.spools.append(printer.spool)
printer.spool = null
#Library.save.printers.erase(printer)
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

View file

@ -18,6 +18,7 @@ config/icon="uid://dta0nr1cvl70v"
[autoload]
Library="*res://Library.tscn"
AutocompleteTools="*res://AutocompleteTools.gd"
[editor_plugins]