簡體   English   中英

Discord 斜杠命令“交互失敗”

[英]Discord slash command "interaction failed"

我正在關注 V13 上 discord 斜杠命令的教程,運行時沒有錯誤,但是當我 go 執行 /ping 時,它說交互失敗。 我查看了文檔所需的權限,但它看起來像我 go 運行它所需的所有權限,即機器人和應用程序命令。

這是我的機器人代碼:

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)
  1. 您拼寫為interactCreate而不是interactionCreate

  2. 您在該事件的回調中使用交互 class 而不是名為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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM