How to avoid having “quota exceeded for 'ReadGroup'” error in google api

∥☆過路亽.° 提交于 2019-12-11 15:59:09

问题


I've created a bot which goes in Google Spreadsheet getting some datas before sending them by DM to 50 guild members into Discord.

However, due to high requests of datas, I've got an error message saying that I've exceeded the quota for group 'ReadGroup' and limit 'USER-100s'.

To avoid getting this error, I've created a buffer function however it still doesn't work, does anyone know how to avoid getting this limit error?

Here is the main code which is launched when I type a specific commande in 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;
        }
    }
}

Here is the first function called ("liste") which return the list of the 50 members 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;
}

And here is the second function called ("envoi") which is supposed to send the DM to the 50 different members of the guild :

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";
}

As a result, I would like to get all the members to get their DM. However after the 18th member, an error appear, even though I put some buffering time.

In the console.log, I get :

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

This is even more strange that the error concerns a member who already have received his DM (he is one the the first 10 members in the list)


回答1:


Thanks to Tanaike, I updated my code using spreadsheets.values.batchGet() method. In that way instead of extraction values by values, I extracted a batch of values.

And then I made my formula. Now I don't have any issues anymore and even better, my script is way much quicker :)



来源:https://stackoverflow.com/questions/56987347/how-to-avoid-having-quota-exceeded-for-readgroup-error-in-google-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!