bot.on('message', message => {
if (message.content.startsWith('ch ')){
a = message.content
let str1= a
let str2= 'ch '
str3 = str1.split(str2)
var str4 = str3.toString()
bot.user.setActivity( { type: 'WATCHING' } , {name:str4} )
}
})
When I type "ch book", it become this :
a comma in front of the word.
How can I fix this? Thanks
Looks like you are simply trying to remove "ch " from the beginning of the string. You don't need split and arrays for that, you can just use .replace() :
bot.on('message', message => {
if (message.content.startsWith('ch ')){
bot.user.setActivity( { type: 'WATCHING' } , { name:message.content.replace('ch ','') })
}
})
You should first understand the usage of this method first. This method splits a string into an array with each element delimited by the character of your choice, here a comma.
"ch, books".split(','); // returns ["ch", " books"]
This method returns a string representation of the array, which would have comma-separated elements and you end up going back to square one!
["ch", " books"].toString(); // returns "ch, books"
As long as you need to just sanitize string by removing commas or other special characters, use this method along with RegExp.
"ch, books, copies, notebooks".replace(/,/g, ''); // returns "ch books copies notebooks"
// further remove more characters such as hyphens
"ch, books, copies, note-books".replace(/,|-/g, ''); // returns "ch books copies notebooks"