Reputation: 93
I have a probleme with this part of code :
message.content = message.content.toLowerCase();
var badWords = ["some bad words"];
const rpsInsulte = ["some funny answer"]
var words = message.content.toLowerCase().trim().match(/\w+|\s+|[^\s\w]+/g);
var containsBadWord = words.some(word => { return badWords.includes(word); });
if (containsBadWord) {
if(message.member.roles.some(r=>["role1"].includes(r.name)) || message.member.roles.some(r=>["role2"].includes(r.name)) || message.member.roles.some(r=>["role3"].includes(r.name))) {
message.channel.send("My message").then(msg => {
msg.delete(10000)
});
if (message.author.bot) return;
} else {
var response = rpsInsulte[Math.floor(Math.random()*rpsInsulte .length)];
message.channel.send(response).then().catch(console.error).then(msg => {
msg.delete(10000)
});
}
}
}
Actually when an other bot delete something on discord i got this error : "TypeError: Cannot read property 'some' of null" for this part of code :
var containsBadWord = words.some(word => {
return badWords.includes(word);
});
My bot no longer works whenever a message is deleted by another bot.
There's something I can do for this issue ?
Upvotes: 0
Views: 2454
Reputation: 387
As a disclaimer, I've never used Discord.js but here's my opinion.
It seems that you know what your problem is, when a message is deleted and you try to read it, you get an error because the message is not there anymore, hence the null
in the error.
If you don't care about parsing the deleted message, then I'd assume you can just check if it's null and if so, you don't check it, like below:
// Here is where you get the message, it can either be a message or null because it was deleted.
var words = message.content.toLowerCase().trim().match(/\w+|\s+|[^\s\w]+/g);
// Only use .some on it if it's not null.
// Basically if there's a message stored in words, do the following and if not, it just gets passed over.
// If you'll notice, it looks just like the lines below where you check for if (containsBadWord).
if (words) {
var containsBadWord = words.some(word => { return badWords.includes(word); });
}
Let me know if this still does not solve your problem or if you really need to check the deleted messages even, and I'll look over the Discord.js documentation some more and see what can be done.
Upvotes: 1