Skip to content

Commit

Permalink
Added automated BGS Report
Browse files Browse the repository at this point in the history
  • Loading branch information
SayakMukhopadhyay committed Nov 22, 2017
1 parent cf6c5ca commit afbf980
Show file tree
Hide file tree
Showing 9 changed files with 545 additions and 189 deletions.
8 changes: 6 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"dependencies": {
"body-parser": "^1.18.2",
"cookie-parser": "^1.4.3",
"cron": "^1.3.0",
"debug": "^3.1.0",
"discord.js": "^11.2.1",
"express": "^4.16.2",
Expand All @@ -20,6 +21,7 @@
"devDependencies": {
"@types/body-parser": "^1.16.8",
"@types/cookie-parser": "^1.4.1",
"@types/cron": "^1.2.1",
"@types/debug": "0.0.30",
"@types/express": "^4.0.39",
"@types/mongoose": "^4.7.27",
Expand Down
1 change: 1 addition & 0 deletions src/db/interfaces/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface IGuild {
guild_id: string,
bgs_channel_id: string,
bgs_role_id: string,
bgs_time: string,
admin_roles_id: string[],
forbidden_roles_id: string[],
created_at: Date,
Expand Down
1 change: 1 addition & 0 deletions src/db/schemas/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export let guildSchema: Schema = new Schema({
},
bgs_channel_id: String,
bgs_role_id: String,
bgs_time: String,
admin_roles_id: [String],
forbidden_roles_id: [String],
created_at: {
Expand Down
8 changes: 8 additions & 0 deletions src/interfaces/typings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as mongoosePaginate from 'mongoose-paginate';
import { PaginateResult } from 'mongoose';
import { CronJob } from 'cron';

interface FactionSchema {
_id: string;
Expand Down Expand Up @@ -197,10 +198,17 @@ export interface EBGSSystemV3SchemaWOHistory {
updated_at: string;
}

export interface CronJobStoreSchema {
cronJob: CronJob;
guildid: String;
time: String;
}


export type FactionsV3 = PaginateResult<FactionSchema>;
export type PopulatedSystemsV3 = PaginateResult<PopulatedSystemSchema>;
export type EBGSFactionsV3 = PaginateResult<EBGSFactionV3Schema>;
export type EBGSSystemsV3 = PaginateResult<EBGSSystemV3Schema>;
export type EBGSFactionsV3WOHistory = PaginateResult<EBGSFactionV3SchemaWOHistory>;
export type EBGSSystemsV3WOHistory = PaginateResult<EBGSSystemV3SchemaWOHistory>;
export type CronJobStore = CronJobStoreSchema;
140 changes: 140 additions & 0 deletions src/modules/cron/autoReport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* KodeBlox Copyright 2017 Sayak Mukhopadhyay
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http: //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { CronJob } from 'cron';
import { IGuildModel } from '../../db/models/index';
import { Client, GuildChannel, TextChannel } from 'discord.js';
import { CronJobStore } from '../../interfaces/typings';
import { BGSReport } from '../discord/commands/bgsReport';

export class AutoReport {
private static jobs: CronJobStore[] = [];

public static initiateJob(guilds: IGuildModel[], client: Client) {
guilds.forEach(guild => {
if (guild.bgs_time && guild.bgs_time.length > 0 && guild.bgs_channel_id && guild.bgs_channel_id.length > 0) {
try {
let cronPattern = `${guild.bgs_time.split(':')[2]} ${guild.bgs_time.split(':')[1]} ${guild.bgs_time.split(':')[0]} * * *`;
let cronJob = new CronJob(cronPattern, () => {
let bgsChannel: GuildChannel = client.guilds.get(guild.guild_id).channels.get(guild.bgs_channel_id);
if (bgsChannel && bgsChannel.type === 'text') {
let bgsReport = new BGSReport();
bgsReport.getBGSReportEmbed(guild.guild_id)
.then(embed => {
(bgsChannel as TextChannel).send(embed);
});
} else {
console.log(`Guild ${guild.guild_id} has not been set up`)
}
});
this.jobs.push({
cronJob: cronJob,
guildid: guild.guild_id,
time: guild.bgs_time
});
cronJob.start();
}
catch (err) {
console.log(err);
console.log(`Time ${guild.bgs_time} is not a suitable cron time`);
}
}
});
}

public static createJob(guild: IGuildModel, client: Client) {
if (guild.bgs_time && guild.bgs_time.length > 0 && guild.bgs_channel_id && guild.bgs_channel_id.length > 0) {
if (this.jobs.findIndex(element => {
return element.guildid === guild.guild_id;
}) !== -1) {
this.editJob(guild, client);
} else {
try {
let cronPattern = `${guild.bgs_time.split(':')[2]} ${guild.bgs_time.split(':')[1]} ${guild.bgs_time.split(':')[0]} * * *`;
let cronJob = new CronJob(cronPattern, () => {
let bgsChannel: GuildChannel = client.guilds.get(guild.guild_id).channels.get(guild.bgs_channel_id);
if (bgsChannel && bgsChannel.type === 'text') {
let bgsReport = new BGSReport();
bgsReport.getBGSReportEmbed(guild.guild_id)
.then(embed => {
(bgsChannel as TextChannel).send(embed);
});
} else {
console.log(`Guild ${guild.guild_id} has not been set up`)
}
});
this.jobs.push({
cronJob: cronJob,
guildid: guild.guild_id,
time: guild.bgs_time
});
cronJob.start();
}
catch (err) {
console.log(err);
console.log(`Time ${guild.bgs_time} is not a suitable cron time`);
}
}
}
}

public static editJob(guild: IGuildModel, client: Client) {
if (guild.bgs_time && guild.bgs_time.length > 0 && guild.bgs_channel_id && guild.bgs_channel_id.length > 0) {
let indexOfJob = this.jobs.findIndex(element => {
return element.guildid === guild.guild_id;
});
if (indexOfJob === -1) {
this.createJob(guild, client);
} else {
let existingCronJob = this.jobs[indexOfJob].cronJob;
existingCronJob.stop();
try {
let cronPattern = `${guild.bgs_time.split(':')[2]} ${guild.bgs_time.split(':')[1]} ${guild.bgs_time.split(':')[0]} * * *`;
let cronJob = new CronJob(cronPattern, () => {
let bgsChannel: GuildChannel = client.guilds.get(guild.guild_id).channels.get(guild.bgs_channel_id);
if (bgsChannel && bgsChannel.type === 'text') {
let bgsReport = new BGSReport();
bgsReport.getBGSReportEmbed(guild.guild_id)
.then(embed => {
(bgsChannel as TextChannel).send(embed);
});
} else {
console.log(`Guild ${guild.guild_id} has not been set up`)
}
});
this.jobs[indexOfJob].cronJob = cronJob;
this.jobs[indexOfJob].time = guild.bgs_time;
cronJob.start();
}
catch (err) {
console.log(err);
console.log(`Time ${guild.bgs_time} is not a suitable cron time`);
}
}
}
}

public static deleteJob(guild: IGuildModel, client: Client) {
let indexOfJob = this.jobs.findIndex(element => {
return element.guildid === guild.guild_id;
});
if (indexOfJob !== -1) {
let existingCronJob = this.jobs[indexOfJob].cronJob;
existingCronJob.stop();
this.jobs.splice(indexOfJob);
}
}
}
17 changes: 17 additions & 0 deletions src/modules/cron/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* KodeBlox Copyright 2017 Sayak Mukhopadhyay
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http: //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export * from './autoReport';
Loading

0 comments on commit afbf980

Please sign in to comment.