Skip to content

Commit

Permalink
Add User Agent to fetch's and add delay between each member calls to …
Browse files Browse the repository at this point in the history
…lessen the load on the VATSIM services.
  • Loading branch information
vFlyingCowboy committed Feb 23, 2024
1 parent b0e87b3 commit 5a9a938
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
3 changes: 0 additions & 3 deletions .fleet/settings.json

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
config.json
.idea
.run
.fleet
11 changes: 9 additions & 2 deletions assign_roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const limit = pLimit(10);

async function assignRoles(member) {
const MANAGED_ROLES = ["Supervisor", "Administrator", "New Member", "PPL", "IR", "CMEL", "ATPL", "Flight Instructor", "Flight Examiner", "No Military Rating", "M1", "M2","M3","M4"];
const discordCidResponse = await fetch(`https://api.vatsim.net/v2/members/discord/${member.user.id}`).catch(error => console.trace(error));
const discordCidResponse = await fetch(`https://api.vatsim.net/v2/members/discord/${member.user.id}`, {
headers : {
"User-Agent" : 'PTDDiscordBot'
}
}).catch(error => console.trace(error));
if (!discordCidResponse || discordCidResponse.status !== 200) {
console.log(`The discordCidResponse could not be completed as dialed for ${member.displayName}`)
return
Expand All @@ -17,7 +21,10 @@ async function assignRoles(member) {
if (discordCidBody.detail === 'Not Found') {
return;
}
const ratingsResponse = await fetch(`https://api.vatsim.net/v2/members/${cid}`).catch(error => console.trace(error));
const ratingsResponse = await fetch(`https://api.vatsim.net/v2/members/${cid}`, {
headers : {
"User-Agent" : 'PTDDiscordBot'
}}).catch(error => console.trace(error));
if (ratingsResponse.status !== 200) {
console.log(`The ratingsResponse could not be completed as dialed due to a status of ${ratingsResponse.status}`)
}
Expand Down
8 changes: 6 additions & 2 deletions events/ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ module.exports = {
setInterval(async function() {
const guild = client.guilds.cache.get("901078003482783765");
const members = await guild.members.fetch();
members.forEach(member => assignRoles(member))
members.forEach(member =>
setInterval(function (){
assignRoles(member)
},500)
)
console.log("I have assigned everyone's roles");
},60*60*1000)
},2*12*60*60*1000)
},
};

0 comments on commit 5a9a938

Please sign in to comment.