【问题标题】:Is it possible to use Discord slash commands as module?是否可以使用 Discord 斜杠命令作为模块?
【发布时间】:2021-08-01 04:00:48
【问题描述】:

首先,我是 node.js 和 discord.js 的初学者。

我目前正在开发我自己的 discord 机器人以获得乐趣,我想知道是否可以“存储”斜杠命令的数据。这是我的意思的一个例子:

    const response = new Discord.MessageEmbed()
      .properties()
      .properties()
      etc...
    
    module.exports = () {
      const data = {
        data: {
          type: 4, // I don't know what type to use  for embed message so please correct me if I'm wrong
          data: {
            content: response
          }
        }
      }
    }

这是否可以将嵌入消息数据“存储”在rules.js 中并在main.js 中使用它,如下所示:

    const rules = require('rules.js') //the file would be in a "commands" folder but I'm keeping this simple

    client.ws.on('INTERACTION_CREATE', async (interaction) => {
      const command = interaction.data.name.toLowerCase()
      if (command === 'rules') {
        client.api.interactions(interaction.id, interaction.token).callback.post(data) // or post(rules.data) ??? again, I'm a beginner
      }
    })

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    你应该尝试创建一个函数而不是保存和导出模块,因为像模块一样使用实例类很危险,因为它会导致内存问题,所以为此编写一个工厂

    【讨论】:

      【解决方案2】:

      您可以尝试以下方法:

      //file name: slashCmd.js
      const Discord = require('discord.js')
      module.exports = async function(client, cmdObj) {
           client.api.applications(client.user.id).commands.post(cmdObj)
      }
      

      您可以根据需要自定义函数,但您可以在 index.js 中执行此操作

      const slashCmd = require('./slashCmd.js');
      const client = new Discord.Client()
      client.on('ready', () => {
          slashCmd(client, {
              name: 'cmdName',
              description: 'cmdDesc'
          })
      })
      

      【讨论】:

      • 我会试试这个。谢谢!
      猜你喜欢
      • 2021-12-31
      • 1970-01-01
      • 2021-10-19
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2011-08-13
      • 2021-10-23
      相关资源
      最近更新 更多