簡體   English   中英

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

[英]How Exactly Does Discord.JS .awaitMessages Work?

我最近聽說了.awaitMessages中的 .awaitMessages 並且我確實搜索了它並查看了一些教程,但我仍然不確定它是如何工作的以及如何正確使用它。 如果有人能告訴我它是如何工作的以及如何使用它,我將不勝感激。 非常感謝!

我已經讓你介紹了我的男人/女人,我寫了一些示例代碼並瘋狂地評論它,解釋我為你放下的一切:)

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.
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM