我正在设置我的第一个discord机器人,它将能够从Google Spreadsheet的官方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:未定义单元”
答案 0 :(得分:0)
我在您的代码中发现了一些问题:
accessSpreadsheet()
不返回它访问的任何值。accessSpreadhseet()
返回一个Promise,因为它被声明为async,但从未等待过。cell
的{{3}}在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);