【问题标题】:Discord.js cannot add option to slash commandsDiscord.js 无法为斜线命令添加选项
【发布时间】:2022-01-15 05:53:58
【问题描述】:

我已经开始迁移到 discordjs v13 并遇到了这个错误:Property 'options' does not exist on type 'Interaction '

运行事件:

export interface runEvent {
  interaction: Interaction,
  client: Client,
  args: string[],
}

哪里出错了

export const execute = ({interaction, client, args}:runEvent) => {
    console.log(interaction.options.getString('name'))
}                          ^^^^^^^^

是什么导致这个抛出这个错误?当我登录 interaction 本身时,我可以看到它具有属性选项。那么是什么引发了这个错误呢?

附带说明,将interaction: Interaction 更改为interaction: any 可立即解决此问题。我只是好奇是什么引发了这个错误

【问题讨论】:

    标签: typescript discord discord.js


    【解决方案1】:

    这是因为Interaction任何类型的交互。这包括按钮、选择菜单和上下文菜单,它们没有options 属性。您可以将其转换为 CommandInteraction,但建议您使用类型保护 (.isCommand()),以便如果它不同的类型,它将改为返回

    export const execute = ({ interaction, client, args }: runEvent) => {
        if (!interaction.isCommand()) return;
        console.log(interaction.options.getString('name'))
    }  
    

    【讨论】:

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