首页 > 解决方案 > Discord.js 重启命令不起作用(返回未定义的错误)

问题描述

我正在使用 discord.js 制作一个机器人,但我的重启命令不起作用。通过不工作,我的意思是,我得到这个错误:

(node:41784) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_timeouts' of undefined

我的代码是:

const config = require('../../config.json');
module.exports.run = async (bot, message, args) => {

    if(!config.owners.includes(message.author.id)) {
        return message.channel.send(`Only the bot owner can execute this command`)
    }

    message.channel.send(`Okay, I'll restart...`)
      .then(
        bot.destroy
    ).then(
        bot.login(config.token)
    )
};

module.exports.help = {
    name: "restart",
    description: "Restarts the bot",
    usage: "restart",
    category: "dev"
};

如果可以,请帮忙

标签: javascriptdiscorddiscord.js

解决方案


尝试这个:

const config = require('../../config.json');
module.exports.run = async (bot, message, args) => {

    if(!config.owners.includes(message.author.id)) {
        return message.channel.send(`Only the bot owner can execute this command`)
    }

    message.channel.send(`Okay, I'll restart...`)
    .then(()=>bot.destroy()) // <<<<
    .then(()=>bot.login(config.token)) // <<<<
};

module.exports.help = {
    name: "restart",
    description: "Restarts the bot",
    usage: "restart",
    category: "dev"
};

.then()将函数作为参数,因此您必须将操作包装到函数中。


推荐阅读