Discord.js V14 ButtonBuilder() buttons stop updating/getting edited after the 15 minute SlashCommand timeout, how do I bypass the timeout?

huangapple go评论103阅读模式
英文:

Discord.js V14 ButtonBuilder() buttons stop updating/getting edited after the 15 minute SlashCommand timeout, how do I bypass the timeout?

问题

  1. // 每30秒更新按钮标签(或者您设置的时间间隔)
  2. setInterval(() => {
  3. const currentTimeET = DateTime.now().setZone("America/New_York");
  4. const formattedTimeET = currentTimeET.toLocaleString(DateTime.TIME_SIMPLE);
  5. const currentTimePT = DateTime.now().setZone("America/Los_Angeles");
  6. const formattedTimePT = currentTimePT.toLocaleString(DateTime.TIME_SIMPLE);
  7. const currentTimeCT = DateTime.now().setZone("America/Chicago");
  8. const formattedTimeCT = currentTimeCT.toLocaleString(DateTime.TIME_SIMPLE);
  9. const currentTimeBST = DateTime.now().setZone("Europe/London");
  10. const formattedTimeBST = currentTimeBST.toLocaleString(DateTime.TIME_SIMPLE);
  11. const currentTimeCEST = DateTime.now().setZone("Europe/Paris");
  12. const formattedTimeCEST = currentTimeCEST.toLocaleString(
  13. DateTime.TIME_SIMPLE
  14. );
  15. // 创建新的按钮,在删除原始按钮后附上更新时间
  16. const newButtonZones = new ActionRowBuilder().addComponents(
  17. new ButtonBuilder()
  18. .setCustomId("buttonET")
  19. .setLabel(`EST/EDT - ${formattedTimeET}`)
  20. .setStyle(ButtonStyle.Secondary),
  21. new ButtonBuilder()
  22. .setCustomId("buttonPT")
  23. .setLabel(`PST/PDT - ${formattedTimePT}`)
  24. .setStyle(ButtonStyle.Secondary),
  25. new ButtonBuilder()
  26. .setCustomId("buttonCT")
  27. .setLabel(`CST/CDT - ${formattedTimeCT}`)
  28. .setStyle(ButtonStyle.Secondary),
  29. new ButtonBuilder()
  30. .setCustomId("buttonBST")
  31. .setLabel(`GMT/BST - ${formattedTimeBST}`)
  32. .setStyle(ButtonStyle.Secondary),
  33. new ButtonBuilder()
  34. .setCustomId("buttonCEST")
  35. .setLabel(`CET/CEST - ${formattedTimeCEST}`)
  36. .setStyle(ButtonStyle.Secondary)
  37. );
  38. // 更新按钮标签
  39. buttonZones.components = newButtonZones.components;
  40. interaction.editReply({ components: [buttonZones] });
  41. }, 30000);

Discord.js V14 ButtonBuilder() buttons stop updating/getting edited after the 15 minute SlashCommand timeout, how do I bypass the timeout?

如标题所示,15分钟后按钮内部的时间戳不再更新。我想这是因为Discord不允许在15分钟后编辑回复。

我该如何绕过这个限制,使按钮永久更新?我看过一些关于这个主题的帖子,但没有涉及到ButtonBuilder()的更新。

(不确定是否重要,但我使用Luxon来处理时间戳,并使用PM2来在关闭托管服务的终端/控制台后继续运行节点实例。)

感谢您的任何帮助。

英文:
  1. // Update button labels every 30 seconds (or whatever you set it at)
  2. setInterval(() => {
  3. const currentTimeET = DateTime.now().setZone("America/New_York");
  4. const formattedTimeET = currentTimeET.toLocaleString(DateTime.TIME_SIMPLE);
  5. const currentTimePT = DateTime.now().setZone("America/Los_Angeles");
  6. const formattedTimePT = currentTimePT.toLocaleString(DateTime.TIME_SIMPLE);
  7. const currentTimeCT = DateTime.now().setZone("America/Chicago");
  8. const formattedTimeCT = currentTimeCT.toLocaleString(DateTime.TIME_SIMPLE);
  9. const currentTimeBST = DateTime.now().setZone("Europe/London");
  10. const formattedTimeBST = currentTimeBST.toLocaleString(DateTime.TIME_SIMPLE);
  11. const currentTimeCEST = DateTime.now().setZone("Europe/Paris");
  12. const formattedTimeCEST = currentTimeCEST.toLocaleString(
  13. DateTime.TIME_SIMPLE
  14. );
  15. //build the new buttons with updating time after the original buttons are removed
  16. const newButtonZones = new ActionRowBuilder().addComponents(
  17. new ButtonBuilder()
  18. .setCustomId("buttonET")
  19. .setLabel(`EST/EDT - ${formattedTimeET}`)
  20. .setStyle(ButtonStyle.Secondary),
  21. new ButtonBuilder()
  22. .setCustomId("buttonPT")
  23. .setLabel(`PST/PDT - ${formattedTimePT}`)
  24. .setStyle(ButtonStyle.Secondary),
  25. new ButtonBuilder()
  26. .setCustomId("buttonCT")
  27. .setLabel(`CST/CDT - ${formattedTimeCT}`)
  28. .setStyle(ButtonStyle.Secondary),
  29. new ButtonBuilder()
  30. .setCustomId("buttonBST")
  31. .setLabel(`GMT/BST - ${formattedTimeBST}`)
  32. .setStyle(ButtonStyle.Secondary),
  33. new ButtonBuilder()
  34. .setCustomId("buttonCEST")
  35. .setLabel(`CET/CEST - ${formattedTimeCEST}`)
  36. .setStyle(ButtonStyle.Secondary)
  37. );
  38. // Update the button labels
  39. buttonZones.components = newButtonZones.components;
  40. interaction.editReply({ components: [buttonZones] });
  41. }, 30000);

Discord.js V14 ButtonBuilder() buttons stop updating/getting edited after the 15 minute SlashCommand timeout, how do I bypass the timeout?

Like the title states, after the 15 minutes the timestamps inside the buttons no longer update. I imagine this is due to the fact that Discord doesn't allow replies to be edited after 15 minutes.

How do I bypass this and allow for the buttons to be updated permanently? I've seen a few posts pertaining to this subject, but none that deal with ButtonBuilder() updating.

(not sure if this matters but I use Luxon for the timestamps and PM2 to keep the node instance running after I've closed out of the hosting services terminal/console.)

Thank you for any assistance.

答案1

得分: 1

在进行了一些深入的研究后,我在Discord.js指南中找到了这一部分。

> "在初始响应之后,交互令牌在15分钟内有效,因此您可以在此时间范围内编辑响应并发送后续消息。您还不能编辑消息的短暂状态,所以请确保您的第一次响应设置正确。"

所以看起来交互令牌在15分钟后会过期,但实际消息不会。可以这样回复:

  1. // 发送回复,并从发送的消息对象中接收消息
  2. interaction.reply("一些内容/嵌入/其他")
  3. .then((msg) => {
  4. // 回复的消息现在包含在消息变量中。
  5. msg.edit("一些新的内容/嵌入/其他")
  6. // 所有编辑代码理想情况下都应放在这里 :)
  7. // 请注意使用.edit(),而不是.editReply()
  8. })
  9. .catch((e) => {
  10. console.error(e)
  11. })

如果它也过期了,您可以始终存储来自msg变量的消息和通道ID,并在15分钟后重新获取消息:

  1. // 获取通道对象作为"channel"变量
  2. client.guilds.fetch(channelID).then((channel) => {
  3. // 获取消息对象作为"msg"变量
  4. channel.messages.fetch(messageID).then((msg) => {
  5. // 现在可以编辑消息 :)
  6. msg.edit("")
  7. // [...]
  8. })
  9. })

希望这有所帮助!如果这仍然不起作用,请在下面留下评论。

英文:

After doing some heavy research, I found this section in the Discord.js Guide.

> "After the initial response, an interaction token is valid for 15 minutes, so this is the timeframe in which you can edit the response and send follow-up messages. You also cannot edit the ephemeral state of a message, so ensure that your first response sets this correctly."

So it looks like the interaction token times out after 15 mins, but not the actual message. Reply like so:

  1. // Sends the reply, and receives the message object from the sent
  2. interaction.reply("Some content/embeds/whatever")
  3. .then((msg) => {
  4. // The replied message is now contained in the message variable.
  5. msg.edit("Some new content/embed/whatever")
  6. // All edit code should ideally go in here :)
  7. // Note the use of .edit(), not .editReply()
  8. })
  9. .catch((e) => {
  10. console.error(e)
  11. })

If that happens to expire too, you can always store the message and channel ID from that msg variable and re-fetch the message after 15 mins:

  1. // Fetches channel object as "channel" variable
  2. client.guilds.fetch(channelID).then((channel) => {
  3. // Fetches message object as "msg" variable
  4. channel.messages.fetch(messageID).then((msg) => {
  5. // Can now edit the message :)
  6. msg.edit("")
  7. // [...]
  8. })
  9. })

Hopefully this helps! Stick a comment below if this still doesn't work.

huangapple
  • 本文由 发表于 2023年6月16日 10:36:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定