Make discord bot (js) echo message, but removing the command from the message?

末鹿安然 提交于 2021-02-11 13:50:33

问题


So what I want in the end is this:

user: "*cmd hello"

bot: "hello"

I tried replacing it, searched different ways to edit it, but nothing worked so far.

    if (receivedMessage.content.includes("*cmd")) {
    receivedMessage.content.replace('*cmd', ' ');
    receivedMessage.channel.send(receivedMessage.content);
    receivedMessage.channel.send("s/*cmd/-");
}    

(^found out that you can edit stuff by typing s/oldtext/newtext, but sadly it doesn't seem to work with the bot. Replacing didn't work, either.)

    if (receivedMessage.content.includes("*cmd")) {
    receivedMessage.channel.send(receivedMessage.content.text(text.replace('*cmd', ' '))); 
}    

(I tried more stuff, but I deleted it when it didn't work, and I don't think it'd help, anyway)

I'd really appreciate some help!


回答1:


string.replace() returns a string.
So, "This is a test".replace("This is a ", "") will return "test".

console.log("This is a test".replace("This is a ", ""));

With that in mind, this is what you need to use:

receivedMessage.channel.send(receivedMessage.content.replace('*cmd', ''));


来源:https://stackoverflow.com/questions/53838838/make-discord-bot-js-echo-message-but-removing-the-command-from-the-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!