如何避免Google API中出现“超出'ReadGroup'的配额”错误

时间:2019-07-11 10:39:39

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

我创建了一个机器人,该机器人会在Google Spreadsheet中获取一些数据,然后再通过DM将其发送给50个公会成员加入Discord。

但是,由于对数据的高要求,我收到一条错误消息,提示我已经超出了“ ReadGroup”组的配额并限制了“ USER-100s”。

为避免发生此错误,我创建了一个缓冲函数,但是它仍然不起作用,有人知道如何避免出现此限制错误吗?

这是我在Discord中键入特定命令时启动的主要代码:

const client1 = new google.auth.JWT(keys.client_email, null, keys.private_key, ['https://www.googleapis.com/auth/spreadsheets']);
const client2 = new discord.Client(); 
.
.
.
let valeur1 = await liste(client1);
await sleep(100000);
console.log("End of first buffering time (100s)");
for (i = 0; i < valeur1.length; i++){
    if (valeur1[i] != undefined){
        try{
            let valeur2 = await envoi(client1, valeur1[i]);             
            const user = client2.users.get(String(valeur1[i])) || await client2.fetchUser(String(valeur1[i]));
            console.log("Ready to send to : " + user.id);
            await user.send("The character you need to improve is : " + valeur2[0] + "\n 1. " + valeur2[1] + " = " + valeur2[2] + " >>> " + valeur2[3] + "\n 2. " + valeur2[4] + " = " + valeur2[5] + " >>> " + valeur2[6] + "\n 3. " + valeur2[7] + " = " + valeur2[8] + " >>> " + valeur2[9]);
            console.log("Message sent for : " + user.id);
        } 
        catch(err){
            console.error(err);
            console.log("Error detected for : " + valeur1[i]);
            break;
        }
    }
}

这是第一个称为(“ liste”)的函数,该函数返回50个成员id的列表:

async function liste(client){
    const gsapi = google.sheets({version:'v4',auth: client});
    let data1 = new Array();

    for (i = 0; i < 50; i++) {
        const opt1 = {spreadsheetId: 'XXXXXX', range: 'Serveur!C' + (3+i)};
        let data2 = await gsapi.spreadsheets.values.get(opt1);  
        data1.push(data2.data.values);
    }
    return data1;
}

这是第二个函数(“ envoi”),该函数应该将DM发送给公会的50个不同成员:

async function envoi(client, id){   
    const gsapi = google.sheets({version:'v4',auth: client});
    for (i = 0; i < 50; i++){
        const opt1 = {spreadsheetId: 'XXXXXX', range: 'Discord!A' + (3+i)};
        let data1 = await gsapi.spreadsheets.values.get(opt1);
        if (parseInt(id) === parseInt(data1.data.values)){                                  
            const opt2 = {spreadsheetId: 'XXXXXX', range: 'Discord!C' + (3+i)};
            let data2 = await gsapi.spreadsheets.values.get(opt2);
            const opt3 = {spreadsheetId: 'XXXXXX', range: 'Discord!D' + (3+i)};
            let data3 = await gsapi.spreadsheets.values.get(opt3);
            .     
            .
            .
            const opt10 = {spreadsheetId: 'XXXXXX', range: 'Discord!K' + (3+i)};
            let data10 = await gsapi.spreadsheets.values.get(opt10);
            const opt11 = {spreadsheetId: 'XXXXXX', range: 'Discord!L' + (3+i)};
            let data11 = await gsapi.spreadsheets.values.get(opt11);

            var stats = [data2.data.values,data3.data.values,data4.data.values,data5.data.values,data6.data.values,data7.data.values,data8.data.values,data9.data.values,data10.data.values,data11.data.values];

            await sleep(10000);
            console.log("Extraction done for " + parseInt(id));
            return stats;
        }
    }
console.log("Member not found");
return "erreur";
}

因此,我想让所有成员获得他们的DM。但是,在第18位成员之后,即使我花了一些缓冲时间,也会出现错误。

在console.log中,我得到:

End of first buffering time (100s)
Extraction done for 408575708424699900
Ready to send to : 408575708424699925
Message sent for : 408575708424699925
.
.
.
Extraction done for 438420776652374000
Ready to send to : 438420776652374036
Message sent for : 438420776652374036
Error: Quota exceeded for quota group 'ReadGroup' and limit 'USER-100s' of service 'sheets.googleapis.com'
.
.
.
Error detected for : 493854774446391296

更奇怪的是,该错误涉及到已经收到其DM的成员(他是列表中前10名成员之一)

1 个答案:

答案 0 :(得分:0)

感谢Tanaike,我使用sheets.values.batchGet()方法更新了代码。这样,我提取了一批值,而不是按值提取值。

然后我制定了公式。现在我没有任何问题了,甚至更好,我的脚本要快得多:)