Discord.js-如何在1台服务器中切换命令?

时间:2020-03-24 11:01:37

标签: javascript node.js discord discord.js

我正在构建一个带有命令处理程序和一些meme命令的discord机器人。我的问题是,当我激活“克里斯蒂安模式”或“不发誓模式”时,它将在机器人所在的每台服务器上激活。我的克里斯蒂安模式代码在此处,横跨C​​hristian.js和Index.js。

Christian.js

const Discord = require('discord.js')

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

(属于index.js的一部分)

if (christian) {
    const messages = {
      flib: 'frick',
      batch: 'nasty person',
      nogger: 'nibba',
      nafga: 'smelly person',
      poohsy: 'child',
      poohsyhole: 'child',
      cant: 'threat to society',
      bestard: 'threat to society',
      dock: 'willy'
    }
    try {
      const message = messages[content.toLowerCase()]
      if (message) msg.delete() & channel.send(message).then((m) => m.delete(10000));
    } catch (error) {
      console.log('An error has occurred - make sure the bot has delete message permissions')
    }
  }

提前感谢:) 新代码:

if (content.startsWith(prefix)) {
    const args = content.toLowerCase().substring(prefix.length).split(/\s+/g)
    const command = args.shift()

    readdir(join(__dirname, 'commands')).then(files => {
      files.map(file => require(join(__dirname, 'commands', file))).forEach(cmd => {
        if (command === cmd.help.name) {
          const resultchristian = cmd.run(bot, msg, args, christian)
          if (typeof resultchristian !== 'undefined') christian = resultchristian
        }
      })
    })
    return
  }

这是我的前缀和命令处理程序,我需要对其进行更改以使其起作用吗?

1 个答案:

答案 0 :(得分:1)

您可以将christian设置为object,然后将message.guild.id添加为具有任何值(true或false)的属性 像这样

const Discord = require('discord.js')

exports.run = (bot, message, args, christian) => {
  if (args[0] === 'off') christian[message.guild.id] = false
  else if (args[0] === 'on') christian[message.guild.id] = true
  message.channel.send(`Christian Mode ${christian[message.guild.id] ? 'Active' : 'Inactive'}`)
  return christian
}

exports.help = {
  name: 'christian'
}
let christian = {}

if (christian[message.guild.id]) {
    const messages = {
      flib: 'frick',
      batch: 'nasty person',
      nogger: 'nibba',
      nafga: 'smelly person',
      poohsy: 'child',
      poohsyhole: 'child',
      cant: 'threat to society',
      bestard: 'threat to society',
      dock: 'willy'
    }
    try {
      const message = messages[content.toLowerCase()]
      if (message) msg.delete() & channel.send(message).then((m) => m.delete(10000));
    } catch (error) {
      console.log('An error has occurred - make sure the bot has delete message permissions')
    }
  }