Delete messages in a specific channel using channel id - javascript

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

Related

Discord js message sending to specific channel

first off all, i know there are similar threads, but i can't find a single one that uses the same method as i would like. My code:
ticketChannel.send({
embeds: supportEmbed,
components: new MessageActionRow().addComponent(supportButton).addComponent(claimButton),
message.ticketChannel.send("<#&874175844422135808> Prosím o vyřešení ticketu");
})
The last line should send a message that pings a role with that id. Well, it doesn't. The message must be sent to the ticketChannel channel.
Thanks for helping me
if you want to always send it to a specific channel, you'll need to use the id of the channel to define it like so: (assuming you have guild defined)
let channelid = "your channel id"
let ticketchannel = guild.channels.fetch(channelid)
ticketchannel.send("<#&874175844422135808> Prosím o vyřešení ticketu")

Discord.JS | Auto-Kick someone after posting a specific link?

Im trying to add some "auto-mod" features to my bot, recently I added a small command that will delete fake steam trade links (as they seem to be getting more common every day) as you can see in this image basically it token logs your discord account, then spams the same message in all of your servers. So I developed some code to auto-delete these links, and reply with a gif... but I want more, as I believe these bots don't stop spamming until they are removed from the server, so I want to add a function to auto kick them, and DM them a message about why they were kicked, and an invite back to the server (as most of these people quickly get their accounts back) sorry for the long talk, my current WORKING code is below:
client.on('message', (message) => { //whenever a message is sent
if (message.content.includes('https://stearncomminuty.ru/tradofferr/new/?partner=1284276379&token=iMNdOshG')) {
message.delete() //delete the message
.then(message.channel.send('https://media.discordapp.net/attachments/859211601726275636/859977676919013386/5f36hh.gif'))
}})
Any help or suggested improvements would be appreciated!
You can solve this really simply. Just add another .then() after your first one and call the <guildMember>.kick() function.
For example:
client.on('message', (message) => { //whenever a message is sent
if (message.content.includes('https://stearncomminuty.ru/tradofferr/new/?partner=1284276379&token=iMNdOshG')) {
message.delete() //delete the message
.then(message.channel.send('https://media.discordapp.net/attachments/859211601726275636/859977676919013386/5f36hh.gif'))
.then(() => message.member.kick('User kicked for sending spam links.'))
}
});
Also, why don't you just put https://stearncomminuty.ru/tradofferr/ in your include()? This will also delete similar fake-steam-URLs and not just a specific one because it is not unlikely that there are lots of URLs starting like this.

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.

Problems with messageDelete in Discord js

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.

How to make a bot find a channel owner / compare to the author's ID

I'm making a bot where if you do d!move, the bot will move the channel where the message was sent in under a category via ID. I also want to make it so that whoever does the command has permissions such as MANAGE_CHANNELS, which I've already added. The problem is that when I want to confirm whoever created that channel is the person that activated the command, the bot says yes. I did this on an alt account, where I made the channel and my alt was the one initializing it, and the bot said "success!" I also wanted to make it so if someone else made the channel, and when I did it, it would work because I made the bot know my ID.
I've researched Google and found nothing.
I've tried using a function with fetchAuditlog but get anywhere.
if(!message.channel.client.user.id == message.author || !message.author.id == `329023088517971969`) return message.channel.send("You don't own this channel!")
else message.channel.send("success!");
message.channel.setParent(`576976244575305759`);
I expect the bot to be able to check if the author created the channel, and lead to You don't own this channel if they don't own it. But if they do then the bot moves the channel.
The actual result is the bot moving the channel anyway regardless if they own the channel or not.
As #André has pointed out, channel.client represents the client itself, not the user who created the channel. Also, the last line in your code is not part of the else statement, so that's why it's run regardless of the conditions you defined.
To reach a solution, you can make use of the guild's audit logs. You can search for entries where the user is the message author and a channel was created. Then, all you have left is to check if one of those entries is for the current channel, and run the rest of your code if so.
Sample:
message.guild.fetchAuditLogs({
user: message.author,
type: 'CHANNEL_CREATE'
}).then(logs => {
if (!logs.entries.find(e => e.target && e.target.id === message.channel.id)) return message.channel.send('You don\'t own this channel.');
else {
// rest of code
}
}).catch(err => console.error(err));
When you go <anything>.client.user it will return the bot client.
If you want to see who created the channel you would have to check the Audit Logs or save it internally.
I've checked the documents. Here's what it says for .client about the
channel. It says the person that initialized the channel, or the
person that created it.
On documentation I see this:
The client that instantiated the Channel
instantiated is different of initialized

Categories