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?
Related
I want my bot to send the message "first" every time a channel is created
the closest I could get was at Discord JS : How can ı send message when create channel by bot but I don't want to that he sends the message only on channels he created himself, but on channels created by anyone
You can use the channelCreate event.
Here's a simple example for you:
client.on("channelCreate", async (channel) => {
const channelName = channel.name;
console.log(`New channel created: ${channelName}`);
});
I am looking to send a message to my logging channel when a user types something important (for a game server) what I have now doesn't work and just spams messages for some reason. if you know how to fix this and make it work and not spam messages, that would be good. (no errors in console)
client.on('message', (message) => {
if (message.content.startsWith("####")) return; {}
const logChannel = client.channels.cache.get('783009285709496371');
logChannel.send(`User: <#${message.author.id}> | Message: ${message.content}`);
})
Invert the return condition. Right now you are sensing all messages that dont start with #####.
Put a ! in front of the content check.
userID = "7645876345"
client.on("message", function(message) {
if (message.author.id === userID) {
message.react('test');
}
});
I'm trying to get the discord bot to reply to a SPECIFIC user every time he writes something.
I changed the userID to random numbers for this post.
I figured it out! I replaced message.react with message.channel.send()
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}`);
});
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);
})