cloudservices/src/api/routes/Webhook.ts

142 lines
7.2 KiB
TypeScript

/* eslint-disable no-continue */
import { TextChannel } from 'eris';
import { Server } from '..';
import { Route, RichEmbed } from '../../class';
export default class Webhook extends Route {
constructor(server: Server) {
super(server, { path: '/wh', deprecated: false, maintenance: false });
}
public bind() {
this.router.post('/s1', async (req, res) => {
try {
if (req.headers.authorization !== this.server.security.keys.iv.toString('base64')) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.codes.UNAUTHORIZED });
const embed = new RichEmbed();
embed.setTitle('Service Request');
embed.setDescription(`https://staff.libraryofcode.org/browse/${req.body.key}\n${req.body.url}`);
embed.setColor('#FF00FF');
embed.addField('Ticket Number', req.body.key, true);
embed.addField('Reporter', req.body.reporter, true);
embed.addField('Status', req.body.status, true);
embed.addField('Summary', req.body.summary);
embed.setFooter(this.server.client.user.username, this.server.client.user.avatarURL);
embed.setTimestamp();
const chan = <TextChannel> this.server.client.getChannel('580950455581147146');
chan.createMessage({ content: '<@&741797822940315650>', embed });
return res.status(200).json({ code: this.constants.codes.SUCCESS, message: this.constants.codes.SUCCESS });
} catch (err) {
return this.handleError(err, res);
}
});
this.router.get('/t2', async (req, res) => {
if (req.query?.auth !== this.server.security.keys.internal.toString('hex')) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED });
if (!req.query?.userID) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR });
const account = await this.server.client.db.Account.findOne({ userID: req.query.userID.toString() });
const tier = await this.server.client.db.Tier.findOne({ id: 2 });
if (account.tier >= 2) return res.sendStatus(200);
if (account.ramLimitNotification !== -1) {
await account.updateOne({ $set: { tier: 2, ramLimitNotification: tier.resourceLimits.ram - 20 } });
} else {
await account.updateOne({ $set: { tier: 2 } });
}
const embed = new RichEmbed();
embed.setTitle('Cloud Account | Tier Change');
embed.setColor('#0099ff');
embed.addField('User', `${account.username} | <@${account.userID}>`, true);
embed.addField('Technician', 'SYSTEM', true);
embed.addField('Old Tier -> New Tier', `${account.tier} -> 2`, true);
embed.setFooter(this.server.client.user.username, this.server.client.user.avatarURL);
embed.setTimestamp();
await this.server.client.util.sendMessageToUserTerminal(account.username, 'A technician has changed your tier to 2').catch(() => { });
this.server.client.createMessage('580950455581147146', { embed });
this.server.client.getDMChannel(account.userID).then((channel) => channel.createMessage({ embed })).catch();
return res.sendStatus(200);
});
this.router.get('/t2-rm', async (req, res) => {
if (req.query?.auth !== this.server.security.keys.internal.toString('hex')) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED });
if (!req.query?.userID) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR });
const account = await this.server.client.db.Account.findOne({ userID: req.query.userID.toString() });
const tier = await this.server.client.db.Tier.findOne({ id: 1 });
if (account.tier !== 2) return res.sendStatus(200);
if (account.ramLimitNotification !== -1) {
await account.updateOne({ $set: { tier: 1, ramLimitNotification: tier.resourceLimits.ram - 20 } });
} else {
await account.updateOne({ $set: { tier: 1 } });
}
const embed = new RichEmbed();
embed.setTitle('Cloud Account | Tier Change');
embed.setColor('#0099ff');
embed.addField('User', `${account.username} | <@${account.userID}>`, true);
embed.addField('Technician', 'SYSTEM', true);
embed.addField('Old Tier -> New Tier', `${account.tier} -> 1`, true);
embed.setFooter(this.server.client.user.username, this.server.client.user.avatarURL);
embed.setTimestamp();
await this.server.client.util.sendMessageToUserTerminal(account.username, 'A technician has changed your tier.').catch(() => { });
this.server.client.createMessage('580950455581147146', { embed });
this.server.client.getDMChannel(account.userID).then((channel) => channel.createMessage({ embed })).catch();
return res.sendStatus(200);
});
this.router.get('/info', async (req, res) => {
if (req.query?.authorization !== this.server.security.keys.internal.toString('hex')) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED });
if (!req.query?.id) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR });
const account = await this.server.client.db.Account.findOne({ userID: req.query.id.toString() }).lean().exec();
if (!account) return res.status(200).send({ found: false });
return res.status(200).send({
found: true,
emailAddress: account.emailAddress,
tier: account.tier,
supportKey: account.supportKey,
});
});
this.router.get('/score', async (req, res) => {
try {
if (req.query?.authorization !== this.server.security.keys.internal.toString('hex')) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED });
if (!req.query?.id) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR });
const account = await this.server.client.db.Account.findOne({ userID: req.query.id.toString() }).lean().exec();
const moderations = await this.server.client.db.Moderation.find({ userID: req.query.id.toString() }).lean().exec();
if (!account && (!moderations || moderations.length <= 0)) return res.status(200).send({ found: false });
const response: {
found: boolean,
tier: number,
totalReferrals?: number,
createdAt?: Date,
warns?: Date[],
locks?: Date[],
deletes?: Date[],
} = {
found: true,
tier: account?.tier || 0,
totalReferrals: 0,
createdAt: account?.createdAt || null,
warns: [],
locks: [],
deletes: [],
};
if (account?.totalReferrals && account?.totalReferrals > 0) response.totalReferrals = account.totalReferrals;
if (moderations.length > 0) {
for (const moderation of moderations) {
if (moderation.reason?.includes('DN/C')) continue;
if (moderation.type === 1) response.warns.push(moderation.date);
if (moderation.type === 2) response.locks.push(moderation.date);
if (moderation.type === 4) response.deletes.push(moderation.date);
}
}
return res.status(200).json(response);
} catch (err) {
return this.handleError(err, res);
}
});
}
}