javascript - Discord.JS .awaitMessages 到底是如何工作的?

标签 javascript discord.js

我最近听说了 Discord.JS 中的 .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.
    }
});

关于javascript - Discord.JS .awaitMessages 到底是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61706821/

相关文章:

javascript - 在 ionic 中使用 OneSignal 插件在通知单击时路由到应用程序中的状态

javascript - 为什么 POST 请求后会显示页面?

javascript - 如何在 Meteor 中将图像资源编码为 Base64 数据 URI?

javascript - 错误connection.play不是discordjs v13中的函数

javascript - 使用表情符号 react 加入用户语音 channel

javascript - 这个Ajax登录系统足够安全吗?

javascript - 在 Promise 中,使用 catch 和 then 的第二个参数有什么区别?

node.js - 我得到这个 "Error: FFmpeg/avconv not found!"

javascript - 类型错误 : Cannot read property 'add' of undefined Discord. js javascript

javascript - 尝试将数据库中的整数添加到 let 但总是返回 NaN