簡體   English   中英

Discord.js - 為什么我的 Discord 機器人不工作?

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

我正在制作一個機器人,當你輸入=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'
}

這是我為您制作的沒有命令處理程序的基本示例。

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