How to make a discord.js bot repeat a user's message without including the prefix and command name?

Viewed 10703

Okay this most likely has a really simple answer but I couldn't find the same thing online and I can't figure it out on my own thinking.

So in the args for responding to a prefix, i have this:

case 'say':
   const usermsg = message.content
   message.channel.send(usermsg)
break;

Since it's the entire contents, it responds with the c!say too then it triggers itself. But the triggering isn't the issue, I want the c!say not included in the message. (I know I don't need the const for this, I just wanted to experiment different combinations of stuff in a separate line)

Update:

So I found a second method of approaching this by using the arguments part, like this:

        case 'say':
                message.channel.send(args[1])
                message.delete(1)
            break;

So this does what I want but only for the second argument, meaning it doesn't work for more than 1 word. So my current thought is subtracting args[0] (the c!say phrase) from message.content.

3 Answers

Found a fix to the issue, instead of sending the arguments or subtracting text I used the replace command.

            case 'say':
                let saymsg = message.content
                message.channel.send(saymsg.replace("c!say",""))
                message.delete(1)
            break;

I can't believe I didn't remember the world replace sooner, but that's it, it works.

case 'say':
   const usermsg = message.content.split(' ');
   message.channel.send(usermsg.slice(0).join(' '));
break;

This code will take in the full user string, slice out the first argument (the command and prefix), then join the rest of the message and send it in chat.

bot.on('message', message => {    
    var sender = message.author;
    var msg = message.content.toUpperCase();
    var prefix = '>';
    var cont = message.content.slice(prefix.length).split(" ");
    var args = cont.slice(1);

    for (x = 0; x < profanities.length; x++) {

        if (message.content.toUpperCase == profanities[x].toUpperCase()) {
            message.channel.send('Hey! Don\'t say that');
            message.delete();
            return;
        }
     
    if (msg.startsWith(prefix + 'SAY')) {

        message.delete();
        var saymsg = message.content;
        message.channel.send(saymsg.replace(prefix + 'say', ''))

        }
    }
}
Related