How to send a message on deleteing a message after bot restarting? - discord.js

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.

Related

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()
}
}

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 detect when a user joins a voice channel?

I am currently trying to make a bot for my server and one of the things I want to do is have it detect whenever a user joins any voice channel and simply make it send a message. I can't personally figure it out or find any answers on the internet as most of the time people are detecting it based on a command while I want it to be passive. I know that voiceStateUpdate has been changed and some things are different from how I've seen others use it in the past.
If anyone has any solutions please let me know, thanks.
client.on('voiceStateUpdate', (oldState, newState) => {
if (newState.channelID === null) console.log('user left channel', oldState.channelID);
else if (oldState.channelID === null) console.log('user joined channel', newState.channelID);
else console.log('user moved channels', oldState.channelID, newState.channelID);
});
In discord.js v.12 the listener you need to use is indeed voiceStateUpdate. It has the parameters oldState and newState. With those you can detect a number of things including the member object.
Using that you might use something like this to detect if a user or bot is connecting or disconnecting a voice channel.
client.on('voiceStateUpdate', (oldState, newState) => {
// check for bot
if (oldState.member.user.bot) return;
// the rest of your code
})

Discord bot that sends an message to a channel whenever a message is deleted. 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}`);
});

How to get a Discord bot to send a message to a specfic channel when created?

I'm making a bot which on a react to a certain will create a channel...
That all works perfectly expect I want a nessage to be posted when the cahnnel is created which has a specfic beginning.
client.on('channelCreate', (channel, message) => {
if(channel.name.startsWith('ticket-')){
message.channel.send('test');
});
I'm not getting any errors, just nothing...
You can't use the message variable in the channelCreate event. The only thing you're receiving is a channel object, so you need to use channel.send():
client.on('channelCreate', (channel, message) => {
if(channel.name.startsWith('ticket-')){
channel.send('test');
});

Resources