简体   繁体   中英

I got this error, how can i fix? DiscordAPIError: Cannot send an empty message

How can I fix this?
Error: UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message

Code:

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(" "));
});

The problem comes from this line:

if (!x) return;

You should use x[0] as your return condition instead of just x . An empty array is still an array, and can thus pass your current requirement without issue.

However, discord.js cannot send an empty array, which is why you're getting your error.


Quick Demonstration:

 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM