Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda