【问题标题】:How do I toggle a command in 1 discord server instead of across all discord servers the bot is in?如何在 1 个不和谐服务器中切换命令,而不是在机器人所在的所有不和谐服务器之间切换?
【发布时间】:2020-07-06 08:41:58
【问题描述】:

我正在编写一个不和谐的机器人,它有一些命令,由我制作的命令处理程序处理。我将在下面包含代码。我的问题是,如果我启用该命令,它会在机器人所在的每个服务器上切换。那么我如何实现一个message.guild.id 系统,它只切换服务器中输入命令的命令? 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[message.guild.id] ? 'Active' : 'Inactive'}`)
  return christian[message.guild.id]
}

exports.help = {
  name: 'christian'
}
  • 我曾尝试添加公会,但惨遭失败 (部分)index.js - 主文件
bot.on('message', msg => {
  const {author, channel, content} = msg

  if (author.bot) return

  try {
    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
    }
  } catch (error) {
    console.log('Command loading error has cccured')
  }

  if (christian[msg.guild.id]) {
    const messages = {
      fdck: 'frick',
      biadtch: 'nasty person',
      nadr: 'nibba',
      ndga: 'smelly person',
      padsy: 'child',
      pude: 'child',
      casdnt: 'threat to society',
      bdrd: 'threat to society',
      dsk: 'widly'
    }
    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')
    }
  }
})

由于某种原因,我无法让我的代码与公会一起使用。

提前谢谢你,

乔(原型)

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    尝试像这样改变它:

     if (args[0] === 'off') christian[message.guild.id] = false
     else if (args[0] === 'on') christian[message.guild.id] = true
    

    return christian
    

    【讨论】:

    • 对不起,我的意思是我希望机器人只在输入命令的服务器中切换命令 - 不是机器人所在的每个服务器。 - 我不介意特定的服务器。跨度>
    • @Proto 然后你必须创建一个文件,你将在其中存储服务器,它已启用。我认为您在弄乱变量类型,将 christian 设置为布尔值,然后尝试将其解析为对象
    • 天哪,这比我想象的要复杂。我需要将服务器的公会 ID 存储在一个文件中,我可以在其中指定 Christian 是打开还是关闭 - 这可能需要很长时间。
    • 当我这样做时,无论我输入什么,它都会发布 Christian Mode Inactive。在这么少的代码中出现了如此奇怪的错误 - 我需要根据参数确定 Christian[message.guild.id] 是返回真还是假:/
    • 加我到我的不和谐 Tomas Gaming#7156
    猜你喜欢
    • 2019-11-10
    • 2020-07-21
    • 2018-11-10
    • 2021-06-20
    • 2021-08-11
    • 1970-01-01
    • 2021-08-30
    • 2020-10-17
    • 1970-01-01
    相关资源
    最近更新 更多