From 159870877b0a8d2bf0ce7424a355311a72adb6ed Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Sun, 29 Aug 2021 20:02:02 -0600 Subject: [PATCH] utils.selectionMessage: move to components --- src/lib/utils.js | 118 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 80 insertions(+), 38 deletions(-) diff --git a/src/lib/utils.js b/src/lib/utils.js index f847192..8ae9316 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -159,62 +159,103 @@ async function hastebin(body) { hf.selectionMessages = hf.selectionMessages || new Eris.Collection(); async function selectionMessage(msg, heading, options, timeout = 30000) { - let content = `${heading}\n\`\`\`ini\n`; - options.slice(0, 20).forEach((value, index) => { - content += `[${index + 1}] ${value.display}\n`; - }); - if (options.length > 20) { - content += `; Displaying 20/${options.length} results\n`; - } - content += "\n[c] Cancel```"; - - const displayMessage = await msg.channel.createMessage({ - content, + const data = { + content: heading, allowedMentions: { repliedUser: false, }, messageReference: { messageID: msg.id, }, + components: [ + { + type: 1, + components: [ + { + type: 3, + custom_id: "selection", + options: [], + }, + ], + }, + { + type: 1, + components: [ + { + type: 2, + style: 4, + label: "Cancel", + custom_id: "cancel", + }, + ], + }, + ], + }; + options.slice(0, 20).forEach((value) => { + data.components[0].components[0].options.push({ + label: value.display, + value: value.key, + }); }); + if (options.length > 20) { + data.content += `\n\nDisplaying 20/${options.length} results`; + } + + const displayMessage = await msg.channel.createMessage(data); return await new Promise((resolve, reject) => { - function listener(msg2) { - if ( - msg2.author.id == msg.author.id && - msg2.channel.id == msg.channel.id - ) { - if (msg2.content == "c") { - hf.events.remove("messageCreate", `selection.${msg.id}`); - clearTimeout(hf.selectionMessages.get(msg.id)); - hf.selectionMessages.remove(msg.id); + function listener(packet) { + if (packet.t == "INTERACTION_CREATE") { + const interaction = packet.d; + const user = interaction.member.user || interaction.user; - displayMessage.delete(); - if (msg.channel.permissionsOf(hf.bot.user.id).has("manageMessages")) { - msg2.delete(); - } - - reject("Canceled"); - } else { - const number = parseInt(msg2.content); - if (!isNaN(number) && number < 21 && number > 0) { - hf.events.remove("messageCreate", `selection.${msg.id}`); + if ( + user.id == msg.author.id && + interaction.channel_id == msg.channel.id + ) { + if (interaction.data.custom_id == "cancel") { + hf.events.remove("unknown", `selection.${msg.id}`); clearTimeout(hf.selectionMessages.get(msg.id)); hf.selectionMessages.remove(msg.id); - displayMessage.delete(); - if ( - msg.channel.permissionsOf(hf.bot.user.id).has("manageMessages") - ) { - msg2.delete(); - } + hf.bot.requestHandler.request( + "POST", + `/interactions/${interaction.id}/${interaction.token}/callback`, + false, + { + type: 6, + } + ); - resolve(options[number - 1].value); + displayMessage.delete(); + + reject("Canceled"); + } else { + hf.events.remove("unknown", `selection.${msg.id}`); + clearTimeout(hf.selectionMessages.get(msg.id)); + hf.selectionMessages.remove(msg.id); + + hf.bot.requestHandler.request( + "POST", + `/interactions/${interaction.id}/${interaction.token}/callback`, + false, + { + type: 6, + } + ); + + displayMessage.delete(); + + const result = options.filter( + (opt) => opt.key == interaction.data.values[0] + )[0].value; + + resolve(result); } } } } - hf.events.add("messageCreate", `selection.${msg.id}`, listener); + hf.events.add("unknown", `selection.${msg.id}`, listener); hf.selectionMessages.set( msg.id, setTimeout(() => { @@ -272,6 +313,7 @@ async function lookupUser(msg, str, filter) { ) { selection.push({ value: user, + key: user.id, display: `${user.username}#${user.discriminator}${ user.nick ? ` (${user.nick})` : "" }`,