Skip to content
Advertisement

How do I delete a user’s new message

I’m pretty trash at coding so I need a little bit of help. I’m trying to code my discord bot to delete someone’s messages for one minute after they click a react emoji. It sounds simple but for my tiny pea brain, it’s not. This is what I have got so far. It deletes all messages from different users and guilds it’s in, forever. I want it so it only delete messages in one channel for one minute.

  client.once('message', async userMessage => {
    if (userMessage.content.startsWith('')) 
      {
         botMessage = await userMessage.channel.send('Who here likes goats?')
            await botMessage.react("👍") 
            await botMessage.react("👎") 
        const filter = (reaction, user) => {
                return (
                  ["👍", "👎"].includes(reaction.emoji.name) && user.id === userMessage.author.id
                );
         };
              botMessage
              .awaitReactions(filter, { max: 1, time: 60000, errors: ["time"] })
              .then((collected) => {
                const reaction = collected.first();
        
                if (reaction.emoji.name === "👎") {
                userMessage.channel.send(`${userMessage.author}, how dare you. I guess no on here likes me. Hmmm, because of that I shall now eat all your messages! BAAAAAHAHAHHAHAHA!`)
                setTimeout(() => {
                  client.on("message", async msg => {
                    if (author.msg.content.startsWith("")) {
                        userMessage.channel = await msg.delete();
                    }
                });
                }, 2000);
                } else {
                  userMessage.reply("Thanks!");
                }
              })
              .catch((_collected) => {
                userMessage.channel.send("Hehe")
              });
      }
  }); 

Btw, the code is in discord.js!

Advertisement

Answer

Your problem is this chunk of code

setTimeout(() => {
   client.on("message", async msg => {
      if (author.msg.content.startsWith("")) {
         userMessage.channel = await msg.delete();
      }
   });
}, 2000);

This is not how you use events.

A) Your message event is nested within another which could cause memory leaks.

B) To get the content you need to use msg.content, author.msg Is not a thing.

C) I assume your intention here: msg.content.startsWith("") is to always fire the if statement, in that case why not do if (true).


Here’s how I would do it:

Create a Set in the namespace which will hold id’s of users who’s messages should be deleted

const toDelete = new Set();

If they react with a 👎 add them to the set.

if (reaction.emoji.name === "👎") {
   userMessage.channel.send('Your message here');

   if (!toDelete.has(userMessage.author.id)) {
      toDelete.add(userMessage.author.id);
   }
}

On each message event check if the author of the message has their id in the set, If so delete their message

client.once('message', async userMessage => {
    if (toDelete.has(userMessage.author.id)) {
       return userMessage.delete()
          .catch(console.error);
    }

    if (userMessage.content.startsWith('')) {
       // Rest of your code
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement