首页 > 解决方案 > 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 等。在此先感谢您的支持。

标签: javascriptdiscorddiscord.js

解决方案


尝试这个:

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'
}

推荐阅读