how to detect when a message gets edited - javascript

Hi so I'm trying to make a discord bot that deletes certain words I've managed to do this but I want to make it so it will delete edited messages
This is what I've got so far
client.on('messageUpdate', message => {
if(config.FILTER_LIST.some(word => message.content.toLowerCase().includes(word))) {
message.delete()
}
})
But it won't delete the messages

See this cool doc page.
You can use client.on('messageUpdate'), which is triggered every time a message is edited.
client.on('messageUpdate', (oldMessage, newMessage) => {
newMessage.delete();
}
Disclaimer: this only works for cached messages, which means that your bot will only have access to the messages it was notified of when it was online. There is no way for the bot to have access to messages that were sent when it was offline.

Related

Discord.JS Message in a specific Channel

So i'm new to the whole Discord.JS thing and i am trying to figure out how to get the bot to write a message to the General Chat when a new person joins.
I have seen a lot of people doing it like this:
const channel = client.channels.cache.find(channel => channel.name === channelName)
channel.send(message)
But this isnt working for me. Whenever i am trying to send a message with the channel.send(message) it gives me a error message.
I have also tried the version where you do it with the client.channels.cache.get(<Channel-ID>)
But this also didnt work for me.
Use this code similar to the post: how to send a message to specific channel Discord.js
<client>.channels.fetch('<id>').then(channel => channel.send('<content>'))
Example
client.channels.fetch('922880975074127872')
.then(channel => channel.send(`Welcome, ${user}`))

Discord bot reacts when specific user is tagged

I'm new to programming and i'm trying to make a Discord bot for me and my friends, for now I have set up two commands, the first works fine, but the second one is the problem.
client.on('message', msg => {
if (msg.content === `#usertag#0001`) {
msg.channel.send(`<#${msg.author.id}> <#${msg.author.id}> <#${msg.author.id}> <#${msg.author.id}> don't tag me`);
}
});
So what it should do is when a specific user is tagged it sends a message, the message sent from the bot is working (it tags the user who sent the message 3 times and then it says something else), but the part where the bot recognizes the tag doesn't work.
I tried by putting the Discord id, using the full #, and other stuff, but it doesn't seem to work.
For this you could use the message.mentions method, and here is how you could use it for example:
// For Discord V13 'message' is deprecated and
// will be removed in the future, use 'messageCreate' instead.
client.on("message", msg => {
// Disable messages from bot to avoid endless loops / replies
if(msg.author.bot === true) return;
// Check if anyone is mentioned.
// Skip #everyone mentions to be checked.
if(msg.mentions.users.size > 0 && msg.mentions.everyone === false) {
if(msg.mentions.users.find(user => user.id === "ENTER USER ID HERE")) {
msg.channel.send(`<#${msg.author.id}> <#${msg.author.id}> <#${msg.author.id}> <#${msg.author.id}> don't tag me!`);
}
}
});
Hope this will help you! ! I have tested this with Discord V13, i'm not 100% sure if this will work on Discord V12 as well.

How do I create a discord.js command in index.js [The bot's startup file] that deletes the message when a message that contains "hi" was sent

I've tried to create a command where the bot waits for a message that was sent until a message contained "thebotdeletesthismessage", the bot doesn't seem to do anything, the language I've used is node.js.
The bot doesn't really responds neither delete the message, I've tried to change the code a little bit, Still nothing, I need to know what I've done wrong here.
If you didn't understand something, Please let me know in the comments, And please don't close this topic, I really need help.
bot.on('message', message => {
if (message.content.starts("thebotdeletesthismessage")) {
message.delete
message.channel.send('No, dont send that.');
}
});
Change message.delete to message.delete().
First, there is no .starts() method, you probably want to use .startsWith(). Also, message.delete is not a property but a method, so you'll need to use parentheses after that.
The following code will delete every message that starts with "thebotdeletesthismessage" and sends a message instead:
bot.on('message', (message) => {
if (message.content.startsWith('thebotdeletesthismessage')) {
message.delete();
message.channel.send("No, don't send that.");
}
});

messageDelete event and partials discord.js v12

Lets say that I have a messageDelete event that I want to get the data from. Since I am required to have the Channel and Message partials for the messageDelete event to fire, the message object returned in the messageDelete event will be partial.
When I'm trying to fetch this message, it returns an error saying that the fetched message is unkown.
So how can I get information like the content etc. from the deleted message?
My current code:
client.on("messageDelete", async message => {
if (message.partial) await message.fetch() // this is where the error occurs
console.log(message.content) // will only work on non partial messages
})
Is there any way around this, cause it would be useful to get the information from past deleted messages.
EDIT
Toasty recommended that I use the audit logs, to which I have used the following code
client.on("messageDelete", async message => {
console.log(message) // seeing whats avaliable in the return
if (message.partial) console.log("message was partial") // checking if the message was partial to compare with non partial messages
if (message.guild) {
const fLogs = await message.guild.fetchAuditLogs({limit:1, type:"MESSAGE_DELETE"}) //getting audit logs
const log = fLogs.entries.first()
let {executor, target} = log
console.log("Message deleted by "+executor.tag+" in "+target) // responding.
}
})
Output:
message was partial
Message deleted by CT-1409 "Echo"#0093 in 606323576714559489
So I can get the who and the (sort of) what of the message that was deleted.
I still cannot get the rest of the message information, as if I tried to fetch the message with the target id, it would give me Unkown Message again. But also when I logged the message object to start with, I noticed that there was a decent amount of information already present, which may mean some data would still be accessible from a partial message. I don't know how much, but perhaps enough for what I need.
That you can't get any information of the deleted message is probably because the message..has been deleted.
As you can read here, this is not possible and would also be a violation of the rules and a violation of the privacy of users.
But...
...if you have a command for deleting messages, you could get the information of the message before you delete it and do stuff with it
Alternatively, you could work with the audit logs

Discord Bot Development: How do I stop this infinite loop?

This calls and retrieves a dog url from random.dog, when posting the link to log it stops at one, however when using message.channel.send below it runs an infinite loop of the call, what would be the best way to prevent this and to only send one link then stop till it is called again?
const animals = require('relevant-animals')
client.on("message", (message) => {
if(message.content.includes("dog")){
animals.dog().then(s => message.channel.send(s)) ;
animals.dog().then(s => console.log(s)) ;
};
Below is console log after one request it sends one link
Below is after it is sent to the channel, it just posts links non stop rather than just the one as shown in the console
Your bot is responding to itself. You can exclude it for replying to itself by using message.author.bot.
if(!message.author.bot) {
// Do something if message doesn't come from a bot.
}
I hope this code will help you getting on the right path, good luck!
You could just do this:
if(message.author.bot) return;
This wouldn't only stop bot from executing commands etc. on itself, it would prevent any bot from using your bot.
Since it checks for author of message if bot property returns true it would return;.
And if bot property returns false it would work as normally - only for users like you and me, and many others!
You can try this by doing the following code:
console.log(message.author.bot);
It will log the boolean value of bot property of the messages author.
You can read more about booleans (true/false) here.

Categories

Resources