score remodel with frontend

pull/29/head
Matthew 2020-09-20 22:33:56 -04:00
parent b5e8230940
commit 375ea3a963
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
4 changed files with 106 additions and 1 deletions

View File

@ -2,8 +2,11 @@ import { Route, Server } from '../../../class';
// import acknowledgements from '../../../configs/acknowledgements.json'; // import acknowledgements from '../../../configs/acknowledgements.json';
export default class Internal extends Route { export default class Internal extends Route {
public timeout: Set<string>;
constructor(server: Server) { constructor(server: Server) {
super(server); super(server);
this.timeout = new Set();
this.conf = { this.conf = {
path: '/int', path: '/int',
}; };
@ -28,5 +31,91 @@ export default class Internal extends Route {
return this.handleError(err, res); return this.handleError(err, res);
} }
}); });
this.router.get('/score', async (req, res) => {
try {
res.setHeader('Access-Control-Allow-Origin', '*');
if (this.timeout.has(req.ip)) return res.status(429).json({ code: this.constants.codes.PERMISSION_DENIED, message: this.constants.messages.PERMISSION_DENIED });
if (!req.query.pin) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR });
const args = req.query.pin.toString();
this.timeout.add(req.ip);
setTimeout(() => this.timeout.delete(req.ip), 1800000);
let score = await this.server.client.db.Score.findOne({ pin: [Number(args.split('-')[0]), Number(args.split('-')[1]), Number(args.split('-')[2])] }).lean().exec();
if (!score) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED });
const member = await this.server.client.getRESTGuildMember(this.constants.discord.SERVER_ID, score.userID);
if (!member) return res.status(404).json({ code: this.constants.codes.ACCOUNT_NOT_FOUND });
await this.server.client.db.Score.updateOne({ userID: score.userID }, { $addToSet: { softInquiries: { name: `${member.username} via report.libraryofcode.org @ IP ${req.ip}`, date: new Date() } } });
score = await this.server.client.db.Score.findOne({ pin: [Number(args.split('-')[0]), Number(args.split('-')[1]), Number(args.split('-')[2])] }).lean().exec();
let totalScore = '0';
let activityScore = '0';
let moderationScore = '0';
let roleScore = '0';
let cloudServicesScore = '0';
let otherScore = '0';
let miscScore = '0';
if (score.total < 200) totalScore = '---';
else if (score.total > 800) totalScore = '800';
else totalScore = `${score.total}`;
if (score.activity < 10) activityScore = '---';
else if (score.activity > Math.floor((Math.log1p(5000 + 300 + 200 + 100) * 12))) activityScore = String(Math.floor((Math.log1p(5000 + 300 + 200 + 100) * 12)));
else activityScore = `${score.activity}`;
if (score.roles <= 0) roleScore = '---';
else if (score.roles > 54) roleScore = '54';
else roleScore = `${score.roles}`;
moderationScore = `${score.moderation}`;
if (score.other === 0) otherScore = '---';
else otherScore = `${score.other}`;
if (score.staff <= 0) miscScore = '---';
else miscScore = `${score.staff}`;
if (score.cloudServices === 0) cloudServicesScore = '---';
else if (score.cloudServices > 10) cloudServicesScore = '10';
else cloudServicesScore = `${score.cloudServices}`;
const moderations = await this.server.client.db.Moderation.find({ userID: score.userID });
return res.status(200).json({
name: `${member.username}#${member.discriminator}`,
userID: score.userID,
pin: score.pin?.join('-'),
score: totalScore,
activityScore,
cloudServicesScore,
moderationScore,
roleScore,
otherScore,
miscScore,
notify: score.notify,
locked: !!score.locked,
totalModerations: moderations?.length > 0 ? moderations.length : 0,
inquiries: score.inquiries?.length > 0 ? score.inquiries : [],
softInquiries: score.softInquiries?.length > 0 ? score.softInquiries : [],
lastUpdated: score.lastUpdate,
});
} catch (err) {
return this.handleError(err, res);
}
});
this.router.get('/id', async (req, res) => {
try {
if (req.query?.auth.toString() !== this.server.client.config.internalKey) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED });
if (!req.query.pin) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR });
const args = req.query.pin.toString();
const user = await this.server.client.db.Score.findOne({ pin: [Number(args.split('-')[0]), Number(args.split('-')[1]), Number(args.split('-')[2])] }).lean().exec();
if (!user) return res.status(404).json({ code: this.constants.codes.ACCOUNT_NOT_FOUND, message: this.constants.messages.NOT_FOUND });
return res.status(200).json({ userID: user.userID });
} catch (err) {
return this.handleError(err, res);
}
});
} }
} }

View File

@ -15,6 +15,7 @@ export default class Score extends Command {
public async run(message: Message, args: string[]) { public async run(message: Message, args: string[]) {
try { try {
let check = false;
let user: User; let user: User;
if (args[0] === 'notify') { if (args[0] === 'notify') {
user = message.author; user = message.author;
@ -51,6 +52,8 @@ export default class Score extends Command {
if (!args[0] || !this.checkCustomPermissions(this.client.util.resolveMember(message.author.id, this.mainGuild), 6)) { if (!args[0] || !this.checkCustomPermissions(this.client.util.resolveMember(message.author.id, this.mainGuild), 6)) {
user = message.author; user = message.author;
if (!user) return this.error(message.channel, 'Member not found.'); if (!user) return this.error(message.channel, 'Member not found.');
await this.client.db.Score.updateOne({ userID: user.id }, { $addToSet: { softInquiries: { name: `${user.username} via Discord`, date: new Date() } } });
check = true;
} else { } else {
user = this.client.util.resolveMember(args[0], this.mainGuild).user; user = this.client.util.resolveMember(args[0], this.mainGuild).user;
if (!user) { if (!user) {
@ -157,6 +160,15 @@ export default class Score extends Command {
// eslint-disable-next-line no-mixed-operators // eslint-disable-next-line no-mixed-operators
if ((args[1] === 'hard' || args[1] === 'soft') && this.checkCustomPermissions(this.client.util.resolveMember(message.author.id, this.mainGuild), 6)) { if ((args[1] === 'hard' || args[1] === 'soft') && this.checkCustomPermissions(this.client.util.resolveMember(message.author.id, this.mainGuild), 6)) {
if (score.pin?.length > 0) embed.addField('PIN', score.pin.join('-'), true); if (score.pin?.length > 0) embed.addField('PIN', score.pin.join('-'), true);
if (args[1] === 'soft' && !check) {
let name = '';
for (const role of this.client.util.resolveMember(message.author.id, this.mainGuild).roles.map((r) => this.mainGuild.roles.get(r)).sort((a, b) => b.position - a.position)) {
name = `Library of Code sp-us | ${role.name}`;
break;
}
await this.client.db.Score.updateOne({ userID: user.id }, { $addToSet: { softInquiries: { name, date: new Date() } } });
}
} }
if (args[1] === 'hard' && this.checkCustomPermissions(this.client.util.resolveMember(message.author.id, this.mainGuild), 6)) { if (args[1] === 'hard' && this.checkCustomPermissions(this.client.util.resolveMember(message.author.id, this.mainGuild), 6)) {
await message.channel.createMessage({ embed }); await message.channel.createMessage({ embed });

View File

@ -40,7 +40,8 @@ export default async function calculateScore(client: Client): Promise<NodeJS.Tim
staff: boolean, staff: boolean,
locked: boolean, locked: boolean,
notify: boolean, notify: boolean,
inquiries: [{ name: string, reason: string}?], inquiries: [{ name: string, reason: string, date: Date }?],
softInquiries: [{name: string, date: Date }?]
lastUpdated: Date, lastUpdated: Date,
pin: number[], pin: number[],
/* generalMessagesRatio: number, /* generalMessagesRatio: number,
@ -59,6 +60,7 @@ export default async function calculateScore(client: Client): Promise<NodeJS.Tim
locked: false, locked: false,
notify: false, notify: false,
inquiries: [], inquiries: [],
softInquiries: [],
lastUpdated: new Date(), lastUpdated: new Date(),
pin: [client.util.randomNumber(100, 999), client.util.randomNumber(10, 99), client.util.randomNumber(1000, 9999)], pin: [client.util.randomNumber(100, 999), client.util.randomNumber(10, 99), client.util.randomNumber(1000, 9999)],
}; };

View File

@ -34,6 +34,7 @@ export interface ScoreInterface extends Document {
notify: boolean, notify: boolean,
locked: boolean, locked: boolean,
inquiries: [{ name: string, reason: string, date: Date }], inquiries: [{ name: string, reason: string, date: Date }],
softInquiries: [{ name: string, date: Date }],
lastUpdate: Date, lastUpdate: Date,
pin: number[], pin: number[],
@ -57,6 +58,7 @@ const Score: Schema = new Schema({
notify: Boolean, notify: Boolean,
locked: Boolean, locked: Boolean,
inquiries: Array, inquiries: Array,
softInquiries: Array,
lastUpdate: Date, lastUpdate: Date,
pin: Array, pin: Array,
/* generalMessagesRatio: Number, /* generalMessagesRatio: Number,