我收到此错误,我该如何解决? DiscordAPIError:无法发送空消息

时间:2020-08-25 23:27:12

标签: javascript discord.js

该如何解决?
错误:UnhandledPromiseRejection警告:DiscordAPI错误:无法发送空消息

代码:

let y = process.openStdin()
y.addListener('data', res => {
    let x = res.toString().trim().split(/ +/g)
    if (!x) return;
    client.channels.cache.get("736725455599697980").send(x.join(" "));
});

1 个答案:

答案 0 :(得分:0)

问题来自此行:

if (!x) return;

您应该使用x[0]作为返回条件,而不仅仅是x。空数组仍然是数组,因此可以毫无问题地通过您当前的要求。

但是,discord.js无法发送空数组,这就是为什么出现错误的原因。


快速演示:

const example = () => {

// even if x is an empty array
let x = [];

// it will still be defined, thus passing your condition
if (x) return;

// however, when you try to send it as a string, nothing shows up,
// as it is empty. that's why you're getting you're error
console.log(x.toString());
};

example()