我想从用户那里收集消息,但是如果用户停止输入假设5分钟,收集器/命令结束,我就会遇到问题。
目前我可以让它在没有time和max过滤器的情况下无限运行,我知道我可以检查像"stop"所以用户可以自己停止命令,但是我想确保如果用户忘记这样做,收集器/命令会自己停止。
import { SlashCommandBuilder } from "discord.js";
import { command, textToSpeech } from "../../utils";
const meta = new SlashCommandBuilder()
.setName("echo")
.setDescription("I'll say in voice whatever you type.");
export default command(meta, ({ interaction }) => {
if (!interaction.channel) {
return interaction.reply(`You can't use this command here.`);
}
if (interaction.channel.type != 0) {
return interaction.reply(`I can't read your messages here.`);
}
interaction.reply(`Now I'll say everything you type.`);
const collector = interaction.channel.createMessageCollector({
filter: (msg) => msg.author.id === interaction.user.id,
max: 1,
time: 300000,
});
collector.on("collect", (msg) => {
textToSpeech(msg.content);
});
collector.on("end", () => {
interaction.reply(`Now I'll stop saying what you type.`);
});
});
我也有问题发送另一个回复collector.on("end")似乎一次互动只能回复一次。最后我该如何回复呢?如果我编辑第一个回复,用户可能看不到它,因为随着用户输入,回复消息会上升。
我通过删除max过滤器并使用collector.resetTimer();重置计时器来解决这个问题。在collector.on("collect",…)
另外,我可以使用interaction.followUp()发送另一个回复。在收集器结束时发送另一个应答。
这里的解决方案代码:
const collector = interaction.channel.createMessageCollector({
filter: (msg) => msg.author.id === interaction.user.id,
time: 300000,
});
collector.on("collect", (msg) => {
textToSpeech(msg.content);
collector.resetTimer();
});
collector.on("end", () => {
interaction.followUp(`Now I'll stop saying what you type.`);
});