1
0
Fork 0

various fixes to account model and intervals

refactor/models
Matthew 2020-03-28 12:10:58 -04:00
parent 23afdcdf90
commit febb9ee924
No known key found for this signature in database
GPG Key ID: 766BE43AE75F7559
3 changed files with 61 additions and 59 deletions

View File

@ -16,10 +16,10 @@ export default class SecureSign_Account extends Command {
public async run(message: Message, args: string[]) { public async run(message: Message, args: string[]) {
try { try {
const user = await this.client.db.Account.findOne({ userID: message.author.id }); const user = await this.client.db.Account.findOne({ userID: message.author.id });
if (!user || (!user.permissions.associate && !(message.channel instanceof PrivateChannel))) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Run this command in your DMs!***`); if (!user || (!user.permissions.staff && !(message.channel instanceof PrivateChannel))) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Run this command in your DMs!***`);
let account: AccountInterface; let account: AccountInterface;
if (!args[0] || !user.permissions.associate) account = user; if (!args[0] || !user.permissions.staff) account = user;
else account = await this.client.db.Account.findOne({ $or: [{ userID: args[0] }, { username: args[0] }, { emailAddress: args[0] }] }); else account = await this.client.db.Account.findOne({ $or: [{ userID: args[0] }, { username: args[0] }, { emailAddress: args[0] }] });
if (!account) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Account not found***`); if (!account) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Account not found***`);

View File

@ -13,61 +13,63 @@ export const memoryLimits = {
TIER_3_HARD: 550, TIER_3_HARD: 550,
}; };
export default async function memory(client: Client) { export default function memory(client: Client) {
const accounts = await client.db.Account.find(); setInterval(async () => {
for (const acc of accounts) { const accounts = await client.db.Account.find();
if (acc.root) return; for (const acc of accounts) {
// memory in bytes if (acc.root) return;
const mem = Number(await client.util.exec(`memory ${acc.username}`)) * 1000; // memory in bytes
// memory in megabytes const mem = Number(await client.util.exec(`memory ${acc.username}`)) * 1000;
const memoryConversion = mem / 1024 / 1024; // memory in megabytes
let userLimits: { soft: number, hard: number }; const memoryConversion = mem / 1024 / 1024;
if (acc.tier === 1) { let userLimits: { soft: number, hard: number };
userLimits = { soft: memoryLimits.TIER_1_SOFT, hard: memoryLimits.TIER_1_HARD }; if (acc.tier === 1) {
} else if (acc.tier === 2) { userLimits = { soft: memoryLimits.TIER_1_SOFT, hard: memoryLimits.TIER_1_HARD };
userLimits = { soft: memoryLimits.TIER_2_SOFT, hard: memoryLimits.TIER_2_HARD }; } else if (acc.tier === 2) {
} else if (acc.tier === 3) { userLimits = { soft: memoryLimits.TIER_2_SOFT, hard: memoryLimits.TIER_2_HARD };
userLimits = { soft: memoryLimits.TIER_3_SOFT, hard: memoryLimits.TIER_3_HARD }; } else if (acc.tier === 3) {
} userLimits = { soft: memoryLimits.TIER_3_SOFT, hard: memoryLimits.TIER_3_HARD };
}
/* if the user has exceeded their soft memory limit, which is the one described in the
resource limit guidelines, we'll inform staff. /* if the user has exceeded their soft memory limit, which is the one described in the
*/ resource limit guidelines, we'll inform staff.
if (acc.tier === 1 && memoryConversion >= userLimits.soft) { */
const embed = new RichEmbed(); if (acc.tier === 1 && memoryConversion >= userLimits.soft) {
if (client.users.get(acc.userID)) embed.setThumbnail(client.users.get(acc.userID).avatarURL); const embed = new RichEmbed();
embed.addField('User', `${acc.username} | <@${acc.userID}> | ${acc.userID}`, true); if (client.users.get(acc.userID)) embed.setThumbnail(client.users.get(acc.userID).avatarURL);
embed.addField('Tier', String(acc.tier), true); embed.addField('User', `${acc.username} | <@${acc.userID}> | ${acc.userID}`, true);
embed.addField('Memory Usage', `${String(memoryConversion)} MB`, true); embed.addField('Tier', String(acc.tier), true);
embed.addField('Memory Limit', `${String(userLimits.soft)} MB`, true); embed.addField('Memory Usage', `${String(memoryConversion)} MB`, true);
embed.setFooter(client.user.username, client.user.avatarURL); embed.addField('Memory Limit', `${String(userLimits.soft)} MB`, true);
embed.setTimestamp(); embed.setFooter(client.user.username, client.user.avatarURL);
embed.setTimestamp();
// if they exceed the hard limit, we'll kill all of their processes.
if (memoryConversion >= userLimits.hard) { // if they exceed the hard limit, we'll kill all of their processes.
client.util.exec(`killall -9 -u ${acc.username}`); if (memoryConversion >= userLimits.hard) {
embed.setTitle('Resource Enforcement Notification'); client.util.exec(`killall -9 -u ${acc.username}`);
embed.setDescription('Someone has reached the (hard) resource limit for their tier on RAM. The system has automatically killed all of their processes.'); embed.setTitle('Resource Enforcement Notification');
client.util.createModerationLog(acc.userID, client.guilds.get('446067825673633794').members.get(client.user.id), 1, '[AUTO] Exceeded resource limit for RAM.'); embed.setDescription('Someone has reached the (hard) resource limit for their tier on RAM. The system has automatically killed all of their processes.');
client.util.transport.sendMail({ client.util.createModerationLog(acc.userID, client.guilds.get('446067825673633794').members.get(client.user.id), 1, '[AUTO] Exceeded resource limit for RAM.');
to: acc.emailAddress, client.util.transport.sendMail({
from: 'Library of Code sp-us | Cloud Services <help@libraryofcode.org>', to: acc.emailAddress,
subject: 'Your account has been warned', from: 'Library of Code sp-us | Cloud Services <help@libraryofcode.org>',
html: ` subject: 'Your account has been warned',
<h1>Library of Code sp-us | Cloud Services</h1> html: `
<p>Your account has received an official warning from a Moderator. Please get the underlying issue resolved to avoid <i>possible</i> moderative action.</p> <h1>Library of Code sp-us | Cloud Services</h1>
<p><strong>Reason:</strong> [AUTO] Exceeded resource limit for RAM.</p> <p>Your account has received an official warning from a Moderator. Please get the underlying issue resolved to avoid <i>possible</i> moderative action.</p>
<p><strong>Moderator:</strong> ${client.user.username}</p> <p><strong>Reason:</strong> [AUTO] Exceeded resource limit for RAM.</p>
<p><strong>Moderator:</strong> ${client.user.username}</p>
<b><i>Library of Code sp-us | Support Team</i></b>
`, <b><i>Library of Code sp-us | Support Team</i></b>
}); `,
} else { });
embed.setTitle('Resource Limit Notification'); } else {
embed.setDescription('Someone has reached the (soft) resource limit for their tier on RAM.'); embed.setTitle('Resource Limit Notification');
embed.setDescription('Someone has reached the (soft) resource limit for their tier on RAM.');
}
// @ts-ignore
client.createMessage(channelID, { embed });
} }
// @ts-ignore
client.createMessage(channelID, { embed });
} }
} }, 300000);
} }

View File

@ -10,7 +10,7 @@ export interface AccountInterface extends Document {
locked: boolean, locked: boolean,
tier: number; tier: number;
permissions: { permissions: {
associate: boolean, staff: boolean,
sheriff: boolean, sheriff: boolean,
facultyMarshal: boolean, facultyMarshal: boolean,
}, },
@ -30,7 +30,7 @@ const Account: Schema = new Schema({
locked: Boolean, locked: Boolean,
tier: Number, tier: Number,
permissions: { permissions: {
associate: Boolean, staff: Boolean,
sheriff: Boolean, sheriff: Boolean,
facultyMarshal: Boolean, facultyMarshal: Boolean,
}, },