javascript - 杀死 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
}
}
}
解决方案
首先,您可能应该尝试更多地了解命令处理程序,因为您似乎将一个事件放在一个事件中,并且您“结束”它的方式将使用“return”,它会停止您的命令。
推荐阅读
- python-3.x - 使用 relative_to_field 时 Python Faust Tumbling Window 不起作用
- typescript - Nuxt / Vue 2 + Typescript – VS Code 道具自动完成
- javascript - 为什么将值分配给所有数组而不仅仅是第一个数组?
- c++ - _popen() 不返回任何内容
- python - 尝试在 elif 中除外,但不能按预期工作
- c++ - 尝试使用继承但无法访问父类
- firebase - vuexfire + firestore:如何存储/获取当前页面 DocumentId?
- angular - 如何找到打字稿signalR的onDisconnect方法
- python - 线性回归中的平均绝对误差和均方误差较大,但 R**2 为 0.979
- laravel - 如何每 2 分钟从 API 获取数据并在所有项目中使用它?