Discord.JS .awaitMessages是如何工作的?

时间:2020-05-10 03:33:42

标签: javascript discord.js

我最近在Discord.JS中听说过.awaitMessages,我确实进行了搜索并看了一些教程,但是我仍然不确定它是如何工作以及如何正确使用的。如果有人能告诉我它的工作原理和使用方法,我将不胜感激。非常感谢!

1 个答案:

答案 0 :(得分:2)

我已经让您涵盖了我的男人/女人,我写了一些示例代码并对其进行了评论,就像疯狂地解释了我为您付出的一切:)

client.on('message', message => {

    // This just converts all messages to complete lowercase for the bot to interpret.
    const command = message.content.toLowerCase();

    // This variable simply stores the User ID of the person who sent the message.
    const messageAuthor = message.author.id;

    if (command == 'what is your name?') {

        // This is the filter we will use for the Message Collector we're going to use A.K.A ".awaitMessages".
        const messageFilter = message.author.id == messageAuthor;

        /* This is the command you asked about in all its glory. The first parameter you give it is the filter to use (although a filter isn't required if I recall correctly.).

            The next parameter is "max", this purely dictates how many messages (applying to the filter if applicable) for the collector to collect before ending (unless of course the timer runs out, which we'll touch on next).

            Second to last parameter is "time", this is, like max, pretty straightforward and dictates the amount of time the collector attempts to collect messages before ending (as stated above, if the collector-
            reaches the max for collected messages before the timer runs out it will end prematurely, however if for whatever reason the collector doesn't receive the max amount of collected messages, it will continue to attempt collect messages-
            until the timer runs out.)

            Now onto the last parameter, "errors". This parameter basically tells the collector to treat the timer running out as if it was an error, that way you can then reference said error in the ".catch" method you'll see below and give the bot-
            instructions on what to do if the time does end up running out.
        */
        message.channel.awaitMessages(messageFilter, { max: 1, time: 10000, errors: [time] })
            .then(collected => {

                // Checks to see if the message we collected is a number, and if so, responds with the message in the command below.
                if (isNaN(collected) == false) {

                    // The response for if the collected message IS a number.
                    message.channel.send(`${collected} is definitely not your name!`);
                }

                // This runs as long as the message we collected is NOT a number.
                else {

                    // The response if the collected message is NOT a number.
                    message.channel.send(`${collected} is an awesome name!`);
                }
            })
            .catch(collected => message.channel.send('You never gave me your name ):'));
            // ^ This is the ".catch" method where you'll give the instructions for what to do if the timer runs out, in this case I have it just reply to the user to tell them that they never gave me their name.
    }
});
相关问题