Deleting all messages from somebody on Discord - discord

I'm trying to make a bot that will delete all messages from another bot. A bot malfunctioned and spammed a whole bunch of messages, and so I want to delete the messages, which would take absurdly long.

You can fetch all messages from the channel. Then filter it by userID and delete
In your post you said that your bot spammed the messages so this code is for removing your bot's messages
message.channel.messages.fetch().then(messages => {
const botMessages = messages.filter(msg => msg.author.bot);
message.channel.bulkDelete(botMessages);
})

Related

Can I catch discord Dyno bot message and auto reply to it?

As title, my scenario is:
Periodically checking if new messages were sent by Dyno bot to specific channel, and reply to it automatically.
Is it possible?
[update1]
Here are some cases:
select the right option based one the message sent by bot
click the right button based one the message sent by bot
[update2]
add some figures. something like these:
Any suggestion is appreciated.
Using discord.py, you can check if a message is written by a bot and reply to it with:
#client.event
async def on_message(self, message): #call the on_message() function
if (message.author.bot): #check if the author is a bot
await message.reply('Hello, bot!') #reply to the bot

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

Get previous message before trigger DiscordJS

I'm trying to get the message sent RIGHT BEFORE each time I call the bot.
Right now I'm using:
client.on('message', message => {
if (message.content === '$test')) {
message.channel.messages.fetch({ limit: 2 }).then(messages => {
lastMessage = messages.last().content
message.channel.send(lastMessage)
})
}
The bot correctly sends the message sent before the trigger phrase if I send:
hello
$test
hello
However, if I send four messages quickly:
Welcome!
$test
hello
$test
it sends:
hello
hello
when it should reply with Welcome and then hello.
How do I get the content of the message sent right before each time the bot is triggered, rather than of the second-to-last message in the channel?
You should not try and grab the previous message, rather store the messages in order.
var messages = [];
client.on('message', message => {
if (message.content === '$test') {
message.channel.send(messages[messages.length - 1]);
}
messages.push(message.content);
})
You are grabbing the most recent message from the channel, and that can provide unpredictable results when multiple messages are sent.
This instead logs every message in order. This will make sure that all messages will be added along with the processing to prevent discord messages loading faster than your discord bot application processing them.
There is a similar answered question to this one
DiscordJS Bot - How to get the message immediately before another message in a channel?

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 send a message to every channel in every guild?

similar to this question however I want to be able to send it to every channel it has access to!
inside the on message event after I verify myself by ID and the issued command I was using this code:
const listedChannels = [];
msg.guild.channels.forEach(channel => {
//get all channels
client.channels.get(channel.id).send("you like bred? (message) ");
//send a message to every channel in this guild
});
however I get the error that .send is not a function...
I have been told to use .send after getting the ID of the channels
If you are looping through all of the channels, you simpily need to send your content to the channel, which you've already gotten from msg.guild.channels.forEach(channel => {//code}).
Replace what you have inside the .forEach block with;
channel.send("You like bred? (message)");
Although this will send You like bred? (message)
If you're trying to get a response back, perhaps look at this answer that explains collecting responses via reactions to a discord message.
The following explanation pertains only to v11 (stable).
Client.channels is a Collection of Channels your bot is watching. You can only send messages to text channels, and this Collection will include DM channels as well. For this reason, we can use Collection.filter() to retrieve a new Collection of only text channels within a guild. Finally, you can iterate over the channels and call TextChannel.send() on each. Because you're dealing with Promises, I'd recommend a Promise.all()/Collection.map() combination (see hyperlinked documentation).
For example...
// assuming "client" is your Discord Bot
const channels = client.channels.filter(c => c.guild && c.type === 'text');
Promise.all(channels.map(c => c.send('Hello, world!')))
.then(msgs => console.log(`${msgs.length} successfully sent.`))
.catch(console.error);
You can use client.channels for this. Check if channel type is guild text channel and then try send a message.
client.channels.forEach(channel => {
if(channel.type === 'text') channel.send('MSG').catch(console.error)
})

Resources