Check for the message before the last - javascript

I am trying to make my bot delete a specific number of messages, I think an if would be best for the case, but I don't know how to do this...
This is how it's supposed to work: There's a message from my bot in the channel, it's always the last one in the chat. My bot will check if the message is actually there, if it is, it will delete the message, along with the command, and then send a new message. If the message is not there, it will just delete the command and send the new message.
Here's my code for reference:
if(/* message is there */) const fetched = await message.channel.fetchMessages({limit: 2});
else const fetched = await message.channel.fetchMessages({limit: 1});
// Deletes stuff
message.channel.bulkDelete(fetched)
.catch(error => message.reply(`There was an error: ${error}`));
message.channel.send("```Open```");
How can I check if that previous message is there?

I would check if the author of the last message is your bot:
let lastTwo = await message.channel.fetchMessages({limit: 2}), // Get the last 2 messages
last = lastTwo.last(); // The last in the collection will be the last message
if (last.author.id == client.user.id) await message.channel.bulkDelete(lastTwo);
else await last.delete();
message.channel.send("```Open```");

Related

Discord.JS Message Edit

I want to edit the message over and over again.
So I made variable
this.msgRef = await channel.send({ embeds: [embedMessage] });
If I want to edit I use
this.msgRef.edit(...)
If the bot for some reason disconnects, he will lose msgReference.
Because of that, I saved it to DB.
How to create a message again because Message (object) in Discord.JS have a private constructor?
If you want to get the message again, you can simply fetch the channel's messages every time you bot connects (on the ready event for example) like so
client.on('ready', () =>{
const messages = await channel.messages.fetch()
client.msgRef = messages.find(msg => msg.author.id === client.user.id
&& msg.embeds[0]
// Etc.. Add as many conditions to make sure you choose the right message
);
})
This way, use client.msgRef.edit(...) whenever you want.

Discord.js await messages from users other than the author

The title basically says it all, Is there a way for me to await for a message from a user other than the author. Like if a user #s someone in the command can I wait for the #ed person to respond to a certain question?
Basically just set the filter so it checks if the user who sent a message is the pinged/mentioned member.
Ex.
//just get a user from the message
let mentionedMember = message.mentions.users.first();
//checks if the user who send the message is the mentioned member
const filter = (message) => message.author == mentionedMember;
message.channel.awaitMessages(filter, { max: 1 }).then((collected) => {
//Whatever the mentioned member sent
});
You can use TextChannel#awaitMessages() to listen for new messages in a channel. The first parameter of this function is a filter method, which can look something like this:
const filter = (message) => message.content === 'COWS';
message.channel.awaitMessages(filter, { max: 1 }).then((collected) => {
// someone just said COWS in the channel
});
Just as I compared the message content, you can also compare the message author. This way you can listen for a specific person to respond to a question, collect their input, and then do something with it.

DISCORD.JS How to get a specific channel ID by name?

Fairly newbie question, but my overall goal is to have a dedicated log channel the bot sends messages too. So it would be something like this
log = logstuff;
channel = [WAY OF GETTING CHANNEL ID BY NAME]
client.channels.get(channel).send(log)
All inside an async fucntion bc im using Commando.
The other answer will work but you can shorten it down a lot by doing the following:
let log = "Your Stuff";
message.guild.channels.find('name', 'channel-name').send(log)
Solving this means you just have to search the message.guild for your channel, then send your log;
let targetChannelName = "MY_SAMPLE_CHANNEL_NAME";
let log = "test payload";
try {
await message.guild.channels.find(channel => channel.name === targetChannelName && channel.type === "text").send(log)
} catch {console.log} // log any errors
When you pass a message to this snippet, it searches through the message.guild's channel collection, for a channel with the name value of targetChannelName and is a textChannel. When it finds the target channel, it sends the log content.

How do I use readline for Discord Bots?

While trying to make my Discord Bot I ran into yet another problem. I'm trying to have a way to have the owner have the bot say something in the announcement section for me so it doesn't show up as my name.
I've tried the readline interface (though I don't know if I did it correctly) and I've tried to do different ways of making a variable (const, var).
if(message.member.roles.find(r => r.name === "Owner")){
return message.reply("What would you like to say?")
const dm = message.content;
return message.reply("$dm")
I want the bot to respond with whatever I say. So, If I put in The Server Is Going Down Soon! then the bot should say that back to me (in this case).
When you call return you're ending the execution of the function that you're writing.
In this case,
if(message.member.roles.find(r => r.name === "Owner")){
return message.reply("What would you like to say?") // the current function stops
const dm = message.content; // this line never executes
return message.reply("$dm")
You will have to wait for the response from the user who I presume is responding to a command of some sort
if (message.member.roles.find(r => r.name === "Owner")) {
await message.reply("What would you like to say?"); // Send the request
// Wait for the person who sent the original message to send another message
let userMessages = await message.channel
.awaitMessages(m => m.id === message.member.id, { max: 1 });
let userMessage = userMessages.first()
// Reply with the message they sent
return message.member.reply(userMessage.content);

Referencing a DM channel

I'm trying to create a command that allows users to create their password following prompts through DM. I'm able to send a message to the user, but not able to read a message sent back with a MessageCollector because I cannot find a way to reference the DM channel.
I have tried using another instance of bot.on("message", message) here, but that creates a leak in the system causing the second instance to never disappear.
I also can't let users use a command say !CreatePassword *** because this function is linked with many others in a strict order.
Maybe I'm doing something fundamentally wrong, or approaching the problem in a bad way, but I need a way to reference a DM channel.
This is the best iteration of my code so far.
function createAccount(receivedMessage, embedMessage)
{
const chan = new Discord.DMChannel(bot, receivedMessage.author);
const msgCollector = new Discord.MessageCollector(chan , m => m.author.id == receivedMessage.author.id);
msgCollector.on("collect", (message) =>
{
// Other Code
msgCollector.stop();
// Removing an embed message on the server, which doesn't have a problem.
embedMessage.delete();
})
}
I can show the rest of my code if necessary.
Thank you for your time. I've lost an entire night of sleep over this.
I would do it like this (I'll assume that receivedMessage is the message that triggered the command, correct me if I'm wrong)
async function createAccount(receivedMessage, embedMessage) {
// first send a message to the user, so that you're sure that the DM channel exists.
let firstMsg = await receivedMessage.author.send("Type your password here");
let filter = () => true; // you don't need it, since it's a DM.
let collected = await firstMsg.channel.awaitMessages(filter, {
maxMatches: 1, // you only need one message
time: 60000 // the time you want it to run for
}).catch(console.log);
if (collected && collected.size > 0) {
let password = collected.first().content.split(' ')[0]; // grab the password
collected.forEach(msg => msg.delete()); // delete every collected message (and so the password)
await firstMsg.edit("Password saved!"); // edit the first message you sent
} else await firstMsg.edit("Command timed out :("); // no message has been received
firstMsg.delete(30000); // delete it after 30 seconds
}

Categories