简体   繁体   中英

Please help me with my discord.js userinfo command

I am quite new in Stack Overflow and in discord.js

So I am trying to make a userinfo command in discord.js and i keep getting this error in my console:

const userFlags = (await member.user.fetchFlags()).toArray(); SyntaxError: Unexpected identifier

( member is highlighted with ^^^). You also have the error at the back of the code.

Here is the code:

module.exports = {
    name: "userinfo",
    description: "Userinfo of mentioned user/id or if no one mentioned then yours",
   execute(client, msg, args, guild) {
         const embed = new MessageEmbed()
   const moment = require('moment');
   const Discord = require('discord.js');

   const member =  msg.mentions.members.first() || msg.guild.members.cache.get(args[0]) || msg.member;
   if (!member) 
        return msg.channel.send('Please mention the user for the userinfo..');
      const userFlags = (await member.user.fetchFlags()).toArray();
      const activities = [];
      let customStatus;
      for (const activity of member.presence.activities.values()) {
        switch (activity.type) {
          case 'PLAYING':
            activities.push(`Playing **${activity.name}**`);
            break;
          case 'LISTENING':
            if (member.user.bot) activities.push(`Listening to **${activity.name}**`);
            else activities.push(`Listening to **${activity.details}** by **${activity.state}**`);
            break;
          case 'WATCHING':
            activities.push(`Watching **${activity.name}**`);
            break;
          case 'STREAMING':
            activities.push(`Streaming **${activity.name}**`);
            break;
          case 'CUSTOM_STATUS':
            customStatus = activity.state;
            break;
        }
      }
      const uiembed = new Discord.MessageEmbed()
      .setTitle(`${member.displayName}'s Information`)
        .setThumbnail(member.user.displayAvatarURL({ dynamic: true }))
        .addField('User', member, true)
        .addField('Discriminator', `\`#${member.user.discriminator}\``, true)
        .addField('ID', `\`${member.id}\``, true)
        .addField('Status', statuses[member.presence.status], true)
        .addField('Bot', `\`${member.user.bot}\``, true)
        .addField('Color Role', member.roles.color || '`None`', true)
        .addField('Highest Role', member.roles.highest, true)
        .addField('Joined server on', `\`${moment(member.joinedAt).format('MMM DD YYYY')}\``, true)
        .addField('Joined Discord on', `\`${moment(member.user.createdAt).format('MMM DD YYYY')}\``, true)
        .setFooter(msg.member.displayName,  msg.author.displayAvatarURL({ dynamic: true }))
        .setTimestamp()
        .setColor(member.displayHexColor);
     if (activities.length > 0) uiembed.setDescription(activities.join('\n'));
      if (customStatus) uiembed.spliceFields(0, 0, { name: 'Custom Status', value: customStatus});
      if (userFlags.length > 0) uiembed.addField('Badges', userFlags.map(flag => flags[flag]).join('\n'));
      msg.channel.send(uiembed);
      }
    }

I get this error in the console:

      const userFlags = (await member.user.fetchFlags()).toArray();
                               ^^^^^^

SyntaxError: Unexpected identifier
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/Users/victor/Desktop/BIT bot/main.js:11:19)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)

When using the await keyword like that, you need to surround whatever you are await ing with parentheses

const userFlags = ( await (member.user.fetchFlags()) ).toArray()

Another, much cleaner option is making another variable for this

const rawFlags = await member.user.fetchFlags()
const userFlags = rawFlags.toArray()

Ok, so there are a few issues here.

  1. Change this line execute(client, msg, args, guild) { to execute: async(client, msg, args, guild) => {

  2. I'd discourage anyone to use msg.mentions.members.first() || msg.guild.members.cache.get(args[0]) || msg.member; msg.mentions.members.first() || msg.guild.members.cache.get(args[0]) || msg.member; despite it being easier to write. Because it only falls back to the next one if the one that it wants is undefined which with the nature of Discord.js, it has the intention of returning an object even though there is no one mentioned. Instead, use either ternary operators or if statements to check for the id using msg.mentions.members.first().id . That ensures that it will always return the person that has been mentioned. After that, you can find for the member using await guild.members.fetch(/* The mentioned user's ID */) .

This is all I know, I hope that my solution is helpful to you and let me know if it still doesn't work for you.



Edit: I just realised that there is no real purpose for ternary operators for this use case. I'd personally use if statements as it's easier to write it directly then to waste time defining that fallback and the managing system in ternary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM