简体   繁体   English

请帮我处理我的 discord.js userinfo 命令

[英]Please help me with my discord.js userinfo command

I am quite new in Stack Overflow and in discord.js我是 Stack Overflow 和 discord.js 的新手

So I am trying to make a userinfo command in discord.js and i keep getting this error in my console:所以我试图在 discord.js 中创建一个 userinfo 命令,但我在控制台中不断收到此错误:

const userFlags = (await member.user.fetchFlags()).toArray(); const userFlags = (await member.user.fetchFlags()).toArray(); SyntaxError: Unexpected identifier SyntaxError:意外的标识符

( member is highlighted with ^^^). ( member用^^^突出显示)。 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像这样使用await关键字时,您需要用括号括住您正在await的任何内容

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) => {更改此行execute(client, msg, args, guild) { 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; 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.因为如果它想要的那个是undefined的,它只回退到下一个,它具有 Discord.js 的性质,它打算返回一个 object,即使没有提到。 Instead, use either ternary operators or if statements to check for the id using msg.mentions.members.first().id .相反,使用三元运算符或 if 语句使用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 */) .之后,您可以使用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.我个人会使用 if 语句,因为直接编写它更容易,然后浪费时间定义回退和三元管理系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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