简体   繁体   English

怎么修? 由于值错误,Discord.js Bot 不断脱机

[英]How to fix? Discord.js Bot keeps going offline because of Value error

I keep getting this error我不断收到此错误

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 

I have checked for errors and compared it to the sample code.我检查了错误并将其与示例代码进行了比较。 It all looks right to me.这一切对我来说都是正确的。

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);

the expected output should be you need to tag a user in order to kick them from my bot when I use the ?kick command.预期的输出应该是您需要标记用户,以便在我使用 ?kick 命令时将他们从我的机器人中踢出。

On your code:在您的代码上:

message.mentions.user.size

Is trying to read the property size of user, that is inside mentions, that is inside message.正在尝试读取用户的属性大小,即内部提及,即内部消息。 If message, mentions and user don't exist, there's no size property to be read, you cannot read a property of something that does not exist.如果消息、提及和用户不存在,则没有要读取的大小属性,您无法读取不存在的内容的属性。 You can check beforehand if it exists:您可以事先检查它是否存在:

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

On line 38, you have what appears to be a simple typographical error.在第 38 行,您有一个看似简单的印刷错误。

user (singular) is not a valid property ofMessageMentions . user (singular) 不是MessageMentions的有效属性。 The correct property is users (plural), which you use correctly a few lines later.正确的属性是users (复数),您在几行之后正确使用它。

Perhaps you could use this, if you only need to kick one person at a time.也许你可以使用这个,如果你一次只需要踢一个人。

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]}`);
}

You should use this for kick command!您应该将它用于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!`))
});

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

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