英文:
Discord.js InteractionAlreadyReplied – sending select menu and after it modal
问题
我有这段代码用于我的Discord机器人。当它接收到斜杠命令时,它会回复一个选择菜单,在其中你可以从.json文件中选择,一切都正常。然后,当你选择选项时,它应该发送一个带有文本输入的模态框,以便你可以更改.json中的参数,但它没有发送,出现了错误"InteractionAlreadyReplied The reply to this interaction has already been sent or deferred"
} else if (testOption === 'upravit') {
const database = JSON.parse(fs.readFileSync('events/testy.json', 'utf8'));
const currentDate = new Date();
const currentDateString = currentDate.toISOString().slice(0, 10).replace(/-/g, ''); // 格式化为YYYYMMDD
const options = database.entries
.filter(entry => {
return entry.datum >= currentDateString;
})
.map(entry => {
const entryDate = new Date(
entry.datum.substring(0, 4),
entry.datum.substring(4, 6) - 1, // 月份是从零开始计数的
entry.datum.substring(6, 8)
);
const formattedDate = entryDate.toLocaleDateString('cs-CZ', {
day: '2-digit',
month: '2-digit',
year: 'numeric'
});
return {
label: entry.predmet,
description: `${entry.tema}, naplánováno na ${formattedDate}`,
value: entry.id.toString()
};
});
const select = new StringSelectMenuBuilder()
.setCustomId('pick-test')
.setPlaceholder('Testy')
.addOptions(options);
const row = new ActionRowBuilder()
.addComponents(select);
try {
await interaction.reply({
content: 'Vyber test pro upravení.',
components: [row],
});
const userInteraction = await interaction.channel.awaitMessageComponent({
filter: i => i.customId === 'pick-test' && i.user.id === interaction.user.id,
time: 60000
});
if (!userInteraction) {
await interaction.followUp({ content: 'Vypršel čas na výběr.' });
return;
}
const selectedTestId = userInteraction.values[0];
const modal = new ModalBuilder()
.setCustomId('test-edit')
.setTitle('Upravení testu');
const idTestu = selectedTestId
const newTema = new TextInputBuilder()
.setLabel('Nové téma testu')
.setCustomId('newTema-testu')
.setRequired(false)
.setPlaceholder(`(nepovinné)`)
.setStyle(TextInputStyle.Short);
const newDate = new TextInputBuilder()
.setCustomId('newDate-testu')
.setLabel('Nové datum testu')
.setStyle(TextInputStyle.Short)
.setPlaceholder(`ve formátu YYYYMMDD (nepovinné)`)
.setRequired(false)
.setMaxLength(8);
const firstActionRow = new ActionRowBuilder().addComponents(idTestu);
const secondActionRow = new ActionRowBuilder().addComponents(newTema);
const thirdActionRow = new ActionRowBuilder().addComponents(newDate);
modal.addComponents(firstActionRow, secondActionRow, thirdActionRow);
await interaction.showModal(modal);
} catch (error) {
console.error(error);
await interaction.followUp({ content: 'Nastala chyba při zpracování příkazu.', ephemeral: true });
}
}
尝试更改这个
await userInteraction.reply({ content: ' ', components: [modal] });
还尝试使用
interaction.followUp
和 interaction.deferReply
谢谢帮助!
英文:
I have this code for my discord bot. When it receives slash command, it replies with select menu, where you pick from .json file, all working. Then, when you select option, it should send a modal with text input so you can change params in the .json, butit doesnt send, with error "InteractionAlreadyReplied The reply to this interaction has already been sent or deferred"
} else if (testOption === 'upravit') {
const database = JSON.parse(fs.readFileSync('events/testy.json', 'utf8'));
const currentDate = new Date();
const currentDateString = currentDate.toISOString().slice(0, 10).replace(/-/g, ''); // Format to YYYYMMDD
const options = database.entries
.filter(entry => {
return entry.datum >= currentDateString;
})
.map(entry => {
const entryDate = new Date(
entry.datum.substring(0, 4),
entry.datum.substring(4, 6) - 1, // Month is zero-based
entry.datum.substring(6, 8)
);
const formattedDate = entryDate.toLocaleDateString('cs-CZ', {
day: '2-digit',
month: '2-digit',
year: 'numeric'
});
return {
label: entry.predmet,
description: `${entry.tema}, naplánováno na ${formattedDate}`,
value: entry.id.toString()
};
});
const select = new StringSelectMenuBuilder()
.setCustomId('pick-test')
.setPlaceholder('Testy')
.addOptions(options);
const row = new ActionRowBuilder()
.addComponents(select);
try {
await interaction.reply({
content: 'Vyber test pro upravení.',
components: [row],
});
const userInteraction = await interaction.channel.awaitMessageComponent({
filter: i => i.customId === 'pick-test' && i.user.id === interaction.user.id,
time: 60000
});
if (!userInteraction) {
await interaction.followUp({ content: 'Vypršel čas na výběr.' });
return;
}
const selectedTestId = userInteraction.values[0];
const modal = new ModalBuilder()
.setCustomId('test-edit')
.setTitle('Upravení testu');
const idTestu = selectedTestId
const newTema = new TextInputBuilder()
.setLabel('Nové téma testu')
.setCustomId('newTema-testu')
.setRequired(false)
.setPlaceholder(`(nepovinné)`)
.setStyle(TextInputStyle.Short);
const newDate = new TextInputBuilder()
.setCustomId('newDate-testu')
.setLabel('Nové datum testu')
.setStyle(TextInputStyle.Short)
.setPlaceholder(`ve formátu YYYYMMDD (nepovinné)`)
.setRequired(false)
.setMaxLength(8);
const firstActionRow = new ActionRowBuilder().addComponents(idTestu);
const secondActionRow = new ActionRowBuilder().addComponents(newTema);
const thirdActionRow = new ActionRowBuilder().addComponents(newDate);
modal.addComponents(firstActionRow, secondActionRow, thirdActionRow);
await interaction.showModal(modal);
} catch (error) {
console.error(error);
await interaction.followUp({ content: 'Nastala chyba při zpracování příkazu.', ephemeral: true });
}
}
Tried to change this
await userInteraction.reply({ content: ' ', components: [modal] });
also used
interaction.followUp
and interaction.deferReply
Thanks for help!
答案1
得分: 1
你正在为一个“interaction”发送一个模态框。这应该发送到“userInteraction”。
- await interaction.showModal(modal);
+ await userInteraction.showModal(modal);
英文:
You are sending a Modal for an interaction
. This should be sent to userInteraction
.
- await interaction.showModal(modal);
+ await userInteraction.showModal(modal);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论