Skip to content

My Discord bot does not respond to my messages

I am making a bot using Python. I am very new to coding with Python and I don’t understand a lot of things, I am just following a tutorial(https://youtu.be/j_sD9udZnCk) but I am stuck with my bot not responding to my messages. It goes online and offline as intented but it does not respond to my messages. Also it is an administrator in my Discord server. This is my code:

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

const client = new Discord.Client();

const prefix = '-';

client.once('ready', () =>{ 
    console.log('Money Farmer 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.sendMessage('pong!');
    }
});


client.login('My token');

Answer

Try using the following code to fix your problem:

const args = message.content.split(' ').slice(1);
const command = message.content.split(' ')[0].slice(prefix.length).toLowerCase();

First you split() the the message and then you slice() the first element for your args variable. To get the command you split the message.content and take the first element of the Array. Then you slice the prefix from the Array and toLowerCase() the command.