Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不和谐机器人不上线?_Javascript_Node.js_Discord - Fatal编程技术网

Javascript 不和谐机器人不上线?

Javascript 不和谐机器人不上线?,javascript,node.js,discord,Javascript,Node.js,Discord,我最近做了一个discord机器人,但它不会上线。没有错误。我也知道为什么这是我的代码 const TOKEN = "MyBotsToken"; const fs = require('fs') const Discord = require('discord.js'); const Client = require('./client/Client'); const { prefix, token, } = require('./config.json');

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

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

这里的问题是您的
登录
操作在您的
onMessage
事件中

这意味着只有当你的机器人检测到一条消息时才会使用它。因为它不会上网,所以无法检测到消息等等

好消息是,您可以通过放置
bot.login(MyBotsToken)来修复这个问题
onReady
事件之外。您还需要在客户端对象上使用
.login()

所以应该是这样的

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

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