如何为 Discord 拆分和发布多条消息
从其 用户反馈,所以我必须把我的消息分成几块,然后一一发送。然而,我在最后一步被阻止了。
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将地图功能替换为以下内容,它可能会起作用。我还没有实际测试过代码。
If you replace your map function with the following, it will likely work. I haven't actually tested the code.