emoji resolver

This commit is contained in:
Erik 2021-06-15 13:04:38 +03:00
parent ed4a498fc8
commit b9db9e759c
No known key found for this signature in database
GPG Key ID: 7E862371D3409F16
3 changed files with 69 additions and 3 deletions

View File

@ -35,6 +35,7 @@
"request": "^2.88.2",
"similarity": "^1.2.1",
"timestring": "^6.0.0",
"twemoji-parser": "^13.1.0",
"winston": "^3.2.1",
"winston-transport": "^4.3.0"
},

View File

@ -1,6 +1,7 @@
const timestring = require('timestring');
const moment = require('moment');
const dns = require('dns');
const { parse: resolveTwemoji } = require('twemoji-parser');
const { InfractionResolves } = require('../../util/Constants.js');
// eslint-disable-next-line no-unused-vars
@ -593,10 +594,10 @@ class Resolver {
const { roles } = guild;
const resolved = [];
for(const resolveable of resolveables) {
const id = /^(<@&)?([0-9]{16,22})>?/iu;
for(const resolveable of resolveables) {
if(id.test(resolveable)) {
const match = resolveable.match(id);
@ -711,6 +712,65 @@ class Resolver {
}
async resolveEmojis(resolveables = [], strict, guild) {
if (typeof resolveables === 'string') resolveables = [resolveables];
if (resolveables.length === 0) return false;
//const { emojis: guildEmojis } = guild;
const { emojis: clientEmojis } = this.client;
const resolved = [];
const emojiMention = /<(?:a)?:?([\w-]{2,32}):(\d{17,32})>/iu;
for (const resolveable of resolveables) {
if (emojiMention.test(resolveable)) {
const match = resolveable.match(emojiMention);
const [, , eId] = match;
const emoji = clientEmojis.resolve(eId); //.catch(this.client.logger.error); // use .fetch(eId) once v13 rolls out
if (emoji) resolved.push({ type: 'custom', emoji });
} else {
const sorter = (a, b) => a.name.length - b.name.length;
const filter = (e) => {
if (!strict) return e.name.toLowerCase().includes(resolveable.toLowerCase());
return e.name.toLowerCase() === resolveable.toLowerCase();
};
let emoji = null;
if (guild) emoji = guild.emojis.cache.sort(sorter).filter(filter).first();
if (!emoji) emoji = clientEmojis.cache.sort(sorter).filter(filter).first();
if (emoji) {
resolved.push({ type: 'custom', emoji });
continue;
} else { // twemoji parse
const [result] = resolveTwemoji(resolveable);
if (result) {
({ text: emoji } = result);
resolved.push({ type: 'unicode', emoji });
}
}
}
}
return resolved.length > 0 ? resolved : false;
}
async resolveEmoji(resolveable, strict, guild) {
if (!resolveable) return false;
if (resolveable instanceof Array) throw new Error('Resolveable cannot be of type Array, use resolveEmojis for resolving arrays of emojis');
const result = await this.resolveEmojis([resolveable], strict, guild);
return result ? result[0] : false;
}
}
module.exports = Resolver;

View File

@ -1572,7 +1572,7 @@ natural-compare@^1.4.0:
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=
node-fetch@^2.6.0, node-fetch@^2.6.1:
node-fetch@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
@ -2247,6 +2247,11 @@ tweetnacl@^1.0.3:
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-1.0.3.tgz#ac0af71680458d8a6378d0d0d050ab1407d35596"
integrity sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==
twemoji-parser@^13.1.0:
version "13.1.0"
resolved "https://registry.yarnpkg.com/twemoji-parser/-/twemoji-parser-13.1.0.tgz#65e7e449c59258791b22ac0b37077349127e3ea4"
integrity sha512-AQOzLJpYlpWMy8n+0ATyKKZzWlZBJN+G0C+5lhX7Ftc2PeEVdUU/7ns2Pn2vVje26AIZ/OHwFoUbdv6YYD/wGg==
type-check@^0.4.0, type-check@~0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.4.0.tgz#07b8203bfa7056c0657050e3ccd2c37730bab8f1"