你如何制作一个在 discord.js 中重启你的机器人的命令?

How do you make a command which restarts your bot in discord.js?

我正在 discord.js 制作一个机器人。如何发出重启机器人的命令?

您可以使用 client.destroy() 方法重置机器人,然后再次调用 .login。尝试这样的事情:

// set message listener 
client.on('message', message => {
    switch(message.content.toUpperCase()) {
        case '?RESET':
            resetBot(message.channel);
            break;

        // ... other commands
    }
});

// Turn bot off (destroy), then turn it back on
function resetBot(channel) {
    // send channel a message that you're resetting bot [optional]
    channel.send('Resetting...')
    .then(msg => client.destroy())
    .then(() => client.login(<your bot token here>));
}

如果您在机器人中设置了一个现成的侦听器,您将看到 ready 事件触发了两次。我像这样设置了一个现成的监听器:

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