首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Discord.js --如何在1台服务器上切换命令?

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

Stack Overflow用户
提问于 2020-03-24 11:01:37
回答 1查看 767关注 0票数 0

我正在建立一个不和谐的机器人与一个命令处理程序和一些模因命令。我的问题是,当我激活‘基督教模式’或‘无诅咒模式’时,它会在机器人所在的每一个服务器上激活。我的基督教模式代码在这里,横跨Christian.js和Index.js。

Christian.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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')
    }
  }

)新代码:

代码语言:javascript
复制
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
  }

这是我的前缀和命令处理程序,有什么需要修改的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-24 11:17:08

您可以将christian设置为object,然后将message.guild.id添加为任何值(true或false)的属性,如下所示

代码语言:javascript
复制
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'
}
代码语言:javascript
复制
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')
    }
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60829715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档