首页 > 解决方案 > 杀死 discord.js 中的命令处理程序脚本

问题描述

我正在使用 discord.js 开发一个不和谐的机器人,但我不擅长编码。我想要做的是获得一个命令来启动一个进程来询问然后接受用户输入。所以机器人会问一些东西,它会听用户的消息。但我也想要一个命令来停止这个询问过程。有没有办法杀死这个特定命令的命令处理程序而不是 main.js?我的 main.js 和我使用的命令处理程序:

const Discord = require('discord.js')
const bot = new Discord.Client()
const fs = require('fs')
bot.commands = new Discord.Collection()

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'))
for(const file of commandFiles){
    const command = require(`./commands/${file}`)
 
    bot.commands.set(command.name, command)
}

bot.on('message', message =>{
    if(command === 'ask'){
        bot.commands.get('ask').execute(message,args, bot)
    }
}

命令处理程序:

module.exports = {
    name: 'ask',
    execute(message, args, bot){
         bot.on('message', msg =>{
             if(msg.content.toLowerCase() == "endask"){
                // So is there anyway here to only kill this command handler
             }
         }
    }

标签: javascriptnode.jsdiscorddiscord.jsfs

解决方案


首先,您可能应该尝试更多地了解命令处理程序,因为您似乎将一个事件放在一个事件中,并且您“结束”它的方式将使用“return”,它会停止您的命令。


推荐阅读