Skip to content

Commit

Permalink
autocomplete on monitor to easily rename node
Browse files Browse the repository at this point in the history
  • Loading branch information
dadatuputi committed Dec 13, 2022
1 parent 883ff21 commit 2ba0c57
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion bot/commands/monitor_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = {
.setDescription('The Node ID to monitor')
.setRequired(true)
.setMaxLength(44)
.setMinLength(44))
.setMinLength(44)
.setAutocomplete(true))
.addStringOption(option =>
option.setName('name')
.setDescription('A friendly name for the node')),
Expand Down Expand Up @@ -65,4 +66,17 @@ module.exports = {
await interaction.reply({ content: reply_string, ephemeral: eph });
console.log(`User ${user.id} interaction from ${eph ? 'channel' : 'dm' }: monitor ${node_id}: ${reply_string}`)
},
async autocomplete(interaction, db) {
const user = interaction.user;
const focusedValue = interaction.options.getFocused();

// Get list of nodes monitored from db
const monitored_nodes = await db.list_user_nodes(user.id)
const choices = monitored_nodes.map( entry => ({id: entry.node, text: `${entry.name} (${entry.node})`}));
const filtered = choices.filter(choice => choice.text.toLowerCase().includes(focusedValue.toLowerCase()));

await interaction.respond(
filtered.map(choice => ({ name: choice.id, value: choice.id })), // setting name: choice.text should work, but it doesn't. Asked on SO: https://stackoverflow.com/q/74532512/1486966
);
},
};

0 comments on commit 2ba0c57

Please sign in to comment.