如何按间隔编辑嵌入消息?

时间:2020-08-04 22:13:25

标签: javascript discord discord.js setinterval

var dateTime = time+' '+date;

 const boosterembed = new Discord.RichEmbed()
            .setTitle("Boosters")
            .setColor('#ff0000')
            .setDescription("Last Time updated: " + dateTime)
            .setTimestamp()
setInterval(function(){guild.channels.get('740327839177375765').message.channel.fetchMessages('740327893103673466').edit(boosterembed)}, 1000)

为什么这不起作用?无法添加更多内容(看起来您的帖子大部分是代码)

2 个答案:

答案 0 :(得分:1)

我假设您正在使用Discord.js v11。

首先,fetchMessages用于从一个通道中获取多个消息。您将改为使用fetchMessage。其次,使用edit编辑消息。第三,频道没有message属性,因此guild.channels.get(...).message.channel将引发错误。

setInterval(
  () =>
    guild.channels.get('740327839177375765').fetchMessage('740327893103673466')
      .then(message => message.edit(boosterembed))
      // This catches all errors and logs them to stop unhandled promise rejections
      .catch(console.error),
  1000
)

// Or with async/await:
setInterval(
  async () => {
    try {
      const message = await guild.channels.get('740327839177375765').fetchMessage('740327893103673466')
      await message.edit(boosterembed)
    } catch (error) {
      console.error(error)
    }
  },
  1000
)

答案 1 :(得分:0)

此答案改编自cherryblossom的答案,请确保您投票赞成。

function getDate() {
  var today = new Date();
  var date = today.getDate()+'/'+(today.getMonth()+1)+'/'+today.getFullYear();
  var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
  return time + ' ' + date;
}

setInterval(
  () =>
    bot.channels.get('740327839177375765').fetchMessage('740327893103673466')
      .then(message => message.edit(new Discord.RichEmbed(boosterembed).setDescription('Last Time updated: ' + getTime())))
      // This catches all errors and logs them to stop unhandled promise rejections
      .catch(console.error),
  1000
)

// Or with async/await:
setInterval(
  async () => {
    try {
      const message = await bot.channels.get('740327839177375765').fetchMessage('740327893103673466')
      await message.edit(new Discord.RichEmbed(boosterembed).setDescription('Last Time updated: ' + getTime()))
    } catch (error) {
      console.error(error)
    }
  },
  1000
)