Created
November 26, 2019 22:44
-
-
Save NoTimeForHero/c0cf161d8d2daa84179ce834cb7f223e to your computer and use it in GitHub Desktop.
Discord.JS function to get users IDS as array from any mention in message (@here/@everyone/@groupName/@userName)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Utils = { | |
findUsersByMessage(ev) { | |
const findUsers = ev => { | |
const regExMention = /<@(\d+)>/g; | |
const users = [...ev.content.matchAll(regExMention)]; | |
return users.map(x => x[1]); | |
} | |
const findGroups = ev => { | |
const regExMention = /<@&(\d+)>/g; | |
const groupsIds = [...ev.content.matchAll(regExMention)].map(x => x[1]); | |
const groups = [...ev.guild.roles.values()].filter(gr => groupsIds.includes(gr.id)); | |
const users = groups.map(gr => [...gr.members.keys()]); | |
return [].concat.apply([], users); | |
} | |
const findAll = ev => { | |
if (!ev.content.includes("@everyone")) return []; | |
const members = [...ev.guild.members.values()]; | |
return members.map(x => x.id); | |
} | |
const findHere = ev => { | |
if (!ev.content.includes("@here")) return []; | |
const members = [...ev.channel.members.values()]; | |
return members.map(x => x.id); | |
} | |
const ids = [findAll, findGroups, findUsers, findHere].map(x => x(ev)); | |
const users = [].concat.apply([], ids); | |
return [...new Set(users)]; | |
} | |
} | |
module.exports = Utils; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment