【问题标题】:(node:14000) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message(节点:14000)UnhandledPromiseRejectionWarning:DiscordAPIError:无法发送空消息
【发布时间】:2021-12-15 09:28:02
【问题描述】:

所以我正在尝试创建一个命令,它生成可能是中奖代码的代码,我希望代码继续重复/发送,并且每个人都必须复制 1 个数字。但我不断收到此错误,它没有发送消息:

(node:14000) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message

lottery.js =

代码如下:


const Discord = require('discord.js');

module.exports = {
    name: 'lottery',
    async execute(message, args) {
        const lottery = require('../text.js');
        setInterval(() => {
            const item = lottery[Math.floor(Math.random() * lottery.length)];
            message.channel.send(item);
        }, 5 * 1000);
    }
}

text.js =

`w2H1
DmKS
wSLS
yRpQ
8J57
0qlq
hRRc
Cn8G
B0FL
D2H0
k3dD
jchM
MWyN
VK5m
NllW
CKxJ
0mKq
b4rR
Bl5t
dDdt
5MT0
84s1
7DwG
mzDW
06rl
R3Hl
fG82
tnbt
lc02
6TDh
MySv
FgBq
3g30
qB6B
QQyq
wQMr
nMg5
PvK9
jL72
zZ8z`

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    你的text.js 有很多换行符,所以item === '\n' 有时因为item 是 来自lottery 的随机字符。当您执行 message.channel.send('\n') 时,Discord 会抛出“无法发送空消息”错误,因为它将仅包含空格的消息视为空消息。

    我认为您打算让item 成为lottery 的整个 而不仅仅是一个字符,因此您可以将代码更改为:

    const text = require('../text.js');
    const lottery = text.split('\n');
    const item = lottery[Math.floor(Math.random() * lottery.length)];
    \\ ...
    

    【讨论】:

    • 现在我收到一个新错误:TypeError: require(...).split is not a function。我也照你说的做了,但我不完全知道该怎么做。
    • @Outrage 手动编辑您的文件,使其类似于module.exports = `w2H1\nDmKS\nwSLS etc...`
    • 它只是开始发送随机字母而不是单词。有解决办法吗?
    • 字母我的意思是整个事情的1个数字
    • lottery 应该是一个字符串的array,而不仅仅是一个字符串。我已经更新了我的答案,假设 text.js 使用 module.exports =
    猜你喜欢
    • 2021-05-05
    • 2020-01-25
    • 2020-12-20
    • 2020-12-01
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    相关资源
    最近更新 更多