utils.selectionMessage: move to components
This commit is contained in:
parent
1014fe9979
commit
159870877b
1 changed files with 80 additions and 38 deletions
118
src/lib/utils.js
118
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})` : ""
|
||||
}`,
|
||||
|
|
Loading…
Reference in a new issue