简体   繁体   English

Discord.js 表示“消息为空”但 console.log 显示数据

[英]Discord.js says "message is empty" but console.log shows data

I'm currently working on a project that sends the information of my google-spreadsheets to a discord channel.我目前正在开展一个项目,将我的 google 电子表格的信息发送到 discord 频道。 So, I have some questions:所以,我有一些问题:

1) The final data (value), when I try to console.log(value) , it worked! 1)最终数据(值),当我尝试console.log(value)时,它起作用了!

So that means it does get the data from google-spreadsheets and set it to (value).所以这意味着它确实从 google-spreadsheets 获取数据并将其设置为(值)。

But when I try to do msg.channel.send(value) , it responds to me with a crash and says can't send an empty message which makes me confused, why?但是当我尝试执行msg.channel.send(value)时,它以崩溃响应我并说不能发送空消息这让我感到困惑,为什么?

And therefore I can't send this value to my discord channel.因此我无法将此值发送到我的 discord 频道。


2) My final data (value) as shown at the bottom of my code, is actually an array like this: 2)我的代码底部显示的最终数据(值)实际上是这样的数组:

{ values: [ [A] , [B] , [C] , [D] , ... ] }

And I don't know how to change them into A,B,C,D form.而且我不知道如何将它们更改为A,B,C,D形式。 Does anyone know how to convert them?有谁知道如何转换它们?

const { google } = require('googleapis')
async function google_sheet_get() {
    const auth = new google.auth.GoogleAuth({
        keyFile: "./google_sheets/Google_credentials.json",
        scopes: "https://www.googleapis.com/auth/spreadsheets"
    })
    
    const client = await auth.getClient()
    const googleSheets = google.sheets({version: "v4", auth: client})
    const spreadsheetId = "My Sheet ID"
    const getRows = await googleSheets.spreadsheets.values.get({
        auth,
        spreadsheetId,
        majorDimension: "ROWS",
        range: "'Day1'!B1:F24"
    })
    return getRows.data
}

client.on('messageCreate', msg=> {
    if(msg.content.startsWith(settings.prefix+'ttes')||
    msg.content.startsWith(settings.prefix2+'ttes')) {
        sheet.get().then( value => msg.channel.send(value))
        msg.channel.send("Command ttes success.")
    }
})

Really appreciate the help, thank you.真的很感谢帮助,谢谢。

I'm guessing that value is not a string, when you look at channel.send() , it accepts either string, "MessagePayload" object or "MessageOptions" object, so if the value is an object it tries to interpret it as either one of those and it fails because the required properties aren't there.我猜该value不是字符串,当您查看channel.send()时,它接受字符串“MessagePayload”object 或“MessageOptions”object,因此如果该value object,它会尝试将其解释为其中之一失败,因为所需的属性不存在。

Try converting it to string before sending it, like: msg.channel.send(`${value}`) or msg.channel.send(value.toString() .在发送之前尝试将其转换为字符串,例如: msg.channel.send(`${value}`)msg.channel.send(value.toString()


And as for the second part of the question you might want to take a look at Array.flat() .至于问题的第二部分,您可能想看看Array.flat()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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