I
I
itachi9072020-04-25 17:02:37
JavaScript
itachi907, 2020-04-25 17:02:37

How to implement deleting a message and remembering its contents for discord.js?

Hello, dear users of Habr! I am a beginner in Java scripting and node.js. I am writing a bot for Discord on discord.js. Initially I have this code:

client.on("message", message => {
    if(message.content.toLowerCase()==config.prefix + "cmd")
    {
    message.reply("привет, введи какое-нибудь слово, я его запомню!")
    }
})

What do I need and what do I expect? On some !cmd command, the bot writes "@*username*, hello, enter some word, I'll remember it!". The user writes some word/expression, the bot remembers it and deletes it.
After memorization, on some command, the bot issues a message with this word/expression.
At the same time, the bot must identify and delete/respond to messages only from the user who initially registered the command and worked separately from others.

Example:
!cmd
@*username*, hello, type in some word, I'll remember it!
Hey!
'message is being deleted'
!slovo
Bot: "Hello!"

=================
Someone may say that I am asking for a ready-made code. Maybe it is, but it's only optional. In some way, explanations and some kind of "instruction" will be enough for me)
Thank you in advance for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
2
2CHEVSKII, 2020-04-25
@itachi907

Well, something like that, but it's just a super-on-the-knee version, written in a minute and a half, just to give you direction.
Please, stick the "Discord" tag, and mark the solution if it helped.

const userMessageDict = {};

const prefix = '!';

client.on('message', msg => {
    if (msg.content === `${prefix}cmd`) {
        userMessageDict[msg.author.id] = null;
        msg.delete({ timeout = 0 })
           .then(() => msg.channel.send('I will remember your next message.'));
    }
    else if (msg.content === `${prefix}slovo`) {
        if ((typeof userMessageDict[msg.author.id]) === 'string') {
            msg.channel.send(userMessageDict[msg.author.id]);
        } else {
            msg.channel.send('I dont have any words remembered from you yet!');
        }
    } else if (userMessageDict[msg.author.id] === null) {
        userMessageDict[msg.author.id] = msg.content;
    }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question