简体   繁体   中英

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.

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.

user (singular) is not a valid property ofMessageMentions . The correct property is users (plural), which you use correctly a few lines later.

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!

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

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