【问题标题】:Discord bot not coming online?Discord 机器人没有上线?
【发布时间】:2021-04-01 09:47:18
【问题描述】:

我最近做了一个不和谐的机器人,但它不会上线没有错误也知道为什么这是我的代码。

const TOKEN = "MyBotsToken";
const fs = require('fs')
const Discord = require('discord.js');
const Client = require('./client/Client');
const {
    prefix,
    token,
} = require('./config.json');

const client = new Client();
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

console.log(client.commands);

client.once('ready', () => {
    console.log('Ready!');
});

client.once('reconnecting', () => {
    console.log('Reconnecting!');
});

client.once('disconnect', () => {
    console.log('Disconnect!');
});

client.on('message', async message => {
    const args = message.content.slice(prefix.length).split(/ +/);
    const commandName = args.shift().toLowerCase();
    const command = client.commands.get(commandName);

    if (message.author.bot) return;
    if (!message.content.startsWith(prefix)) return;

    try {
        if(commandName == "ban" || commandName == "userinfo") {
            command.execute(message, client);
        } else {
            command.execute(message);
        }
    } catch (error) {
        console.error(error);
        message.reply('There was an error trying to execute that command!');
    }

    console.log("bot is ready for use")

    bot.login(MyBotsToken);
});

我的机器人也在使用 node.js 和 javascript。我也在 cmd 中尝试过 node index.js。没有其他的 没有其他的 没有其他的 没有其他的 没有其他的 没有其他的 没有其他的 没有其他的 没有其他的 没有其他的 没有其他的 没有其他的 没有其他的 没有其他的 没有别的了

谢谢 Extremepro999

【问题讨论】:

    标签: javascript node.js discord


    【解决方案1】:

    这里的问题是您的 login 操作在您的 onMessage 事件中。

    这意味着它只会在您的机器人检测到消息时使用。由于它不会上线它无法检测到消息等等......

    好消息是,您只需将bot.login(MyBotsToken); 放在onReady 事件之外即可解决此问题。您还需要做的是在您的客户端对象上使用.login()

    所以它应该看起来像这样。

    client.on('message', message => {
        // your code
    })
    
    client.login(MyBotsToken);
    

    【讨论】:

      【解决方案2】:

      bot.login(MyBotsToken) 应该在client.on('message', async message => {}); 之外

      【讨论】:

        猜你喜欢
        • 2021-11-08
        • 1970-01-01
        • 2020-12-27
        • 2021-04-21
        • 2021-05-25
        • 2018-07-22
        • 2023-02-10
        • 2021-10-06
        • 1970-01-01
        相关资源
        最近更新 更多