提问人:David Wang 提问时间:11/16/2023 最后编辑:Muhamed FazeelDavid Wang 更新时间:11/18/2023 访问量:28
Discord .js V13 按钮“此交互失败”
Discord.js V13 buttons "this interaction failed"
问:
我正在尝试编写一个语言测验 Discord 机器人。但是,消息按钮不起作用,只会说“此交互失败”。我无法修复它。请注意,我使用的是Discord .js V13。
const { Client, Intents, MessageActionRow, MessageButton } = require('discord.js');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { SlashCommandBuilder } = require('@discordjs/builders');
const mySecret = process.env['Key'];
const clientID = process.env.CLIIENT_ID;
const guildID = process.env.GUILD_ID;
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const commands = [
{
name: 'learngerman',
description: 'Learn German',
},
{
name: 'learnrussian',
description: 'Learn Russian',
},
];
const rest = new REST({ version: '9' }).setToken(mySecret);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(clientID, guildID),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error('Error refreshing application (/) commands:', error);
}
})();
const questions = {
german: [
{ question: 'Translate the word "dog" to German:', answer: 'Hund', choices: ['Hund', 'Katze', 'Auto', 'Tisch'] },
{ question: 'Translate the word "tree" to German:', answer: 'Baum', choices: ['Baum', 'Blume', 'Haus', 'Buch'] },
{ question: 'Translate the word "computer" to German:', answer: 'Computer', choices: ['Computer', 'Fenster', 'Stuhl', 'Telefon'] },
// Add more German questions as needed
],
russian: [
{ question: 'Translate the word "cat" to Russian:', answer: 'кошка', choices: ['кошка', 'собака', 'машина', 'дом'] },
{ question: 'Translate the word "sun" to Russian:', answer: 'солнце', choices: ['солнце', 'луна', 'звезда', 'вода'] },
{ question: 'Translate the word "book" to Russian:', answer: 'книга', choices: ['книга', 'стол', 'цветок', 'окно'] },
// Add more Russian questions as needed
],
};
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('interactionCreate', async interaction => {
if (!interaction.isButton()) return;
const { customId, user } = interaction;
const choiceIndex = parseInt(customId);
if (interaction.customId.startsWith('choice_')) {
// Interaction is from the initial question, ignore it
return;
}
const commandName = interaction.commandName;
if (commandName === 'learngerman' || commandName === 'learnrussian') {
// Implement German and Russian learning logic
const language = commandName === 'learngerman' ? 'german' : 'russian';
const randomQuestion = questions[language][Math.floor(Math.random() * questions[language].length)];
const { answer } = randomQuestion;
const correct = choiceIndex === 0 && interaction.user.id === user.id;
await interaction.update({
content: correct ? `Correct! ${user.username} answered "${answer}".` : `Incorrect! The correct answer is "${answer}".`,
components: [],
});
}
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
if (commandName === 'learngerman' || commandName === 'learnrussian') {
// Implement German and Russian learning logic
const language = commandName === 'learngerman' ? 'german' : 'russian';
const randomQuestion = questions[language][Math.floor(Math.random() * questions[language].length)];
const { question, choices } = randomQuestion;
const row = new MessageActionRow()
.addComponents(
choices.map((choice, index) => new MessageButton()
.setCustomId(index.toString())
.setLabel(choice)
.setStyle('PRIMARY')),
);
await interaction.reply({
content: `${question}`,
components: [row],
});
}
});
client.login(mySecret);
这是怎么回事?!??!?!?我也没有收到任何错误消息。
我重写了 ie=t 几次,结果都是一样的。它应该回应宣布你是对还是错。
答:
-1赞
LmanTW
11/17/2023
#1
似乎当交互通过按钮激活时,它不会提供 .commandName,因此它不会传递此 if 语句:
if (commandName === 'learngerman' || commandName === 'learnrussian') {
...
}
我建议你使用 Collector 来制作这个功能。
评论