Miki_hero
Miki_hero

Reputation: 33

Discord.js v13 why is this code not working?

I wanted to make my bot say whatever I tell it to by using a command. I have the code copied from other parts of my bot and it is working there yet here it just doesn't and I can't understand why. There are no error messages. Here's my code:

const { Permissions } = require('discord.js');

module.exports = {
    name: 'say',
    description: "Sprawia że coś mówię.",
    execute(message, args)
    {
        let text = args.join(" ").slice(22);
        if(text)
        {
            message.channel.send(text)
        }
        else 
        {
            message.reply('test')//show correct usage
        }
    }
}

(const {Permissions} will be used later so don't worry about it being there) Also you can see what it looks like on discord: here.

Upvotes: 1

Views: 421

Answers (1)

Cannicide
Cannicide

Reputation: 4520

The code is working exactly how you have programmed it. Take a look at this line:

let text = args.join(" ").slice(22);

Assuming your args in the test case "rp say xd" are just supposed to be "xd", the above code is entirely wrong. The code combines the args, separating them with spaces, and then gets a substring of that starting from the 22nd character. In other words, for text to be anything other than an empty string, your args would have to be over 22 characters long ("rp say xdddddddddddddddddddd!" would make text equal just the ending "!").

Because text is always an empty string for args under 22 characters long, you are basically just doing if (""), which is the same as doing if (false). This means your else statement will always run, unless your args are over 22 characters long combined.

To fix this, do something like this instead:

let text = args.join(" ");

Or, if you meant to make your bot only use the first 22 characters of args provided to it:

let text = args.join(" ").slice(0, 22); //.slice(startIndex, endIndexExclusive)

Upvotes: 3

Related Questions