【问题标题】:making the embed change color during a time discord.js使嵌入在一段时间内改变颜色 discord.js
【发布时间】:2020-10-05 12:30:11
【问题描述】:
    case 'test':
      let timed = "10s"
      const tests = new Discord.RichEmbed()
      .setTitle("tets")
      .setColor('#000000')

      message.channel.send(tests);
      setTimeout(function(){
      tests.setColor('#5e4242')
      }, ms(timed));

      break;

所以我试图让嵌入的颜色在 10 秒后发生变化,这是一个测试命令,所以它被称为测试。我尝试了很多方法,我搜索了谷歌并显示了这一点,所以我决定尝试它,它完全没有做任何事情。我使用richembed 是因为当我使用messagemebed 时它显示客户端错误

【问题讨论】:

  • 使用不同颜色的相同嵌入编辑消息。 MessageEmbed 可能无法正常工作,因为您没有使用 Discord.js v12。

标签: javascript discord discord.js embed


【解决方案1】:

在发送消息后设置颜色将编辑对象但不会编辑该消息,您可以使用相同的消息对象编辑消息但在更改颜色后也可以

    let timed = "10s"
      const tests = new Discord.RichEmbed()
      .setTitle("tets")
      .setColor('#000000');

    var testMsg = message.channel.send(tests).then(
      setTimeout(function(){
         tests.setColor('#5e4242');
         testMsg.edit(tests); // edit the message with the same object but different color
      }, ms(timed));
)

      break;

【讨论】:

  • 这行不通。 tests.edit(tests),tests 不是消息,只有嵌入,所以你需要将 message.channel.send 设置为一个变量,然后编辑它。还应该注意 message.channel.send 是异步的。
  • 是的,你是对的,我会更新我的答案,谢谢!
  • 那还是不行,message.channel.send返回一个promise,所以你需要等待它解决,你可以使用await或者.then()
【解决方案2】:

您需要将发送的消息设置为变量,然后编辑该消息。

  let timed = "10s"
  const tests = new Discord.RichEmbed()
  .setTitle("tets")
  .setColor('#000000')

  const res = await message.channel.send(tests);
  setTimeout(function(){
     tests.setColor('#5e4242');
     res.edit(tests);
  }, ms(timed));

还应注意,await 仅适用于异步函数

【讨论】:

    【解决方案3】:

    这里是V12版本

        let embed = new Discord.MessageEmbed()
        .setColor('#ffffff')
        .setTitle('I am a colour-changing embed')
    
        let msg = await message.channel.send(embed)
    setTimeout(() => {
         embed.setColor('#5e4242');
         msg.edit(embed);
      }, /*Delay*/);
    

    这段代码创建了一个新的嵌入,然后创建了一个 let 变量,用于发送消息[有点像发送带有标签的消息]。然后它需要 embed 变量编辑颜色并在延迟后编辑上一条消息。

    【讨论】:

    猜你喜欢
    • 2020-06-28
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 2020-04-23
    • 2016-04-02
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    相关资源
    最近更新 更多