cache work
This commit is contained in:
@@ -2,3 +2,4 @@ node_modules
|
|||||||
.git
|
.git
|
||||||
config.json
|
config.json
|
||||||
state.json
|
state.json
|
||||||
|
cache.json
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -140,4 +140,5 @@ dist
|
|||||||
|
|
||||||
config.json
|
config.json
|
||||||
state.json
|
state.json
|
||||||
|
cache.json
|
||||||
docker-compose.yml
|
docker-compose.yml
|
||||||
@@ -15,7 +15,6 @@ module.exports = {
|
|||||||
(KZNKing.faction.position === "Leader") ? message += `He is leader of ${KZNKing.faction.faction_name}. ` : message += `He is not leader of his faction. `;
|
(KZNKing.faction.position === "Leader") ? message += `He is leader of ${KZNKing.faction.faction_name}. ` : message += `He is not leader of his faction. `;
|
||||||
|
|
||||||
const company = (await torn.company(KZNKing.job.company_id));
|
const company = (await torn.company(KZNKing.job.company_id));
|
||||||
console.log(company);
|
|
||||||
const jobEmbed = new EmbedBuilder()
|
const jobEmbed = new EmbedBuilder()
|
||||||
.setTitle(company.name)
|
.setTitle(company.name)
|
||||||
.setURL(`https://www.torn.com/joblist.php#/p=corpinfo&ID=${company.ID}`)
|
.setURL(`https://www.torn.com/joblist.php#/p=corpinfo&ID=${company.ID}`)
|
||||||
@@ -55,7 +54,6 @@ module.exports = {
|
|||||||
value: String(company.rating),
|
value: String(company.rating),
|
||||||
inline: true
|
inline: true
|
||||||
}
|
}
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const faction = await torn.faction.basic(KZNKing.faction.faction_id)
|
const faction = await torn.faction.basic(KZNKing.faction.faction_id)
|
||||||
@@ -88,10 +86,36 @@ module.exports = {
|
|||||||
value: `${faction.rank.wins}`,
|
value: `${faction.rank.wins}`,
|
||||||
inline: true
|
inline: true
|
||||||
},
|
},
|
||||||
|
);
|
||||||
|
|
||||||
)
|
let companyFemales = 0;
|
||||||
|
const companyFemalePromises = Object.entries(company.employees).map(([user]) => {
|
||||||
|
return torn.cache.user(user).then(data => {
|
||||||
|
if (data.gender === "Female") {
|
||||||
|
companyFemales++;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
let factionFemales = 0;
|
||||||
|
const factionMembers = await torn.faction.members(KZNKing.faction.faction_id);
|
||||||
|
const factionFemalePromises = factionMembers.map((user) => {
|
||||||
|
return torn.cache.user(user.id).then(data => {
|
||||||
|
if (data.gender === "Female") {
|
||||||
|
factionFemales++;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// i hate async
|
||||||
|
await Promise.all(companyFemalePromises);
|
||||||
|
await Promise.all(factionFemalePromises);
|
||||||
|
|
||||||
|
const companyFemalePercent = (companyFemales / company.employees_capacity) * 100;
|
||||||
|
const factionFemalePercent = (factionFemales / faction.capacity) * 100;
|
||||||
|
|
||||||
|
message += `\nbtw lol his company has ${companyFemales}/${company.employees_capacity} female employees and ${factionFemales}/${faction.capacity} female faction members\n`;
|
||||||
|
message += `thats ${companyFemalePercent.toFixed(2)}% and ${factionFemalePercent.toFixed(2)}% respectively, and last i checked, torn has a 13.43% female population`;
|
||||||
interaction.reply({ content: message, embeds: [jobEmbed, facEmbed] });
|
interaction.reply({ content: message, embeds: [jobEmbed, facEmbed] });
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
59
torn.js
59
torn.js
@@ -1,11 +1,18 @@
|
|||||||
|
const fs = require('fs');
|
||||||
let config;
|
let config;
|
||||||
try {
|
let cache;
|
||||||
config = require('./config')
|
try {config = require('./config.json')} catch {return}
|
||||||
} catch {
|
try {cache = require('./cache.json')} catch {
|
||||||
return
|
cache = {
|
||||||
|
items: {},
|
||||||
|
users: {},
|
||||||
|
factions: {},
|
||||||
|
companies: {}
|
||||||
|
};
|
||||||
|
fs.writeFileSync('./cache.json', JSON.stringify(cache));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
module.exports = () => {};
|
module.exports = () => {};
|
||||||
module.exports.readyCheck = async (key) => {
|
module.exports.readyCheck = async (key) => {
|
||||||
const url = `https://api.torn.com/user/?selections=basic&key=${key}`
|
const url = `https://api.torn.com/user/?selections=basic&key=${key}`
|
||||||
@@ -24,16 +31,58 @@ module.exports.api = async (url) => {
|
|||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
return(data);
|
return(data);
|
||||||
};
|
};
|
||||||
|
module.exports.cache = {
|
||||||
|
async user(user) {
|
||||||
|
const twelveHours = 12 * 60 * 60 * 1000;
|
||||||
|
const now = new Date().getTime();
|
||||||
|
let last
|
||||||
|
try {
|
||||||
|
last = new Date(cache.users[user].updated).getTime();
|
||||||
|
} catch {
|
||||||
|
last = new Date(now - twelveHours).getTime();
|
||||||
|
}
|
||||||
|
if (cache.users[user] && (now - last < twelveHours)) {
|
||||||
|
console.debug(`Cache: Hit for ${cache.users[user].name}`)
|
||||||
|
return(cache.users[user]);
|
||||||
|
} else {
|
||||||
|
console.debug(`Cache: Miss for ${user}`)
|
||||||
|
await module.exports.user.basic(user);
|
||||||
|
console.debug(`Cache: Resolved to ${cache.users[user].name}`)
|
||||||
|
return(cache.users[user]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//async faction(faction) {},
|
||||||
|
//async company(company) {},
|
||||||
|
//async item(item) {}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports.user = {
|
module.exports.user = {
|
||||||
async basic(user) {
|
async basic(user) {
|
||||||
const response = await fetch(`https://api.torn.com/user/${user}?selections=basic&key=${config.torn}`);
|
const response = await fetch(`https://api.torn.com/user/${user}?selections=basic&key=${config.torn}`);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
const now = new Date();
|
||||||
|
cache.users[user] = {
|
||||||
|
name: data.name,
|
||||||
|
player_id: data.player_id,
|
||||||
|
level: data.level,
|
||||||
|
gender: data.gender,
|
||||||
|
updated: now.toISOString()
|
||||||
|
};
|
||||||
|
fs.writeFileSync('./cache.json', JSON.stringify(cache));
|
||||||
return(data);
|
return(data);
|
||||||
},
|
},
|
||||||
async profile(user) {
|
async profile(user) {
|
||||||
const response = await fetch(`https://api.torn.com/user/${user}?selections=profile&key=${config.torn}`);
|
const response = await fetch(`https://api.torn.com/user/${user}?selections=profile&key=${config.torn}`);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
const now = new Date();
|
||||||
|
cache.users[user] = {
|
||||||
|
name: data.name,
|
||||||
|
player_id: data.player_id,
|
||||||
|
level: data.level,
|
||||||
|
gender: data.gender,
|
||||||
|
updated: now.toISOString()
|
||||||
|
};
|
||||||
|
fs.writeFileSync('./cache.json', JSON.stringify(cache));
|
||||||
return(data);
|
return(data);
|
||||||
},
|
},
|
||||||
async stats(user, category, statName) {
|
async stats(user, category, statName) {
|
||||||
|
|||||||
Reference in New Issue
Block a user