简体   繁体   English

Discord.js - 为什么我的 Discord 机器人不工作?

[英]Discord.js - Why does my discord bot not work?

I'm making a bot that toggles a certain mode when you type =Christian.我正在制作一个机器人,当你输入=Christian 时,它会切换某种模式。 I am using a command handler (explaining why this code is not from index.js).我正在使用命令处理程序(解释为什么这段代码不是来自 index.js)。 I have got it working in the past without certain alterations that have broken it.我过去已经让它工作了,而没有进行某些破坏它的改动。 When I run the bot I get no errors, and when I use the command the command prompt throws nothing.当我运行机器人时,我没有收到任何错误,当我使用命令时,命令提示符什么也不抛出。 Please help - here is my code:请帮忙 - 这是我的代码:

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

I have changed a few nsfw words explaining the nigels etc. Thanks in advance for any support.我已经更改了一些 nsfw 词来解释 nigels 等。在此先感谢您的支持。

Try this:尝试这个:

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

Here's a basic example I made for you without a command handler.这是我为您制作的没有命令处理程序的基本示例。

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM