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

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

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

问题

// 每30秒更新按钮标签(或者您设置的时间间隔)
setInterval(() => {
  const currentTimeET = DateTime.now().setZone("America/New_York");
  const formattedTimeET = currentTimeET.toLocaleString(DateTime.TIME_SIMPLE);

  const currentTimePT = DateTime.now().setZone("America/Los_Angeles");
  const formattedTimePT = currentTimePT.toLocaleString(DateTime.TIME_SIMPLE);

  const currentTimeCT = DateTime.now().setZone("America/Chicago");
  const formattedTimeCT = currentTimeCT.toLocaleString(DateTime.TIME_SIMPLE);

  const currentTimeBST = DateTime.now().setZone("Europe/London");
  const formattedTimeBST = currentTimeBST.toLocaleString(DateTime.TIME_SIMPLE);

  const currentTimeCEST = DateTime.now().setZone("Europe/Paris");
  const formattedTimeCEST = currentTimeCEST.toLocaleString(
    DateTime.TIME_SIMPLE
  );

  // 创建新的按钮,在删除原始按钮后附上更新时间
  const newButtonZones = new ActionRowBuilder().addComponents(
    new ButtonBuilder()
      .setCustomId("buttonET")
      .setLabel(`EST/EDT - ${formattedTimeET}`)
      .setStyle(ButtonStyle.Secondary),

    new ButtonBuilder()
      .setCustomId("buttonPT")
      .setLabel(`PST/PDT - ${formattedTimePT}`)
      .setStyle(ButtonStyle.Secondary),

    new ButtonBuilder()
      .setCustomId("buttonCT")
      .setLabel(`CST/CDT - ${formattedTimeCT}`)
      .setStyle(ButtonStyle.Secondary),

    new ButtonBuilder()
      .setCustomId("buttonBST")
      .setLabel(`GMT/BST - ${formattedTimeBST}`)
      .setStyle(ButtonStyle.Secondary),

    new ButtonBuilder()
      .setCustomId("buttonCEST")
      .setLabel(`CET/CEST - ${formattedTimeCEST}`)
      .setStyle(ButtonStyle.Secondary)
  );

  // 更新按钮标签
  buttonZones.components = newButtonZones.components;
  interaction.editReply({ components: [buttonZones] });
}, 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来在关闭托管服务的终端/控制台后继续运行节点实例。)

感谢您的任何帮助。

英文:
// Update button labels every 30 seconds (or whatever you set it at)
setInterval(() => {
const currentTimeET = DateTime.now().setZone("America/New_York");
const formattedTimeET = currentTimeET.toLocaleString(DateTime.TIME_SIMPLE);
const currentTimePT = DateTime.now().setZone("America/Los_Angeles");
const formattedTimePT = currentTimePT.toLocaleString(DateTime.TIME_SIMPLE);
const currentTimeCT = DateTime.now().setZone("America/Chicago");
const formattedTimeCT = currentTimeCT.toLocaleString(DateTime.TIME_SIMPLE);
const currentTimeBST = DateTime.now().setZone("Europe/London");
const formattedTimeBST = currentTimeBST.toLocaleString(DateTime.TIME_SIMPLE);
const currentTimeCEST = DateTime.now().setZone("Europe/Paris");
const formattedTimeCEST = currentTimeCEST.toLocaleString(
DateTime.TIME_SIMPLE
);
//build the new buttons with updating time after the original buttons are removed
const newButtonZones = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId("buttonET")
.setLabel(`EST/EDT - ${formattedTimeET}`)
.setStyle(ButtonStyle.Secondary),
new ButtonBuilder()
.setCustomId("buttonPT")
.setLabel(`PST/PDT - ${formattedTimePT}`)
.setStyle(ButtonStyle.Secondary),
new ButtonBuilder()
.setCustomId("buttonCT")
.setLabel(`CST/CDT - ${formattedTimeCT}`)
.setStyle(ButtonStyle.Secondary),
new ButtonBuilder()
.setCustomId("buttonBST")
.setLabel(`GMT/BST - ${formattedTimeBST}`)
.setStyle(ButtonStyle.Secondary),
new ButtonBuilder()
.setCustomId("buttonCEST")
.setLabel(`CET/CEST - ${formattedTimeCEST}`)
.setStyle(ButtonStyle.Secondary)
);
// Update the button labels
buttonZones.components = newButtonZones.components;
interaction.editReply({ components: [buttonZones] });
}, 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分钟后会过期,但实际消息不会。可以这样回复:

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

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

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

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

英文:

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:

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

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:

// Fetches channel object as "channel" variable
client.guilds.fetch(channelID).then((channel) => {
  // Fetches message object as "msg" variable
  channel.messages.fetch(messageID).then((msg) => {
    // Can now edit the message :)
    msg.edit("")
    // [...]
  })
})

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:

确定