pagination complete

This commit is contained in:
Tabby 2025-11-12 17:14:47 +11:00
parent 97552dffa5
commit 0ae75441c9
7 changed files with 168 additions and 32 deletions

View file

@ -23,5 +23,9 @@
- [x] provide real technician id
- [x] update global commands
- [x] spool pagination
- [x] spool list
- [x] spool delete search result (removed embed)
### Possible furture development
- job management

View file

@ -30,18 +30,20 @@ func execute(main, bot: DiscordBot, interaction: DiscordInteraction, options: Ar
Library.save.spools.append(new_spool)
Library.save_data()
var response : String = "Created new spool: `" + spool_name + "`\n"
var embed = Embed.new().set_description(Library.list_spools())
var response : String = "Created new spool: `" + spool_name + "`"
if spool_link:
response += " [Link]("+spool_link+")"
#var embed = Embed.new().set_description(Library.list_spools())
interaction.reply({
"content": response,
"embeds":[embed]
#"embeds":[embed]
})
pass
var data = ApplicationCommand.new()\
.set_name("spool-create")\
.set_description("create a new spool")\
.add_option(ApplicationCommand.string_option("name", "the printers name",{"required":true}))\
.add_option(ApplicationCommand.string_option("name", "the spools name",{"required":true}))\
.add_option(ApplicationCommand.string_option("link", "optional link to the fillaments page", {"required" : false}))

View file

@ -38,7 +38,7 @@ func execute(main, bot: DiscordBot, interaction: DiscordInteraction, options: Ar
return
var response : String = "Are you sure you want to delete: `" + spool_name + "`?\n"
var embed = Embed.new().set_description(Library.list_spools(spool_name))
#var embed = Embed.new().set_description(Library.list_spools(spool_name))
var row = MessageActionRow.new()
var delete_button = MessageButton.new().set_style(MessageButton.STYLES.DANGER)
delete_button.set_custom_id('delete-spool')
@ -51,7 +51,7 @@ func execute(main, bot: DiscordBot, interaction: DiscordInteraction, options: Ar
interaction.reply({
"content": response,
"embeds":[embed],
#"embeds":[embed],
"components":[row],
})
pass

View file

@ -25,15 +25,17 @@ func execute(main, bot: DiscordBot, interaction: DiscordInteraction, options: Ar
new_link = options[2].value
var spool_exists : bool = false
var found_spool : Spool
for spool : Spool in Library.save.spools:
if spool.name == spool_name:
spool_exists = true
if new_name != "0":
spool.name = new_name
if new_link:
spool.link = new_link
found_spool = spool
#Library.save.printers.erase(printer)
Library.save_data()
@ -44,13 +46,17 @@ func execute(main, bot: DiscordBot, interaction: DiscordInteraction, options: Ar
})
return
var response : String = "finished editing `" + new_name + "`\n"
var embed = Embed.new().set_description(Library.list_spools(new_name))
var response : String = "finished editing `" + new_name + "`"
if found_spool:
if found_spool.link:
response += " [Link]("+found_spool.link+")"
#var embed = Embed.new().set_description(Library.list_spools(new_name))
interaction.reply({
"content": response,
"embeds":[embed],
#"embeds":[embed],
})
pass

View file

@ -1,23 +1,115 @@
extends RefCounted
#func on_ready(main, bot: DiscordBot) -> void:
# pass
#
var page : int = 0
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:
# pass
func execute(main, bot: DiscordBot, interaction: DiscordInteraction, options: Array) -> void:
var response : String = "Listing spools..."
var embed = Embed.new().set_description(Library.list_spools())
page = 0 # start on the first page when this command is run
if options.size() > 0:
page = clampi(int(options[0].value),0,get_total_pages()-1) # if a page is provided start on that page instead
# need to figure out a few things
# 1: are there enough spools to enable pages
var loaded_spools : int = 0
for printer : Printer in Library.save.printers:
loaded_spools += printer.spools.size()
var num_spools : int = Library.save.spools.size() + loaded_spools
var enable_pages : bool = num_spools > 25
var total_pages : int = ceili(num_spools / float(25.0))
var row : MessageActionRow
if enable_pages:
# we need buttons and the more advanced spool list
row = make_buttons()
pass
var response : String = "Listing spools..."
if enable_pages:
response += " (Page "+str(page)+"/"+str(total_pages-1)+")"
var embed = Embed.new().set_description(Library.list_spools("", page))
if enable_pages:
interaction.reply({
"content": response,
"embeds":[embed],
"components" : [row],
})
else:
interaction.reply({
"content": response,
"embeds":[embed]
})
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 == "next-page"):
page = clampi(page+1,0,get_total_pages()-1)
var row : MessageActionRow = make_buttons()
var response = "Listing spools... (Page "+str(page)+"/"+str(get_total_pages()-1)+")"
var embed = Embed.new().set_description(Library.list_spools("", page))
interaction.update({
"content": response,
"embeds": [embed],
"components": [row]
})
elif(interaction.data.custom_id == "prev-page"):
page = clampi(page-1,0,get_total_pages()-1)
var row : MessageActionRow = make_buttons()
var response = "Listing spools... (Page "+str(page)+"/"+str(get_total_pages()-1)+")"
var embed = Embed.new().set_description(Library.list_spools("", page))
interaction.update({
"content": response,
"embeds": [embed],
"components": [row]
})
func make_buttons() -> MessageActionRow:
var row = MessageActionRow.new()
var prev_button = MessageButton.new().set_style(MessageButton.STYLES.SECONDARY)
prev_button.set_custom_id('prev-page')
prev_button.set_label("⬅️ Previous Page")
prev_button.set_disabled(page <= 0)
var next_button = MessageButton.new().set_style(MessageButton.STYLES.SECONDARY)
next_button.set_custom_id('next-page')
next_button.set_label("Next Page ➡️")
next_button.set_disabled(page >= get_total_pages()-1)
row.add_component(prev_button)
row.add_component(next_button)
return row
func get_total_pages() -> int:
var loaded_spools : int = 0
for printer : Printer in Library.save.printers:
loaded_spools += printer.spools.size()
var num_spools : int = Library.save.spools.size() + loaded_spools
return ceili(num_spools / float(25))
var data = ApplicationCommand.new()\
.set_name("spool-list")\
.set_description("view all current spools")\
.add_option(ApplicationCommand.integer_option("page","display a specific page of the spool list (0 indexed)",
{
"required" : false,
}))\

View file

@ -47,9 +47,26 @@ func list_printers() -> String:
response += "\n- " + printer.list_string()
return response
func list_spools(search : String = "") -> String:
var response : String = "Loaded Spools:"
# displays a list of spools, will bold the name of the spool which matches the search if there are
# more than 25 spools, will enable pagination and display 25 of each page, set the page argement to
# display a specific page, will default to the first page
func list_spools(search : String = "", page : int = 0) -> String:
var response : String = ""
var unloaded_printers : int = 0
const per_page : int = 25
# count the number of displayed spools, when it reaches 25(per_page), STOP
var counted_spools : int = 0
# count the number of spools loaded into the printers, will be useful wheather its the first page or not
var loaded_spools : int = 0
for printer : Printer in save.printers:
loaded_spools += printer.spools.size()
if page == 0: # only display the printer spools on the first page
response = "Loaded Spools:"
counted_spools += loaded_spools
for printer : Printer in save.printers:
if printer.spools.size() > 0:
response += "\n- " + printer.list_string()
@ -60,14 +77,29 @@ func list_spools(search : String = "") -> String:
response += "\n\nAvailable Spools: "
var found_search_match = false
for spool : Spool in save.spools:
var i : int = (page * per_page) # starts higher on pages other than 0
if page > 0:
i -= loaded_spools
while i < save.spools.size() and counted_spools < per_page:
# print spools
var bold : bool = false
if spool.name == search and not found_search_match:
if save.spools[i].name == search and not found_search_match:
bold = true
found_search_match = true
response += "\n- " + spool.list_string(bold)
response += "\n- " + save.spools[i].list_string(bold)
counted_spools += 1
i += 1
return response
#for spool : Spool in save.spools:
#var bold : bool = false
#if spool.name == search and not found_search_match:
#bold = true
#found_search_match = true
#response += "\n- " + spool.list_string(bold)
#return response
func printer_choies() -> Array[Dictionary]:
var printer_names : Array[Dictionary] = []
for printer : Printer in save.printers:

View file

@ -56,11 +56,11 @@ func _on_bot_ready(bot: DiscordBot):
# -----Single server (updates instantly)
#_register_application_commands(bot, "guild_id_here")
_register_application_commands(bot, "679917161195765822") # personal server
#_register_application_commands(bot, "679917161195765822") # personal server
#_register_application_commands(bot, "1038007666032787476") # fabsoc
# -----Global (may take upto 1hr to update)
#_register_application_commands(bot)
_register_application_commands(bot) # everywhere
#func _on_message_create(bot: DiscordBot, message: Message, channel: Dictionary) -> void: