【问题标题】:Discord slash command "interaction failed"Discord 斜杠命令“交互失败”
【发布时间】:2021-12-31 13:07:01
【问题描述】:

我正在关注 V13 上不和谐斜线命令的教程,运行时没有错误,但是当我执行 /ping 时,它说交互失败。我查看了文档所需的权限,但看起来我获得了运行它所需的所有权限,即机器人和应用程序命令。

这是我的机器人代码:

import DiscordJS, { Intents, Interaction } from 'discord.js'
import dotenv from 'dotenv'
dotenv.config()

const client = new DiscordJS.Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES
    ]
})

client.on('ready', () => {
    console.log('The bot is ready')

    const guildID = '906389171806040114'
    const guild = client.guilds.cache.get(guildID)
    let commands

    if (guild) {
        commands = guild.commands
    } else {
        commands = client.application?.commands
    }

    commands?.create({
        name: 'pig',
        description: 'Replies with pong',
    })

    commands?.create
})

client.on('interactCreate', async (Interaction) => {
    if (!Interaction.isCommand()) {
        return
    }

    const { commandName, options } = Interaction

    if ( commandName === 'pig') {
        Interaction.reply({
            content: 'pong',
            ephemeral: true,
        })
    }
})



client.login(process.env.TOKEN)

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:
    1. 您拼写为interactCreate 而不是interactionCreate

    2. 您在该事件的回调中使用了 Interaction 类,而不是名为 interaction 的新变量

    3. 在准备好的事件结束时你有一个随机的commands?.create;你应该删除那个

    4. 您的意思是将ping 拼写为pig 吗?这可能会在以后引起一些问题

    5. 一个小建议:你可以解构 DiscordJS 并直接导入 Client

    import { Client, Intents } from 'discord.js'
    import dotenv from 'dotenv'
    dotenv.config()
    
    const client = new Client({
        intents: [
            Intents.FLAGS.GUILDS,
            Intents.FLAGS.GUILD_MESSAGES
        ]
    })
    
    client.on('ready', () => {
        console.log('The bot is ready')
    
        const guildID = '906389171806040114'
        const guild = client.guilds.cache.get(guildID)
        let commands
    
        if (guild) {
            commands = guild.commands
        } else {
            commands = client.application?.commands
        }
    
        commands?.create({
            name: 'ping',
            description: 'Replies with pong',
        })
    })
    
    client.on('interactionCreate', async (interaction) => {
        if (!interaction.isCommand()) {
            return
        }
    
        const { commandName, options } = interaction
    
        if ( commandName === 'ping') {
            Interaction.reply({
                content: 'pong',
                ephemeral: true,
            })
        }
    })
    
    client.login(process.env.TOKEN)
    

    【讨论】:

    • 谢谢!我回顾了你所做的不同并同意。我正在看的教程也犯了更多的错误,我不喜欢他们这样做的方式。我知道为什么会有额外的commands?.create。非常感谢您的帮助!
    • 不客气!
    猜你喜欢
    • 2021-10-19
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2021-12-15
    • 2021-07-02
    • 1970-01-01
    相关资源
    最近更新 更多