skip to Main Content

every time I do !ban [user], the bot crashes and I don’t know why, would be wonderful if some one could help

my code:

if (message.content.startsWith("!ban")) {
  const author = message.author
  const messageContent = message.content.split(' ').slice(1).join(' ');
  if (!messageContent) { 
    return message.reply("please tell me who you would like me to ban!")
  } else {
    if (messageContent = author) {
      return message.reply("You cant ban yourself silly!")
    }
  }
}

2

Answers


  1. Without providing the error message, it is difficult to determine what is going on. However, you are using the equality operators incorrectly.

    Currently, on the line if (messageContent = author) {, you have defined a variable inside of an if-statement.

    The correct way of doing so would as as follows:

    if (messageContent === author) {
    

    Full example:

    if (message.content.startsWith("!ban")) {
      const author = message.author
      const messageContent = message.content.split(' ').slice(1).join(' ');
      if (!messageContent) { 
        return message.reply("please tell me who you would like me to ban!")
      } else {
        if (messageContent === author) {
          return message.reply("You cant ban yourself silly!")
        }
      }
    }
    
    Login or Signup to reply.
  2. An enhacement of the code:

    const {content: messageContent, author} = message
    const [command, content] = messageContent.split(' ')
    
    if (command === '!ban') {
        if (!content) return message.reply('please tell me who you would like me to ban!')
        if (content === author) return message.reply("You cant ban yourself silly!")
        // rest of code
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search