New Coder Working code but get error messages?

SO! Another YouTube code i found got it working! but get error codes i do not yet understand?
If i can get this to work without errors i plan on splitting it into a link banner and blacklist.

const Discord = require(‘discord.js’)
const client = new Discord.Client()

client.on(‘ready’, () => {
});

client.on(‘message’, message => {
let blacklisted = [‘duck’, ‘discord.gg/’, ‘yello’];

let foundInText = false;
for (var i in blacklisted) {
if (message.content.toLowerCase().includes(blacklisted[i].toLowerCase())) foundInText = true;

if (foundInText) {
message.delete();
message.channel.send(‘Sorry, that word is blacklisted!’)
.then(msg => {
msg.delete(3000)
})
.catch();
};
};
});
client.login(process.env.BOT_TOKEN)

(node:20328) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Message
at item.request.gen.end (C:\Users\uryij\Documents\Bot\node_modules\discord.js\src\client\rest\RequestHandlers\Sequential.js:79:15)
at then (C:\Users\uryij\Documents\Bot\node_modules\snekfetch\src\index.js:215:21)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:20328) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:20328) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

This error happens, when you try to do an operation on deleted message. For example, you use message.delete() and then try again to delete same message.

In your case, you have missed } after if statement, so you try to delete message many times.

Try this code:

client.on('message', update => {
  const blacklisted = [ 'duck', 'discord.gg/', 'yello']
  const { content } = update
  const foundInText = blacklisted.includes(e => e.toLowerCase() === content.toLowerCase())
  
  if (foundInText) {
    try {
    message.delete()
    message.channel.send('Sorry, that word is blacklisted!').then(m => m.delete(3000))
    } catch (e) { 
      console.error(e)
    }
  }
})
client.login(process.env.BOT_TOKEN)
1 Like

Sorry it does not work at all? The way i had it before it deleted any and all Discord Invites.Tested all my other commands and events and the bot is working? It won’t trigger on the word yellow or duck? Reverted back to what i posted and that works but with errors? Thx’s for taking the time and trying to help.

Got some outside help and this works like a charm. I split it up into 3 different js files blacklist, discord-link-blocker, Racist-remarks. what it does is Block message (such as N-Bomb anywhere within the sentence or anything within “let blacklisted = [‘this server sucks’, ‘your server sucks’];” ), Give warning, Delete warning…

client.on(‘ready’, () => {
});

client.on(‘message’, (message) => {
let blacklisted = [‘this server sucks’, ‘your server sucks’];
\\ use discord.gg/ above to block Discord Links
if (blacklisted.some(blacklisted => message.content.toLowerCase().includes(blacklisted))) {
try {
message.delete();
message.channel.send(‘Sorry Pal!, I won’t ALLOW THAT!’).then((msg) => {
msg.delete(3000)
});
} catch (e) {

    }
};

});
client.login(process.env.BOT_TOKEN)

Big thx’s to jarvis394 for taking the time & trying to help! You Rock Brother!