【问题标题】:discord.js random video bot adds token to ends of video namediscord.js 随机视频机器人将令牌添加到视频名称的末尾
【发布时间】:2019-10-12 02:06:51
【问题描述】:

编译这个简单的脚本以发送一个随机视频以在他询问时使用,但问题是当我使用流式链接时,它会在 .mp4 之后添加一个令牌 那么如何让它在令牌之后剪切所有内容并发送文件?

我试图切断令牌,它只是发送一个我试图上传到其他服务的链接,它也是如此。 我无法在本地上传,因为我在 glitch.com 上托管 任何帮助,将不胜感激。

//nyan dance//
const nyan_dance = [
'https://cdn-b-east.streamable.com/video/mp4/pepek.mp4token=FrRv_gQneT012mzlC7dRCA&expires=570819200',
]
client.on('message', msg => {
  if (msg.content.includes("dance nyan!")) {
    msg.channel.send("here we go", {
    file: nyan_dance[Math.floor(Math.random() * nyan_dance.length)]
});
  }
});

我希望它做它做的事情,但在 .mp4 之后剪切文件名中的令牌并上传它,如果这是一个愚蠢的问题,我很抱歉,我不是编码器。

【问题讨论】:

    标签: javascript bots discord discord.js


    【解决方案1】:

    您可以使用正则表达式来完成此操作,例如:

    /token.+/
    

    下面是它的工作原理:

    /
      token //matches the literal word "token"
      .+    //matches 1 or more characters of any kind
    /
    

    我们可以使用replace 方法将其应用于您的示例:

    //nyan dance//
    const nyan_dance = [
    'https://cdn-b-east.streamable.com/video/mp4/pepek.mp4token=FrRv_gQneT012mzlC7dRCA&expires=570819200',
    ]
    client.on('message', msg => {
      if (msg.content.includes("dance nyan!")) {
        msg.channel.send("here we go", {
          file: nyan_dance[Math.floor(Math.random() * nyan_dance.length)].replace(/token.+/, "")
        });
      }
    });
    

    【讨论】:

    • 感谢您的帮助,很抱歉打扰您,但控制台中显示 TypeError: Math.floor(...).replace is not a function 我做错了什么吗?再次抱歉,我不是程序员
    • 很抱歉,我错误地将替换函数放在了 Math.floor 函数之后,而不是使其应用于数组中选取的元素。我编辑了我的答案。谢谢你告诉我!
    • 再次感谢您的修复,我希望它不会打扰您,但现在它说“未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数”我用谷歌搜索它并试图提出一个解决方案,但似乎没有人有正确的答案或我理解的答案,你能帮我吗?谢谢你,很抱歉因为这么简单的事情打扰你。
    • 当一个 promise 调用它的 reject 函数时会发生这种情况。一个简单的解决方法是在 promise 调用之后通过 .catch((error) => console.error(error)); 捕获拒绝。
    • 拒绝似乎是因为您使用file而不是files,只需将您的消息发送代码更改为:msg.channel.send("here we go", {files: [nyan_dance[Math.floor(Math.random() * nyan_dance.length)].replace(/token.+/, "")]});
    猜你喜欢
    • 2021-03-24
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多