简体   繁体   中英

(Node.js) Discord bot won't ready

This is by far the weirdest problem I ran in so far developing my Discord bot. This bot uses Discord.js. I use a simple client.on('ready', () => {}) client.login(<token goes here>) thing, the default. But somehow, the console doesn't give me an error, nor any logs, but it also doesn't do anything. Please take a look at my code:

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

As you can see, I use MongoDB for the data storage. Maybe it is a config problem, but I don't know how to fix such problems after much time spent on searching around. Any type of help is much appreciated! If additional code is needed, comment which part you need.

You have an anonymous which never executes

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

Should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM