【问题标题】:Discord.js slash commands type 5Discord.js 斜线命令类型 5
【发布时间】:2021-08-17 19:06:49
【问题描述】:

所以我最近一直在开发一个机器人,我已经在所说的机器人中实现了斜杠命令。我遇到了对类型 5 命令“响应”的需求,但我似乎找不到关于斜杠命令的好的文档。我似乎无法让它“停止思考”。任何帮助将不胜感激!

编辑:我发现您需要编辑交互响应 (https://discord.com/developers/docs/interactions/slash-commands#interaction-response),但我没有使用 webhooks 我正在使用机器人,如果我不希望获得另一个 npm 库'不必。那么如何编辑我的交互呢?

【问题讨论】:

  • 如果你只是return函数呢?代码 sn-p 也可以很好地查看您的代码有什么问题。

标签: discord.js


【解决方案1】:

我已经解决了这个问题,如果你想知道我是怎么做的,这里有一些代码。

如果您的交互响应器如下所示:

if (interaction.data.name === 'whatever') {
    whatever.whatever (interaction)//i am using a command handler to put 
    //the actual event into a different file
  }

你的“交互消息发送者”看起来像这样:(注意它是类型 5)

module.exports.whatever = (interaction) => {
    client.api.interactions(interaction.id, interaction.token).callback.post({
        data: {
            type: 5
        }
    })
};

然后它会说“{botname} 正在思考”并带有一个小省略号,并且在 15 分钟后如果没有任何反应,它将导致交互失败。如果你想让它“停止思考”,你必须编辑消息。我正在使用 axios npm 库 (https://www.npmjs.com/package/axios),如果您只是输入此代码,它应该会编辑您的交互消息。根据您的要求,它位于文件的顶部:

const axios = require('axios')
const appId = ''//bot id goes here

你的文件底部附近的某个地方可能会放在这个:

const editInteraction = async (client, interaction, response) => {
    const data = typeof response === 'object' ? { embeds: [ response ] } : { content: response };
    const channel = await client.channels.resolve(interaction.channel_id);
    return axios
        .patch(`https://discord.com/api/v8/webhooks/${appId}/${interaction.token}/messages/@original`, data)
        .then((answer) => {
            return channel.messages.fetch(answer.data.id)
        })
};

然后您将拥有编辑消息的基本代码结构,现在您只需要编辑消息。为此,请在您的代码中执行以下操作:

  if (interaction.data.name === 'whatever') {
    whatever.whatever (interaction).then(m => {
    editInteraction(client, interaction, '>:(')//this will actually edit the message so 
    //instead of " >:( " put in what you want to edit you message to be
      })
  }

然后您可以运行该命令,它会说机器人正在思考,然后在您想要运行的任何事件之后,它会编辑它以说出任何内容!

【讨论】:

    猜你喜欢
    • 2021-06-11
    • 2022-01-25
    • 2021-11-17
    • 2022-01-14
    • 2022-01-15
    • 2021-11-12
    • 2021-08-28
    • 2021-09-12
    • 2021-12-22
    相关资源
    最近更新 更多