Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
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!

about 4 years ago ยท Juan Pablo Isaza
2 answers
Answer question

0

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
about 4 years ago ยท Juan Pablo Isaza Report

0

I think your problem in understanding how everything works. I took everything from discord.js documentation.

Type reaction command to see how it works.

const Discord = require("discord.js");
require("dotenv").config();
const TOKEN = process.env.TOKEN||"YOUR TOKEN";
const PREFIX = process.env.PREFIX||"YOUR PREFIX";
const bot = new Discord.Client();
bot.on("ready", async function(e) {
    console.log("Loaded!");
})

bot.on("message", async function(message) {
    if (message.author.bot) return;
    if (!message.content.startsWith(PREFIX)) return;
    let args = message.content.slice(PREFIX.length).trim().split(/\s+/);
    let command = args.splice(0, 1).toString().toLowerCase();

    if (command == "reaction") {
        message.delete();
        let msg = await message.channel.send("Click on the reaction");
        await msg.react("๐Ÿ‘");
        await msg.react("๐Ÿ‘Ž");
        let filter = (reaction, user) => {
            return ["๐Ÿ‘", "๐Ÿ‘Ž"].includes(reaction.emoji.name) && user.id == message.author.id;
        }
        msg.awaitReactions(filter, {max: 1, time: 10000, errors: ["time"]}).then(collected => {
            let reaction = collected.first();
            if (reaction.emoji.name == "๐Ÿ‘Ž") {
                return message.channel.send("downvote");
            }
            return message.channel.send("upvote");
        }).catch(e => {
            message.channel.send("user didn't vote");
        })
    }
})
bot.login(TOKEN);
about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!