Reputation: 1488
I have a Discord.js command like that:
'broadcast': (msg) => {
let message2broadcast = msg.content.substr(prefix + "broadcast ".length);
if (!msg.channel.permissionsFor(msg.member).hasPermission("ADMINISTRATOR")) {
msg.channel.sendMessage(msg.author + " | No permissions! :x:");
return;
} else {
if (!message2broadcast) {
msg.channel.sendMessage(msg.author + " | No message entered. :x:");
} else {
let tosend2 = ["`Sender:`", msg.author, "`Server:`", msg.guild.name, "`Message:`", message2broadcast];
msg.channel.guild.members.forEach(user => {
user.send(tosend2.join('\n'));
});
msg.channel.sendMessage(msg.author + " | Successfully broadcasted. :white_check_mark:");
}
}
}
I want it to run by typing
#broadcast Hi, how are you?
So it will send "Hi, how are you?" to all the guild members.
However it currently send the whole command as the message... so it send the following to all the guild's members: #broadcast Hi, how are you?
I know the problem is in this line:
let message2broadcast = msg.content.substr(prefix + "broadcast ".length);
EDIT: I tried to make it like this:
let message2broadcast = msg.content.split(' ')[1];
But this will get "Hi," only from the message entered.
I want to get the argument including the spaces.
Any help would be appreciated.
Thanks!
Upvotes: 0
Views: 3196
Reputation: 1488
The answer of " Profit " is better, but here is an alternative solution which I've figured out:
var array = msg.content.split(' ');
array.shift();
let message2broadcast = array.join(' ');
This will firstly split the empty spaces (arguments)
into a variable called array
.
After that we will call the .shift()
function to remove the first element (the command including the prefix) from the array. Now we replace the array's comma with spaces in the last line to get the final result.
Upvotes: 0
Reputation: 1167
You can use splice and join to achieve this:
let message2broadcast = msg.content.split(' ').splice(1).join(' ')
It will split the message up into an array, remove the first value and then join the remaining values.
Upvotes: 2