邮件收集器不值得

时间:2020-09-10 16:38:55

标签: discord.js

我正在执行支持命令:您键入命令,机器人会向您发送一条消息,然后您回复该消息。我已经使用了awaitMessages函数,但是它不起作用。

s

1 个答案:

答案 0 :(得分:1)

要使用.then(),您需要返回一个Promise。这是如何使用Promise的基本示例:

const myFunction = () => {
    return new Promise((resolve, reject) => {
        if(taskIsSuccesFullyDone)
        {
            resolve(true); // Pass anything
        }else{

            reject('Something went wrong!');
        }
    });
}


myFunction().then(() => {
    // Task is succesful completed.
    // Do anything
})
.catch(error => console.log(error.message || error));

在您的情况下,您的代码应如下所示:

function support_message() {
    return new Promise((resolve, reject) => {
        message.author.send(`Hello, <@${message.author.id}>, reply to this message explaining the problem you have.`)
        .then(message => resolve(message))
        .catch((error) => {
            message.reply("I can't send you messages, be sure that you allow direct messages from unknown users to use this command.");
            reject(error);
        })
    });
}

case `staff-support` : {
    support_message().then(message => {
        // We got the message object passed from the resolved Promise
        // Do anything here
    })
    .catch((err) => {
        // There was a problem!
        // Do anything here.
    });
    break;
}