Javascript 你如何在 discord.js 中创建一个重启你的机器人的命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48601463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you make a command which restarts your bot in discord.js?
提问by Pruina Tempestatis
I'm making a bot in discord.js. How do I make a command that restarts the bot?
我正在 discord.js 中创建一个机器人。如何发出重新启动机器人的命令?
回答by Blundering Philosopher
You can reset a bot by using the client.destroy()method, then calling .loginafter again. Try something like this:
您可以使用该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>));
}
If you set a ready listener in your bot, you will see that the readyevent fires twice. I set up a ready listener like this:
如果您在机器人中设置了就绪侦听器,您将看到该ready事件触发了两次。我像这样设置了一个现成的监听器:
client.on('ready', () => {
console.log('I am ready!');
});
回答by RDc12
My answer would be to use something like Heroku as said in a comment, but I believe there is a way to do it within your js code, using a batch file to start your bot and then calling that and then using client.destroy();
我的答案是像评论中所说的那样使用 Heroku 之类的东西,但我相信有一种方法可以在你的 js 代码中做到这一点,使用批处理文件启动你的机器人,然后调用它,然后使用 client.destroy() ;
I'm not sure exactly what you should use but look into running a batch file from within your bot.
我不确定您应该使用什么,但会考虑从您的机器人中运行批处理文件。

