Problems with messageDelete in Discord js - javascript

I try to make my bot on Discord server. Want to make a function, which will copy all deleted message in text channel, but, messageDelete hear only deleted message which was writing after bot start. When I delete message which make earlier bot start, its not work.
{
client.on ("messageDelete", messageDelete =>{
let channel = client.channels.find(channel => channel.name === 'log-deleted-message')
console.log(`Deleted :${messageDelete.content}`)
channel.send(`${messageDelete.author.username} write : ${messageDelete.content}`
})
}

The above answer is now outdated.
With Discord.js V12 you can now Cache messages on the messageDelete event, You would need to enable Partials. Once this is enabled, you can fetch the message beforehand like this:
if(message.partial){
let msg = await message.fetch()
console.log(msg.content)
}
That will then log the content of the previously uncached message.

messageDelete is an event that is called when a message is deleted while the bot is on. If a message is deleted before the bot is turned on there is no way to recover it, which is why it's known as deleted. The only way to accomplish the goal that you want is to leave the bot on permanently. Read more in the docs if you want more information.

Related

Discord.js || Can bot track ban list?

There are a lot of to ban a user in a server, you can do it manually or with another bot. I want my bot to scan a banlist, and if someone will get banned, it will send a message "user (id) has been banned
I started writing smt but now, I had no idea what can I do to check it.
EDIT: It's working now, but it still need to find this user id.
client.on('guildBanAdd', message => {
client.user.cache.find() //idk how to define it
const channel1 = client.channels.cache.find(channel => channel.id === "158751278127895");
channel1.send('12512');
})
You don't have to fetch bans because the guildBanAdd event parses a GuildBan object,which als includes a User object.
client.on('guildBanAdd', ban => {
console.log(ban.user.username); // Username
})
message.channel won't work because there isn't a message object.
If you want to get the executor (moderator) who performed the ban, kick etc.
There is a good guide for this Working with Audit Logs

Delete messages in a specific channel using channel id

I'm trying to make a command for my discord bot that sends embeds in different channels(using the id) and deletes the message that was sent before. I've googled a lot and I still can't find a working solution on how to delete messages in a specific channel using the channel ID. I only need 1 message to be deleted if that's easier but I don't know.
This is my code:
client.channels.cache.get('channelid').messages.fetch().then(message => message.delete())
client.channels.cache.get('channelid').send({ embeds: [FAQEmbed] }); // sends FAQ Embed
I replaced the id with channelid in case of anything. The embed works and it sends, but the message before doesn't get deleted.
Yes your code correct, but if you want to delete the message when bot replied.
use message.channel.id and message.id to return message and channel id
example:
client.channels.cache.get(message.channel.id).messages.fetch(message.id).then(message => message.delete())
hope this help you, but you need to defined the client as well

How to send a message to another Discord server on a specific channel? [duplicate]

I'm failing to achieve something very simple. I can't send a message to a specific channel. I've browsed trough the documentation and similar threads on stack overflow.
client.channels.get().send()
does NOT work.
It is not a function.
I also do not see it as a method on the Channel class on the official documentation yet every thread I've found so far has told me to use that.
I managed to have the bot reply to a message by listening for a message and then using message.reply() but I don't want that. I want my bot to say something in a specific channel in client.on('ready')
What am I missing?
You didn't provide any code that you already tested so I will give you the code for your ready event that will work!
client.on('ready', client => {
client.channels.get('CHANNEL ID').send('Hello here!');
})
Be careful that your channel id a string.
Let me know if it worked, thank you!
2020 Jun 13 Edit:
A Discord.js update requires the cache member of channels before the get method.
If your modules are legacy the above would still work. My recent solution works changing the send line as follows.
client.channels.cache.get('CHANNEL ID').send('Hello here!')
If you are using TypeScript, you will need to cast the channel in order to use the TextChannel.send(message) method without compiler errors.
import { TextChannel } from 'discord.js';
( client.channels.cache.get('CHANNEL ID') as TextChannel ).send('Hello here!')
for v12 it would be the following:
client.on('messageCreate', message => {
client.channels.cache.get('CHANNEL ID').send('Hello here!');
})
edit: I changed it to log a message.
This will work for the newest version of node.js msg.guild.channels.cache.find(i => i.name === 'announcements').send(annEmbed)
This should work
client.channels.fetch('CHANNEL_ID')
.then(channel=>channel.send('booyah!'))
Here's how I send a message to a specific channel every time a message is deleted. This is helpful if you'd like to send the message without a channel ID.
client.on("messageDelete", (messageDelete) => {
const channel = messageDelete.guild.channels.find(ch => ch.name === 'channel name here');
channel.send(`The message : "${messageDelete.content}" by ${messageDelete.author} was deleted. Their ID is ${messageDelete.author.id}`);
});
Make sure to define it if you're not using messageDelete.

How can I make a command to delete all channels?

I am making a discord bot in discord.jsV12 that auto sets up a server. For this to happen i need the bot to delete all channels. I have tried msg.guild.channels.forEach(channel => channel.delete())
But nothing happens and I get 0 errors.
What you attempted is actually the solution.
You can use it on the message object, which means it's an action after you make a message, like so:
client.on('message', message => {
if (message.content === '.deleteChannels') {
message.guild.channels.forEach(channel => channel.delete());
}
});
triggered by making a discord message containing ".deleteChannels"
Make sure your bot has permissions to delete channels as well.
Be aware that this is a reply to a message. Otherwise, message would not be defined.
discord.js v12+ uses Managers, which means you'll have to go through an extra step and pass through the cache property.
msg.guild.channels.cache.forEach(channel => channel.delete());
Guide to switch from v11 to v12
More info about updating

use react() from DM

I want to use .react() from DM way. But the reaction into the post is sended by bot. I need send a reaction by the user.
var guild = bot.guilds.get(GUILD_ID);
if(guild && guild.channels.get(ch_ID)){
guild.channels.get(ch_ID).fetchMessage(msg_ID).then(message => {
message.react('🤔')
})
}
As I shared in a comment...
A client cannot add a reaction as another user.
Unfortunately, that means that you can't accomplish what you're trying to do within the Discord API. You can read reactions, but the bot simply would never be able to force someone to add a reaction.

Categories