怎么修?由于Value错误,Discord.js Bot保持脱机状态

时间:2019-05-29 04:19:40

标签: javascript node.js discord.js

我不断收到此错误

TypeError: Cannot read property 'size' of undefined
at Client.client.on.message 
(/home/colter/Code/groundskeeper/index.js:38:30)
at emitOne (events.js:116:13)
at Client.emit (events.js:211:7)
at MessageCreateHandler.handle 

我检查了错误并将其与示例代码进行了比较。在我看来一切都很好。

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) 
return;

const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();

// Here is my syntax for adding commands. So far simple ones, but commands non the less!
if (command === 'ping') {
    message.channel.send('Pong');
}
else if (command === 'beep') {
    message.channel.send('Boop');
}
else if (command === 'server') {
    message.channel.send(`Server name: ${message.guild.name}\nTotal members: ${message.guild.memberCount}`);
}
else if (command === 'user-info') {
    message.channel.send(`Your username: ${message.author.username}\nYour ID: ${message.author.id}`);
}
else if (command === 'args-info') {
    if (!args.length) {
        return message.channel.send(`Your didnt provide any arguments, ${message.author}!`);
    }
    else if (args[0] === 'foo') {
        return message.channel.send('bar');
    }
    message.channel.send(`first argument: ${args[0]}`);
}
else if (command === 'kick') {
    if (!message.mentions.user.size) {
        return message.reply('you need to tag a user in order to kick them');
    }

    const taggedUser = message.mentions.users.first();

    message.channel.send(`You wanted to kick: ${taggedUser}`);
}
});


client.login(token);

预期的输出应该是您需要标记一个用户,以便在使用?kick命令时将其踢出我的机器人。

4 个答案:

答案 0 :(得分:0)

在您的代码上:

message.mentions.user.size

正在尝试读取用户的属性 size ,即提及内容,即消息内文。 如果消息,提示和用户不存在,则没有要读取的 size 属性,您将无法读取不存在的属性。 您可以事先检查是否存在:

if(message.mentions.user) {
    if (!message.mentions.user.size) {
        return message.reply('you need to tag a user in order to kick them');
    }
}

答案 1 :(得分:0)

在第38行,您似乎看到了一个简单的印刷错误。

user(单数)不是MessageMentions的有效属性。正确的属性是users(复数),您可以在稍后的几行中正确使用它。

答案 2 :(得分:0)

如果您一次只踢一个人,也许您可​​以使用它。

if(!message.guild) return; // Only runs if in a server
const member = message.mentions.members.first(); // This gets the first guild member, not user.
if (member && message.member.hasPermission('KICK_MEMBERS')) {  // Only tries to kick them if they exist and have permission
  try {
    member.kick(args.slice(1).join(' '); // Gives reason, if any and kicks them.
  } catch (err) {
    console.error(err);
  }
} else {
  message.channel.send(`You tried to kick: ${args[0]}`);
}

答案 3 :(得分:-1)

您应将此用于kick命令!

client.on("message", (message) => {
  // the cmd  >>        if (message.content.startsWith("{prefix}kick")) {
  //permission set to kick members only >>    if (!message.member.hasPermission(["KICK_MEMBERS"])) return message.channel.send // not enough perms message >("this bot Found An Error! Error: You Do Not Have Kick Members Permission!")
  // >> this is used to define @mentions    const member = message.mentions.members.first()
  if (!member) {
    return message
      .reply
      // A User didnt mention someone so thy get this error>>      (`the bot Found An Error! Error: A User Was Not Mentioned!`)
      ();
  }
  // if the bots role doesnt have their role above the user>>  if (!member.kickable) {
  return message.reply(
    `bot Found An Error! Error: Permission Is Not Above The User, Try Again When This Is Fixed!`
  );

  return (
    member
      // >> sucess kick message    .kick()
      .then(() => message.reply(`bot Kicked ${member.user.tag} Sucessfully!`))
  );
  // error bot message >>     .catch(error => message.reply('Error!  Please Try Again!`))
});