Discord bot that sends an message to a channel whenever a message is deleted. Discord.js - discord.js

I’m trying to make my bot send a message to a channel whenever a user deletes his/her message, sorta like the bot Dyno, but I do not know how to do this. I think the method is .deleted() but I can’t seem to make it work. Can anyone tell me how? Sorry for lack of detail, there’s nothing else to add. Thank you in advance.

The Client (or Bot) has an event called messageDelete which is fired everytime a message is deleted. The given parameter with this event is the message that has been deleted. Take a look at the sample code below for an example.
// Create an event listener for deleted messages
client.on('messageDelete', message => {
// Fetch the designated channel on a server
const channel = message.guild.channels.cache.find(ch => ch.name === 'deleted-messages-log');
// Do nothing if the channel wasn't found on this server
if (!channel) return;
// Send a message in the log channel
channel.send(`A message has been deleted. The message was: ${message.content}`);
});

Related

Discordjs: Specifying channel for an interaction.reply

I am using Discordjs v13. I have created a slash command and I am able to "print" a message using
await interaction.reply(messageObj);
I need to send the reply to a different channel where the command was triggered, is this possible?
Something like:
interaction.setChannel(channelId).reply(...)
OR
interaction.reply({
channel: ....
....
})
What you want is not possible. The Discord API does not allow to specify the channel where the app interaction should be replied in: https://discord.com/developers/docs/interactions/receiving-and-responding#responding-to-an-interaction
However if you are concerned with the reply being shown to everyone, you can make the reply ephemeral. If you want to log interactions, you can reply to the interaction then send another message using the solution provided in the comments of your question.
this is actually possible in discordjs v14 and may be in v13 as well. Carl-bot does it with suggestions.but its not an actual reply
use
interaction.guild.channels.cache.get('channel-id').send('message')
this will send a message in a the select channel you may still want to
interaction.reply({Content:'replied in #channel' [embed], ephemeral: true })
so the user knows the reply was redirected . ephemeral: true makes the replay only visible to the user that evoked the interaction.
and if you need the message id for the new message use
const msg = await interaction.guild.channels.cache.get('channel-id').send('message');
to send the message and you can do something like const messageid = msg.id

Deleting a message if it contains a specific word

So im making a discord bot, i made a system so if the bot joins a server it sends a message with the server name in the developer server. but i want it to delete the message when it leaves the server. idk how to do that.
You can create a listener that is triggered whenever a user sends a message. In that listener you would check the message.content against your prohibited word(s) and delete it. You can do that like so:
client.on('messageCreate', () => {
if(message.content.includes("Your prohibited word(s)") {
message.delete()
}
}

Discord: Reading messages from one channel in a (non-admin perm) server, and posting it to another (with admin perms)

I am new to Discord API framework.
ServerFROM is a public server that I was invited to (non-admin perms). Hence I cannot add bots there. But I can view the content in ChannelFROM (a text channel in ServerFROM)
I have my own ServerTO (in which I have admin perms and so can do anything). Inside of which, I have the target ChannelTO
I want to deploy a listener on ChannelFROM, such when there is a new message (announcement) in ChannelFROM, I want it to be read and reposted in ChannelTO.
Something similar to what is done in this Stackoverflow issue, except that I cannot have some script run locally 24x7 on my machine. Maybe use Github Actions or something similar?
How can I go about doing it? Any ideas are appreciated. Maybe some form of a server, or just a custom Discord bot
And thanks in advance.
you can use Custom bot for that. I dont know other way
here's how i do it
1st : We Get the ID of the Channel that you wanted to listen
2nd : We make a Output or where the bot copy the message from and send it
3rd : Bot required a permission to view the channel and send message
or in the nutshell ( sorry if im bad at explaining )
client.on("messageCreate", message => {
if(message.author.bot) return;
if(message.channel.id === ID_HERE) // ChannelFROM in ID_HERE
{
let MSG = message.content
let Author = message.member.displayName
let Avatar = message.author.displayAvatarURL({dynamic: true})
const Embed = new MessageEmbed()
.setAuthor(Author , Avatar )
.setDescription(MSG)
.setColor("RANDOM")
client.channels.cache.get(ID_HERE).send({ embeds: [Embed] }) // SendTo in ID_HERE
}
})
you can remove if(message.author.bot) return; if you want it also read other bot message on that specific channel

How to not send bots message edit discord.js

I have a message edit log but I want to stop sending the log if a mobs message was updated, I tried a few codes like
if(bot.oldMessage.content.edit()){
return;
}
It showed and error
cannot read property 'edit' of undefined
I then removed edit then content was undefined. The code for the message update is below.
The Code
module.exports = async (bot, oldMessage, newMessage) => {
let channels = JSON.parse(
fs.readFileSync('././database/messageChannel.json', 'utf8')
);
let channelId = channels[oldMessage.guild.id].channel;
let msgChannel = bot.channels.cache.get(channelId);
if (!msgChannel) {
return console.log(`No message channel found with ID ${channelId}`);
}
if (oldMessage.content === newMessage.content){
return;
}
let mEmbed = new MessageEmbed()
.setAuthor(oldMessage.author.tag, oldMessage.author.displayAvatarURL({dynamic: true}))
.setColor(cyan)
.setDescription(`**Message Editied in <#${oldMessage.channel.id}>**`)
.addField(`Before`, `${oldMessage.content}`)
.addField(`After`, `${newMessage.content}`)
.setFooter(`UserID: ${oldMessage.author.id}`)
.setTimestamp()
msgChannel.send(mEmbed)
}
How would I stop it from sending the embed if a bots message was updated.
Making a really simple check will resolve this issue. In Discord.js there is a user field that tells you if the user is a bot or not.
In fact, it is really recommended you add this in the "onMessage" part of your code as it stops other bots from using your bot, this is to make sure things are safe and no loopbacks/feedbacks happen, either way, you don't want a malicious bot taking advantage of your bot, which can get your bot in trouble too.
Here is what you want to do;
if (message.author.bot) return;
What this code specifically does is check if the message's author is a bot, if it returns true, it will break the code from running, if it returns a false, the code continues running.
You can do the same if you want to listen to bots ONLY by simply adding a exclamation mark before the message.author.bot like this;
if (!message.author.bot) return;
It is also possible to see what other kinds of information something holds, you can print anything to your console. For example, if you want to view what a message object contains, you can print it into your console with;
console.log(message) // This will show everything within that object.
console.log(message.author) // This will show everything within the author object (like ID's, name, discriminators, avatars, etc.)
Go ahead and explore what you can do!
Happy developing! ^ -^
That is really easy to do. All you need to do is check if the author of the message ist a bot and then return if true. You do that like this
if (oldMessage.author.bot) return;

How to send a message on deleteing a message after bot restarting?

I want the bot to send a message to the channel if someone deleted a message.
It is working, but if i restart the bot and try it again it is not deleting the messages that sent before the restarting
why?
this is my code:
client.on('messageDelete', messageDelete => {
if(messageDelete.channel.id === "563966341980225536" || messageDelete.channel.name === "general"){
messageDelete.channel.send("Working !");
}
});
client.on('messageDelete', messageDelete => {
if(messageDelete.channel.id === "563966341980225536" || messageDelete.channel.name === "general"){
messageDelete.channel.send("Working !");
}
});
The above code is subscribing to an event. Assuming an API is healthy, you are only going to get one event every sent to you. So, if you have an event sent to you, and you restart the bot before it is done handling the event, nothing is going to happen. Unless you have a mechanism on your end that is queueing these events and persisting them, restarting the bot means it won't capture that messageDelete fully with the handler. This is just how events are by design.

Resources