在 discord.js 中处理来自 noblox.js 的 [object Promise]

Handling [object Promise] from noblox.js in discord.js

在 google...

上很难找到这个

所以我正在用 discord.js 制作一个 discord 机器人,并将 noblox.js 机器人连接到它。

const nbx = require('noblox.js');
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');

const prefix = '!';

const client = new Client({ intents: [ Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES ] })
 
async function payout(userId, amount) {
    await nbx.setCookie('roblox cookie');
 
nbx.groupPayout(12845526, userId, amount, false);
console.log("paid " + amount + " robux to " + userId)
}

async function getUser(userId) {
    let username = await nbx.getUsernameFromId(userId);
    return username;
}
 
client.on("ready", () => {
 
    console.log("Bot is ready");
 
});
 
client.on("messageCreate", (message) => {
    if (!message.content.startsWith(prefix)) return;

    const args = message.content.trim().split(/ +/g);
    const cmd = args[0].slice(prefix.length).toLowerCase(); // case INsensitive, without prefix

    if (cmd === 'test') message.reply('Bot is online!');

    if (cmd === 'payout') {
        if (!args[3]) return message.reply('Correct usage: Group, Player, Amount');

        const username = getUser(args[2]);
        
        message.reply('Paid ' + args[3] + " robux to " + username);

        //payout(userId, amount);
      }
    
});
 
client.login(token);

付款已评论,因为我还不想评论,我只是想让消息传递正常工作。

当我输入 !payout 命令时不和谐的输出:

你必须 await getUser 因为它是一个异步函数并且 returns 一个承诺

const username = await getUser(args[2])

注意:您必须将 messageCreate 回调更改为异步