Discord.js-为什么我的discord机器人不起作用?

时间:2020-03-21 18:06:04

标签: javascript discord 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词,以解释奈杰尔等。 预先感谢您的支持。

2 个答案:

答案 0 :(得分:1)

尝试一下:

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

答案 1 :(得分:1)

这是我为您制作的没有命令处理程序的基本示例。

const Discord = require('discord.js');
const Client = new Discord.Client();
let Active = false;
let BlacklistedWords = ["cookie", "noob"];

Client.on('message', (message) => {
    if (message.content == "!christian") {Active = true; return false;}; // Activating the command on "!christian" message.
    if (message.content == "!christianoff") {Active = false; return false;} // Deactivating the command on "!christianoff" message.

    if (!Active) return false; // Checking if the command is active.

    if (BlacklistedWords.some(word => message.toString().toLowerCase().includes(word))) {message.delete(); message.reply("You are not allowed to use that word.")} // Checking if the message has a word included in BlacklistedWords.
});

Client.login("TOKEN");