YEET YOOT
YEET YOOT

Reputation: 17

What am I doing wrong with my discord bot

I am writing my first discord bot with help from a tutorial. I am stuck because my bot hasn't been able to respond to commands and I've checked the tutorials and my code many times over. Is there something I'm doing wrong?

const discord = require('discord.js');

const client = new discord.Client();

const prefix = '!';


client.once('ready' , () => {
  console.log('Zach Is Bad is online');
});


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

if(command === 'ping'){
    message.channel.send('pong!');
  }
});


client.login('redacted')

Upvotes: 0

Views: 279

Answers (2)

Liam
Liam

Reputation: 957

The only issue with your code is message.content.startswith(prefix).

It is startsWith, it's case sensitive.

if (!message.content.startsWith(prefix) || message.author.bot) return;

Upvotes: 1

Vincent Gonnet
Vincent Gonnet

Reputation: 148

There is a typo at client.on('ready', () => ...), but this shouldn't be the cause of the error.

You wrote if (!message.content.startswith(prefix) || message.author.bot) return; but I'm pretty sure startwith must be startWith (caps are importants). Try modifying this, then restart your bot.

If it doesn't work put console.log("test OK") right before the ping command, restart and send !ping. If there is "test OK" in your bot logs, then the problem come from the ping command. If you doesn't see this log, try moving the console.log line before the if statement. This is a simple yet effective method to know where does a problem come from.

Upvotes: 0

Related Questions