【问题标题】:How can I use google sheets api data in discord.js embed?如何在 discord.js 嵌入中使用 google sheet api 数据?
【发布时间】:2019-11-16 10:07:19
【问题描述】:

我正在设置我的第一个 discord 机器人,它将能够从 Google 电子表格、官方 API 中获取数据,并将其作为嵌入消息引入 discord。问题出在 .addField() 级别,我无法输入单元格的值。我该怎么做?

const { Client, RichEmbed } = require('discord.js');
const client= new Client();
const GoogleSpreadsheet = require('google-spreadsheet');
const {promisify} = require('util');
const creds = require('./client_secret.json');

client.on('message', message => {
    if (message.content === '!bot'){

        async function accessSpreadsheet() {
            const doc = new GoogleSpreadsheet('1qA11t460-ceILmwu6RtfiPGb_n9MUD_7z6Ld7I_Z6yc');
            await promisify(doc.useServiceAccountAuth)(creds);
            const info = await promisify(doc.getInfo)();
            var sheet = info.worksheets[0];

            var cells = await promisify(sheet.getCells)({
                'min-row': 2,
                'max-row': 5,
                'min-col': 3,
                'max-col': 3,
                'return-empty': true,
            })
            for (var cell of cells) {
                message.author.send(cell.value)
            }
        }

        accessSpreadsheet();
        const embede = new RichEmbed()
    .setColor('#0099ff')
    .setTitle("My Title")
    .addBlankField()
    .setDescription('Some description')
    .addBlankField()
    .addField('Name', '•'+ cell[1].value , true)
    .setTimestamp();

        message.author.send(embede) }
})
client.login('xxx')

我希望输出“Terrassycup 3”,但实际输出是 console.log 中的“ReferenceError: cell is not defined”

【问题讨论】:

    标签: javascript node.js google-sheets-api discord.js


    【解决方案1】:

    我在您的代码中看到的一些问题:

    • accessSpreadsheet() 不返回它访问的任何值。
    • accessSpreadhseet() 返回 Promise,因为它被声明为 async,但从未等待。
    • cellscope 位于 for...of 循环内 accessSpreadsheet() 函数内,但您尝试在它之外很好地使用它。

    您可以将它们作为字段添加到嵌入中(限制为 25 个)您声明的函数中,而不是将每个单独的值发送给用户。

    async function accessSpreadsheet(embed) {
      // Insert the code already being used up to the for loop.
    
      for (let i = 0; i < 25 && cells[i]; i++) embed.addField('Name', `•${cells[i].value}`, true);
    }
    
    var embed = new RichEmbed()
      .setColor('#0099ff')
      .setTitle('**Spreadsheet Info**')
      .setDescription('Showing as many values as possible...');
    
    accessSpreadsheet(embed)
      .then(() => message.author.send(embed))
      .catch(console.error);
    

    【讨论】:

    • 谢谢你的回答,但我不明白 "i" [for (let i = 0; i •${cells[i].value}, true);] 来自,它是做什么的。能给我解释一下吗?
    • 这个for 循环使用一个“step”变量(i)。如果条件 i &lt; 25(您只能将 25 个字段添加到嵌入)和 cells[i](该索引处有一个单元格)条件 not 都为 true,则循环将停止。每次执行代码时,它都会将步骤加 1 以继续遍历数组。 embed.addField(...) 将每个单元格的值添加到给定的嵌入中。 ${...} 是一个 template literal/嵌入式表达式。
    • 好的,我该怎么做才能输入“1”。在第一个单元格[i].value,一个“2”。在第二个,...?
    • 使用i + 1i是从零开始匹配数组的索引),例如${i + 1} ${cells[i].value}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    相关资源
    最近更新 更多