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

Pru*_*tis 6 javascript node.js discord.js

我正在 discord.js 中创建一个机器人。如何发出重新启动机器人的命令?

Blu*_*her 12

您可以使用该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>));
}
Run Code Online (Sandbox Code Playgroud)

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

client.on('ready', () => {
    console.log('I am ready!');
});
Run Code Online (Sandbox Code Playgroud)

  • 这在以后的更新中不起作用,因为 client.destroy() 会杀死整个客户端并返回。因此,“client.login()”永远不会被调用,并且客户端将保持销毁状态。 (4认同)