【问题标题】:(Node.js) Discord bot won't ready(Node.js) Discord 机器人不会准备好
【发布时间】:2020-09-27 23:34:15
【问题描述】:

这是迄今为止我在开发 Discord 机器人时遇到的最奇怪的问题。该机器人使用 Discord.js。 我使用一个简单的client.on('ready', () => {}) client.login(<token goes here>) 东西,默认。但不知何故,控制台没有给我一个错误,也没有任何日志,但它也没有做任何事情。请看我的代码:

const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
//-------------------------------------------
const UserDataModel = require(`./data/userdata`);
const InventoryModel = require(`./data/inventory`);
const { connect } = require(`mongoose`);

client.on('ready', () => {
    console.log('Bot online!');
});

client.on('message', async (message) => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(' ');
    const command = args.shift().toLowerCase();
    //more code down below
}

(async () => {
    await connect(`mongodb://localhost/discord-bot`, {
        useNewUrlParser: true,
        useFindAndModify: false,
        useUnifiedTopology: true
    });
    client.login(token);
});

如您所见,我使用 MongoDB 进行数据存储。也许这是一个配置问题,但经过大量时间搜索后,我不知道如何解决这些问题。非常感谢任何类型的帮助!如果需要额外的代码,请注释您需要的部分。

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    你有一个永远不会执行的匿名用户

    (async () => {
        await connect(`mongodb://localhost/discord-bot`, {
            useNewUrlParser: true,
            useFindAndModify: false,
            useUnifiedTopology: true
        });
        client.login(token);
    })();
    

    应该工作

    【讨论】:

    • 谢谢!有效。你能解释一下这些匿名的东西是如何工作的,所以如果我遇到另一个问题,我可以解决这类问题吗?
    猜你喜欢
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 2021-04-01
    • 2021-02-02
    相关资源
    最近更新 更多