[英]How do you make a command which restarts your bot in discord.js?
I'm making a bot in discord.js.我正在 discord.js 中创建一个机器人。 How do I make a command that restarts the bot?
如何发出重新启动机器人的命令?
You can reset a bot by using the client.destroy()
method, then calling .login
after again.您可以使用
client.destroy()
方法重置机器人,然后再次调用.login
。 Try something like this:尝试这样的事情:
// 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>));
}
If you set a ready listener in your bot, you will see that the ready
event fires twice.如果您在机器人中设置了就绪侦听器,您将看到
ready
事件触发了两次。 I set up a ready listener like this:我像这样设置了一个现成的监听器:
client.on('ready', () => {
console.log('I am ready!');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.