javascript - Discord.js - 为什么我的不和谐机器人不起作用?
问题描述
我正在制作一个机器人,当你输入 =Christian 时,它会切换某种模式。我正在使用命令处理程序(解释为什么这段代码不是来自 index.js)。我过去已经让它工作了,没有某些改变会破坏它。当我运行机器人时,我没有收到任何错误,当我使用命令时,命令提示符什么也没有。请帮忙 - 这是我的代码:
const Discord = require('discord.js')
module.exports.run = async (bot, message, args) => {
toggled = true;
bot.on('message', msg => {
if (toggled === true) {
message.channel.send('Christian Mode Active')
} else {
message.channel.send('Christian Mode Inactive')
}
})
bot.on('message', msg => {
if (msg.content === '=christianoff')
toggled = false
})
while (toggled = true) {
bot.on('message', msg => {
if (msg.content === 'nigek') {
msg.delete()
msg.channel.send('frick')
} else if (msg.content === 'nigel') {
msg.delete()
msg.channel.send('nasty person')
} else if (msg.content === 'nigeh') {
msg.delete()
msg.channel.send('nibba')
} else if (msg.content === 'nigerh') {
msg.delete()
msg.channel.send('smelly person')
} else if (msg.content === 'nigh') {
msg.delete()
msg.channel.send('child')
} else if (msg.content === 'nigell') {
msg.delete()
msg.channel.send('child')
} else if (msg.content === 'nigels') {
msg.delete()
msg.channel.send('threat to society')
} else if (msg.content === 'nigehsa') {
msg.delete()
msg.channel.send('threat to society')
}
})
}
}
module.exports.help = {
name: "christian"
}
我已经更改了一些 nsfw 单词来解释 nigels 等。在此先感谢您的支持。
解决方案
尝试这个:
index.js
const {join} = require('path')
const {promises: {readdir}} = require('fs')
const {Client} = require('discord.js')
const bot = new Client()
const prefix = '='
let christian = true
bot.on('message', msg => {
const {author, channel, content} = msg
if (author.bot) return
if (content.startsWith(prefix)) {
const args = content.toLowerCase().slice(prefix.length).split(/\s+/g)
const command = args.shift()
// I put the commands in a folder called commands
// replace this with your command handler
readdir(join(__dirname, 'commands')).then(files => {
files.map(file => require(join(__dirname, 'commands', file))).forEach(cmd => {
if (command === cmd.help.name) {
// in the command's run function, you can return a value which will be set to christian
const result = cmd.run(bot, msg, args, christian)
if (typeof result !== 'undefined') christian = result
}
})
})
return
}
if (christian) {
const messages = {
nigek: 'frick',
nigel: 'nasty person',
nigeh: 'nibba',
nigerh: 'smelly person',
nigh: 'child',
nigell: 'child',
nigels: 'threat to society',
nigehsa: 'threat to society'
}
const message = messages[content]
if (message) channel.send(message)
}
})
bot.login('your token')
commands/christian.js
// I made it so that you can turn Christian mode on/off by running
// =christian on
// =christian off
exports.run = (bot, message, args, christian) => {
if (args[0] === 'off') christian = false
else if (args[0] === 'on') christian = true
message.channel.send(`Christian Mode ${christian ? 'Active' : 'Inactive'}`)
return christian
}
exports.help = {
name: 'christian'
}
推荐阅读
- java - 每次打印的 IvParameterSpec 值不同
- c# - WriteableBitmap 中的像素偏移
- gulp - 咕噜SRC | 负片没有按预期工作
- pandas - 如果大于 0,熊猫数据框会更改所有值
- javascript - 创建新用户时如何设置默认属性?
- virtualhost - 带有虚拟主机的 laragon 本地服务器中的 Yii 2 应用程序部署
- python - pyvisa 找不到 VISA 实现
- java - Lettuce 多个反应式 Redis 存储和跨存储事务
- java - 点击几张图片后我的应用程序崩溃了 - 为什么?
- javascript - React redux 嵌套智能组件 - 状态未映射到道具