💬Message

Prototypes from Message

A Super Mention Parsing Feature

-command @user 123456789123456 memberName <- 1 ID and 1 name to parse

-command @user @user2 123456789123456 <- 1 ID to parse

-command 9876543210789 memberName memberName2 <- 1 ID and 2 names to parse

-command @user @user1 @user2 <- 0 ID to parse

You will never know what the user will pass as ID. It can be a member ID, a channel ID, an user ID, or anything else.

Tons of checks are required for the code to work as expected.


What does discord.js return as mentions?

Discord.js returns a Collection of mentions by default

<Message>.mentions.channels; // Collection<string, Channel>()
<Message>.mentions.members; // Collection<string, GuildMember>()
<Message>.mentions.roles; // Collection<string, Role>()
<Message>.mentions.users; // Collection<string, User>()

What about Names and ID's?

The discord.js code does not check names, ID's or strings, just mentions.

<Client>.users.fetch(string); // User | Error
<Guild>.channels.cache.get(string); // Channel | undefined
<Guild>.members.fetch(stringD); // GuildMember | Error
<Guild>.roles.cache.get(string); // Role | undefined

Now you have a Collection of mentions, the results of fetches or possible errors. Very complicated.


Let's parse with djs-protofy!

-command @user @role 12345678912345678 98765432151425487 roleName memberName @channel Let's think this is a hard example, an user, a role and a channel has been mentioned. We have 2 any ID's, 1 role name and 1 member name.

The protofy parser will return a Collection, like discord.js, but with all mentions, names and IDs resolved. We will search for the name and ID, get the result and add it to the original Collection. This is like magic!

CHANNELS - <Message>.parseChannelMentions(); // Collection<string, Channel>()
ROLES - <Message>.parseRoleMentions(); // Collection<string, Role>()
MEMBERS - await <Message>.parseMemberMentions(); // Promise<Collection<string, GuildMember>()>
USERS - await <Message>.parseUserMentions(); // Promise<Collection<string, User>()>
// OR
await <Message>.parseMentions(); // Promise<void>

Parsing Channels

client.on("messageCreate", message => {

  const channels = message.parseChannelMentions();
  
  console.log(channels); // Collection<string, Channel>()
  // OR
  console.log(message.mentions.channels); // Collection<string, Channel>()

});

Parsing Roles

client.on("messageCreate", message => {

  const roles = message.parseRoleMentions();

  console.log(roles); // Collection<string, Role>()
  // OR
  console.log(message.mentions.roles); // Collection<string, Role>()

});

Parsing Members

client.on("messageCreate", async message => {

  const members = await message.parseMemberMentions();

  console.log(members); // Collection<string, GuildMember>()
  // OR
  console.log(message.mentions.members); // Collection<string, GuildMember>()

});

Parsing Users

client.on("messageCreate", async message => {

  const users = await message.parseUserMentions();

  console.log(users); // Collection<string, User>()
  // OR
  console.log(message.mentions.users); // Collection<string, User>()

});

Parsing All Mentions

client.on("messageCreate", async message => {

  // It will resolve all IDs and Names and add to mentions below
  await message.parseMentions();
  console.log(message.mentions.channels); // Collection<string, Channel>()
  console.log(message.mentions.members); // Collection<string, GuildMember>()
  console.log(message.mentions.roles);  // Collection<string, Role>()
  console.log(message.mentions.users); // Collection<string, User>()

});

Last updated