lib.utils: Add selectionMessage and lookupUser
This commit is contained in:
parent
ffd0b91cd9
commit
bb8f3128ea
1 changed files with 148 additions and 2 deletions
150
src/lib/utils.js
150
src/lib/utils.js
|
@ -1,9 +1,10 @@
|
|||
const {v3} = require("murmurhash");
|
||||
const Eris = require("eris");
|
||||
const murmurhash = require("murmurhash").v3;
|
||||
const colorcolor = require("colorcolor");
|
||||
const fetch = require("node-fetch");
|
||||
|
||||
function pastelize(id) {
|
||||
const hue = v3(id) % 360;
|
||||
const hue = murmurhash(id) % 360;
|
||||
const hex = colorcolor(`hsl(${hue},75%,60%)`, "hex");
|
||||
return parseInt(hex.substring(1), 16);
|
||||
}
|
||||
|
@ -135,6 +136,149 @@ async function hastebin(body) {
|
|||
return res.key;
|
||||
}
|
||||
|
||||
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,
|
||||
allowedMentions: {
|
||||
repliedUser: false,
|
||||
},
|
||||
messageReference: {
|
||||
messageID: msg.id,
|
||||
},
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
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}`);
|
||||
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();
|
||||
}
|
||||
|
||||
resolve(options[number - 1].value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
hf.events.add("messageCreate", `selection.${msg.id}`, listener);
|
||||
hf.selectionMessages.set(
|
||||
msg.id,
|
||||
setTimeout(() => {
|
||||
hf.events.remove("messageCreate", `selection.${msg.id}`);
|
||||
hf.selectionMessages.remove(msg.id);
|
||||
|
||||
displayMessage.delete();
|
||||
|
||||
reject("Request timed out");
|
||||
}, timeout)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
async function lookupUser(msg, str, filter) {
|
||||
if (/[0-9]{17,21}/.test(str)) {
|
||||
return await hf.bot.requestHandler.request(
|
||||
"GET",
|
||||
"/users/" + str.match(/([0-9]{17,21})/)[1],
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
let users;
|
||||
if (filter) {
|
||||
users = hf.bot.users.filter(filter).values();
|
||||
} else if (msg.channel.guild) {
|
||||
users = msg.channel.guild.members.values();
|
||||
} else {
|
||||
users = hf.bot.users.values();
|
||||
}
|
||||
|
||||
if (/(.+?)#([0-9]{4})/.test(str)) {
|
||||
const [_, name, discrim] = str.match(/(.+?)#([0-9]{4})/);
|
||||
for (const user of users) {
|
||||
if (
|
||||
user.username.toLowerCase() == name.toLowerCase() &&
|
||||
user.discriminator == discrim
|
||||
) {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const selection = [];
|
||||
for (const user of users) {
|
||||
if (
|
||||
user.username.toLowerCase() == str.toLowerCase() ||
|
||||
(user.nickname && user.nickname == str.toLowerCase())
|
||||
) {
|
||||
return user;
|
||||
} else if (
|
||||
user.username.toLowerCase().indexOf(str.toLowerCase()) > -1 ||
|
||||
(user.nick && user.nick.indexOf(str.toLowerCase()) > -1)
|
||||
) {
|
||||
selection.push({
|
||||
value: user,
|
||||
display: `${user.username}#${user.discriminator}${
|
||||
user.nick ? ` (${user.nick})` : ""
|
||||
}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
selection.sort((a, b) => a.display - b.display);
|
||||
|
||||
if (selection.length == 0) {
|
||||
return "No results";
|
||||
} else if (selection.length == 1) {
|
||||
return selection[0];
|
||||
} else {
|
||||
selection.splice(20);
|
||||
|
||||
try {
|
||||
return await selectionMessage(
|
||||
msg,
|
||||
"Multiple users found, please pick from this list:",
|
||||
selection
|
||||
);
|
||||
} catch (out) {
|
||||
return out;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
pastelize,
|
||||
getTopColor,
|
||||
|
@ -145,4 +289,6 @@ module.exports = {
|
|||
findLastImage,
|
||||
getImage,
|
||||
hastebin,
|
||||
selectionMessage,
|
||||
lookupUser,
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue