Search code examples
node.jsdiscorddiscord.js

Interaction Fails from Discord slash commands


I need a bit of help with my code, I don't get any errors except This Interaction Failed in Discord.

The code I am using for slash commands:

    name: "random",
    description: "Random Message",
    options: [],
    async execute(_bot, say, interaction) {
        var facts = ["test1", "test2", "test3", "test4"];
var fact = Math.floor(Math.random() * facts.length);
        await say(interaction, facts[fact]);
    },
};

The code that works with my prefix and non-slash command:

exports.run = async (client, message, args) => {
var facts = ["test", "test2", "test3"];
var fact = Math.floor(Math.random() * facts.length);
message.channel.send(facts[fact]);
}```

Solution

  • I had this issue, so I looked over the API documentation again and found that while I was sending a response to the channel, I wasn't actually responding to the interaction. To respond to the interaction you have to send data to a webhook using the interaction ID and token. From the documentation:

    url = "https://discord.com/api/v8/interactions/<interaction_id>/<interaction_token>/callback"
    
    json = {
        "type": 4,
        "data": {
            "content": "Congrats on sending your command!"
        }
    }
    r = requests.post(url, json=json)
    

    Check out more here: https://discord.com/developers/docs/interactions/slash-commands#responding-to-an-interaction