Skip to content

Instantly share code, notes, and snippets.

@NoTimeForHero
Created November 26, 2019 22:44
Show Gist options
  • Save NoTimeForHero/c0cf161d8d2daa84179ce834cb7f223e to your computer and use it in GitHub Desktop.
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)
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