Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

341
Views
Discord.js - Get a user by ID or mention
if (!message.mentions.members.first() && !(args[0])) return message.channel.send(`<@${message.author.id}>: ${insultList[Math.floor(Math.random() * insultList.length)]}`);

const user = message.mentions.members.first() || message.guild.members.cache.get(args[0]);

if (!user) return message.inlineReply(new Discord.MessageEmbed().setColor("D9B3FF").setTitle("Error").setDescription("User not found."));

message.channel.send(`${user}: ${insultList[Math.floor(Math.random() * insultList.length)]}`);

An !insult command; insultList being an array of strings. However, calling the command with user IDs (of guild members) only occasionally works; it usually returns User not found. Everything else works as intended.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

user will return false if the id of args[0] corresponds to an uncached member.

  1. Use GuildMemberManager#fetch() to fetch the member if they were not mentioned
  2. Use (GuildMember|User)#toString() to parse member objects into mentions, no need to build them from scratch (<@id>)
const insult = insultList[Math.floor(Math.random() * insultList.length)];

if (!message.mentions.members.first() && !(args[0])) return message.channel.send(`${message.author.toString()}: ${insult}`);

try {
   const user = message.mentions.members.first() || (await message.guild.members.fetch(args[0]));
   if (!user) return message.inlineReply(new Discord.MessageEmbed().setColor("D9B3FF").setTitle("Error").setDescription("User not found."));

   message.channel.send(`${user.toString()}: ${insult}`);
} catch (err) {
   console.error(err);
}

Ensure you have the Guild Member's Intent enabled and you make your parent function is asynchronous for this example. Otherwise resolve the promise of .fetch() with callbacks.

about 4 years ago · Juan Pablo Isaza Report

0

have you tried mentioning the user with their id? like this: <@00000000>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!