Added mention support

This commit is contained in:
CBenni 2018-01-03 14:38:44 +01:00
parent c4a253b3ae
commit 259a571d69

View file

@ -158,7 +158,11 @@ client.login(config.discord.token).catch(error => {
} }
}); });
function findEmoji(str) { const discordAvatarRegex = /(https:\/\/cdn.discordapp.com\/avatars\/\w+\/\w+\.(\w+)\?size=)(\w+)/;
function findEmoji(message) {
const str = message.cleanContent;
// find a discord emote
const discordEmote = /<(a?):(\w+):(\d+)>/g.exec(str); const discordEmote = /<(a?):(\w+):(\d+)>/g.exec(str);
if (discordEmote) { if (discordEmote) {
const ext = discordEmote[1] === 'a' ? 'gif' : 'png'; const ext = discordEmote[1] === 'a' ? 'gif' : 'png';
@ -170,6 +174,7 @@ function findEmoji(str) {
}; };
} }
// find a unicode emoji
let unicodeEmoji; let unicodeEmoji;
twemoji.parse(str, (name, emoji) => { twemoji.parse(str, (name, emoji) => {
if (unicodeEmoji) return false; if (unicodeEmoji) return false;
@ -181,7 +186,28 @@ function findEmoji(str) {
}; };
return false; return false;
}); });
return unicodeEmoji; if (unicodeEmoji) return unicodeEmoji;
// find a user mention
if (message.mentions.members.size > 0) {
const mentionedMember = message.mentions.members.first();
const mentionedUser = mentionedMember.user;
let avatarUrl = mentionedUser.displayAvatarURL;
const avatarMatch = discordAvatarRegex.exec(avatarUrl);
if (avatarMatch) {
const ext = avatarMatch[2];
avatarUrl = `${avatarMatch[1]}128`;
}
console.log('Avatar url: ', avatarUrl);
return {
name: mentionedMember.displayName,
id: mentionedMember.id,
url: avatarUrl,
ext: avatarUrl.indexOf('.gif') >= 0 ? 'gif' : 'png'
};
}
return null;
} }
function reverseString(str) { function reverseString(str) {
@ -210,7 +236,7 @@ client.on('message', async message => {
} }
const messageSplit = message.cleanContent.split(' '); const messageSplit = message.cleanContent.split(' ');
const emoji = findEmoji(message.cleanContent); const emoji = findEmoji(message);
let result = null; let result = null;
let count = 0; let count = 0;
try { try {