如何为 Discord 拆分和发布多条消息

发布于 2025-01-10 07:15:42 字数 1343 浏览 0 评论 0原文

从其 用户反馈,所以我必须把我的消息分成几块,然后一一发送。然而,我在最后一步被阻止了。

我的 sendMessage 调用 Discord 的 发送方法

function sendMessage(chanID, msg) {
  return discordbot.channels.cache.get(chanID).send(msg)
}

我已经将长文本分解为一个名为 part 的数组。这就是我尝试发送它们的方式:

  if (msg.length < 2000) {
    return sendMessage(chanID, msg)
  }

  . . . 

  parts.map(async function(part) {
    return await sendMessage(chanID, part)
  })

第一部分有效,但显然最后一部分无效,因为我的函数最终应该返回 Discord 的 send 方法,这是一个 promise,而最后一个循环不是。

但是,我对 nodejs/promise 不太了解,无法自己修复它。改编自 https://stackoverflow.com/a/24985483/2125837 的答案,这是我的看法:

function sendAll(chanID, parts) {
    return parts.reduce(function(promise, msg) {
        return promise.then(function() {
            sendMessage(chanID, msg)
        });
    }, Promise.resolve());
}

请帮忙。

Discord's 2000 character limit is the most unwelcome restriction, judging from its user feedback, so I have to break up my message in chunks, and send them one by one. However, I'm blocked at the last step.

My sendMessage calls the Discord's send method:

function sendMessage(chanID, msg) {
  return discordbot.channels.cache.get(chanID).send(msg)
}

And I've already broken down the long text into an array called part. This is how I'm trying to send them:

  if (msg.length < 2000) {
    return sendMessage(chanID, msg)
  }

  . . . 

  parts.map(async function(part) {
    return await sendMessage(chanID, part)
  })

The first part is working but obviously the last part is not, as my function should return Discord's send method eventually, which is a promise, and the last loop is not.

However, I don't know nodejs/promise well enough to fix it myself. Adapting from the answer from https://stackoverflow.com/a/24985483/2125837, here is my take:

function sendAll(chanID, parts) {
    return parts.reduce(function(promise, msg) {
        return promise.then(function() {
            sendMessage(chanID, msg)
        });
    }, Promise.resolve());
}

Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲念泪 2025-01-17 07:15:42

如果您将地图功能替换为以下内容,它可能会起作用。我还没有实际测试过代码。

parts.reduce(function(promise, part) {
  return promise.then(function() {
    return sendMessage(chanID, part)
  });
}, Promise.resolve());

If you replace your map function with the following, it will likely work. I haven't actually tested the code.

parts.reduce(function(promise, part) {
  return promise.then(function() {
    return sendMessage(chanID, part)
  });
}, Promise.resolve());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文