如何避免Google API中出现“超出'ReadGroup'的配额”错误 [英] How to avoid having “quota exceeded for 'ReadGroup'” error in google api
问题描述
我创建了一个机器人,该机器人会在Google Spreadsheet中获取一些数据,然后再通过DM将这些数据发送给50个公会成员加入Discord。
I've created a bot which goes in Google Spreadsheet getting some datas before sending them by DM to 50 guild members into Discord.
但是,由于请求量很高数据中,我收到一条错误消息,说我已经超出了组'ReadGroup'的配额并限制了'USER-100s'。
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?
以下是主要代码:当我在Discord中键入特定命令时启动:
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;
}
}
}
第一个函数是( liste)返回50个成员ID的列表:
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;
}
这是第二个名为( envoi)的函数将DM发送到行会的50个不同成员:
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";
}
因此,我想让所有成员获得他们的DM。 。但是,在第18位成员之后,即使我花了一些缓冲时间,也会出现一个错误。
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.
在console.log中,我得到了:
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
更奇怪的是,该错误与某个成员有关已经收到了他的DM(他是列表中的前10名成员之一)
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)
推荐答案
感谢Tanaike,我更新了我的使用sheets.values.batchGet()方法的代码。这样,我提取了一批值,而不是按值提取值。
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 :)
这篇关于如何避免Google API中出现“超出'ReadGroup'的配额”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!