I'm pretty new to the development world, I wanted to practice doing JS, and I learned that Discord bots could be done in this language, I found it cool to practice.
My problem: I want to separate the mand from the rest of the message. I managed to separate the mand from a word, but when I enter several words, it does not work. This is what it does:
(!Command HELLO" will send "Command + Hello", but "!mand HELLO HI" will not work)
const PREFIX = "!";
bot.on('message', function(message) {
if(message.content[0] === PREFIX) {
let splitMessage = message.content.split(" ");
if(splitMessage[0] === '!mand') {
if(splitMessage.length === 2) {
message.channel.send('Command + ' + splitMessage[1]);
}
}
}
});
I'm pretty new to the development world, I wanted to practice doing JS, and I learned that Discord bots could be done in this language, I found it cool to practice.
My problem: I want to separate the mand from the rest of the message. I managed to separate the mand from a word, but when I enter several words, it does not work. This is what it does:
(!Command HELLO" will send "Command + Hello", but "!mand HELLO HI" will not work)
const PREFIX = "!";
bot.on('message', function(message) {
if(message.content[0] === PREFIX) {
let splitMessage = message.content.split(" ");
if(splitMessage[0] === '!mand') {
if(splitMessage.length === 2) {
message.channel.send('Command + ' + splitMessage[1]);
}
}
}
});
Thanks
Share Improve this question edited Sep 23, 2019 at 13:42 Nompumelelo 9574 gold badges18 silver badges32 bronze badges asked Feb 20, 2018 at 16:30 THOMAS CorentinTHOMAS Corentin 231 gold badge2 silver badges4 bronze badges 2- Use indexOf() to find the first occurence of whitespace and substring() to grab everything after the first whitespace – Ryan Wilson Commented Feb 20, 2018 at 16:33
- It is not clear what should be the result for mand HELLO HI. You should send two mands? Or a mand with two parameters? – Rafael Paulino Commented Feb 20, 2018 at 16:34
2 Answers
Reset to default 0As I stated in the ment:
const PREFIX = "!";
bot.on('message', function(message) {
if(message.content[0] === PREFIX) {
let mand = message.content.substring(message.content.indexOf(" ") + 1, message.content.length);
message.channel.send('Command + ' + mand);
}
});
splitMessage[1]
that takes the second word from the splitted array. So with Command! Hello world
it will be Hello
. You might want to take everything after the first element from the split Message like so:
splitMessage.slice(1)
that returns ["Hello", "World"]
, so you just need to join it back to a string
.join(" ")
How i would do that:
const [mand, ...args] = message.content.split(" ");
switch(mand){
case "!Command":
message.channel.send('Command + ' + args.join(" "));
break;
//....
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744733434a4590600.html
评论列表(0条)