Sunny
Sunny

Reputation: 67

Delete an old message from user - Discord.js

I am currently making a bot that is used as a queue for people waiting in line. I am having difficulty deleting the old message that the user sent to be added to the queue (so that discord isn't cluttered).

What is the proper way to access and delete an old message from the client?

!next -> adds the person to the queue,
!remove -> should remove the first person in queue and delete the "!next" call that the user entered in chat.

client.on('message', message => {
    user = message.member;
    user = user.toString();
    idNumber = message.member.id;

   if (message.content === '!next') {
        x++;
       message.reply('Added to waiting queue. Position ' + x);
       arrayName.push(user);
       idName.push(idNumber);
       }
     else if (message.content === '!remove') {
         x--;
         arrayName.shift();
         idName.shift();
         message.reply('Removed Top User from Queue. Users Left: '  + x);
         //how to remove !next call from user that called it?
         }

Upvotes: 0

Views: 219

Answers (1)

Tarazed
Tarazed

Reputation: 2665

I would agree with Syntle in your comments, you should just delete it right away.

That said, if you really want to do this, here is the answer to the question:

You are going to need some kind of multi-dimensional storage for your queue. There are multiple options, but I am going to demonstrate with an array of objects.

if (message.content === '!next') {
    array.push({Id: idNumber, User: user, Message: message});
    message.reply('Added to waiting queue. Position ' + array.length);
} else if (message.content === '!remove') {
    let record = array.shift();
    message.reply('Removed Top User from Queue. Users Left: ' + array.length);

    if(!record.Message.deleted) {
        record.Message.delete();
    }
}

Upvotes: 2

Related Questions