我可以在哪里放置异步函数,以便我可以使用 await 发送不一致的响应?

Where can I put an async function so I can use await to send a discord response?

我正在从 Google Sheet 中提取数据,在一些帮助之后,我已经达到了 discord 机器人以正确的数据响应查询的地步,但它的响应是地图数组的每一行都在单独的响应中,而不是收集数据并将其发送到一条消息中。所以我相信我需要使用异步函数和等待功能。我似乎无法弄清楚将其放入何处或如何放入。我已经坚持了几个小时,似乎无法让它工作?

这是我的代码:

const { google } = require('googleapis');
const { sheets } = require('googleapis/build/src/apis/sheets');
const keys = require('../Data/client_secret.json');
const { Team, DiscordAPIError } = require('discord.js');
const Command = require("../Structures/Command");

const gclient = new google.auth.JWT(
    keys.client_email, 
    null, 
    keys.private_key,
    ['https://www.googleapis.com/auth/spreadsheets']
);


module.exports = new Command({
    name: "freeagents",
    description: "Potential free agents for next season.",
    async run(message, args, client) {

        gclient.authorize(function(err,tokens){

            if(err){
                console.log(err);
                return;
            } else {
                console.log("Connected!");
                gsrun(gclient);
            }
        
        });

    async function gsrun(cl){

        const gsapi = google.sheets({version:'v4', auth: cl });
        gsapi.spreadsheets.values.get({
            spreadsheetId: "11e5nFk50pDztDLngwTSmossJaNXNAGOaLqaGDEwrbQM",
            range: 'Keepers!C1:F',
            
        }, (err, res) => {
            if (err) return console.log('The API returned an error: ' + err);
            const rows = res.data.values;
    
            if (rows.length) {
              const cells = rows.filter(cell => cell[3])
              cells.map((cell) => {
                
                console.log(`${cell[0]}, ${cell[1]}`);
                console.log(`${cells}`);
                return message.reply(`${cell[0]}, ${cell[1]}`);
              });
            } else {
              console.log('No data found.');
            }
    
           
        })
    
    };
}
});

如有任何帮助,我们将不胜感激!

您收到多条消息,因为您是在 cells.map((cell) => {}) 内发送消息。该方法将对数组的每个成员调用一个函数。您应该迭代单元格数组以生成一个字符串,然后您可以将其作为消息发送,例如:

const strings = [];

for (let cell of cells) {
    strings.push(`${cell[0]}, ${cell[1]}`);
}

return message.reply(strings.join("\n"));