Skip to content

Commit afbf980

Browse files
Added automated BGS Report
1 parent cf6c5ca commit afbf980

File tree

9 files changed

+545
-189
lines changed

9 files changed

+545
-189
lines changed

package-lock.json

+6-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"dependencies": {
1010
"body-parser": "^1.18.2",
1111
"cookie-parser": "^1.4.3",
12+
"cron": "^1.3.0",
1213
"debug": "^3.1.0",
1314
"discord.js": "^11.2.1",
1415
"express": "^4.16.2",
@@ -20,6 +21,7 @@
2021
"devDependencies": {
2122
"@types/body-parser": "^1.16.8",
2223
"@types/cookie-parser": "^1.4.1",
24+
"@types/cron": "^1.2.1",
2325
"@types/debug": "0.0.30",
2426
"@types/express": "^4.0.39",
2527
"@types/mongoose": "^4.7.27",

src/db/interfaces/guild.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface IGuild {
1818
guild_id: string,
1919
bgs_channel_id: string,
2020
bgs_role_id: string,
21+
bgs_time: string,
2122
admin_roles_id: string[],
2223
forbidden_roles_id: string[],
2324
created_at: Date,

src/db/schemas/guild.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export let guildSchema: Schema = new Schema({
2323
},
2424
bgs_channel_id: String,
2525
bgs_role_id: String,
26+
bgs_time: String,
2627
admin_roles_id: [String],
2728
forbidden_roles_id: [String],
2829
created_at: {

src/interfaces/typings.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as mongoosePaginate from 'mongoose-paginate';
22
import { PaginateResult } from 'mongoose';
3+
import { CronJob } from 'cron';
34

45
interface FactionSchema {
56
_id: string;
@@ -197,10 +198,17 @@ export interface EBGSSystemV3SchemaWOHistory {
197198
updated_at: string;
198199
}
199200

201+
export interface CronJobStoreSchema {
202+
cronJob: CronJob;
203+
guildid: String;
204+
time: String;
205+
}
206+
200207

201208
export type FactionsV3 = PaginateResult<FactionSchema>;
202209
export type PopulatedSystemsV3 = PaginateResult<PopulatedSystemSchema>;
203210
export type EBGSFactionsV3 = PaginateResult<EBGSFactionV3Schema>;
204211
export type EBGSSystemsV3 = PaginateResult<EBGSSystemV3Schema>;
205212
export type EBGSFactionsV3WOHistory = PaginateResult<EBGSFactionV3SchemaWOHistory>;
206213
export type EBGSSystemsV3WOHistory = PaginateResult<EBGSSystemV3SchemaWOHistory>;
214+
export type CronJobStore = CronJobStoreSchema;

src/modules/cron/autoReport.ts

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* KodeBlox Copyright 2017 Sayak Mukhopadhyay
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http: //www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { CronJob } from 'cron';
18+
import { IGuildModel } from '../../db/models/index';
19+
import { Client, GuildChannel, TextChannel } from 'discord.js';
20+
import { CronJobStore } from '../../interfaces/typings';
21+
import { BGSReport } from '../discord/commands/bgsReport';
22+
23+
export class AutoReport {
24+
private static jobs: CronJobStore[] = [];
25+
26+
public static initiateJob(guilds: IGuildModel[], client: Client) {
27+
guilds.forEach(guild => {
28+
if (guild.bgs_time && guild.bgs_time.length > 0 && guild.bgs_channel_id && guild.bgs_channel_id.length > 0) {
29+
try {
30+
let cronPattern = `${guild.bgs_time.split(':')[2]} ${guild.bgs_time.split(':')[1]} ${guild.bgs_time.split(':')[0]} * * *`;
31+
let cronJob = new CronJob(cronPattern, () => {
32+
let bgsChannel: GuildChannel = client.guilds.get(guild.guild_id).channels.get(guild.bgs_channel_id);
33+
if (bgsChannel && bgsChannel.type === 'text') {
34+
let bgsReport = new BGSReport();
35+
bgsReport.getBGSReportEmbed(guild.guild_id)
36+
.then(embed => {
37+
(bgsChannel as TextChannel).send(embed);
38+
});
39+
} else {
40+
console.log(`Guild ${guild.guild_id} has not been set up`)
41+
}
42+
});
43+
this.jobs.push({
44+
cronJob: cronJob,
45+
guildid: guild.guild_id,
46+
time: guild.bgs_time
47+
});
48+
cronJob.start();
49+
}
50+
catch (err) {
51+
console.log(err);
52+
console.log(`Time ${guild.bgs_time} is not a suitable cron time`);
53+
}
54+
}
55+
});
56+
}
57+
58+
public static createJob(guild: IGuildModel, client: Client) {
59+
if (guild.bgs_time && guild.bgs_time.length > 0 && guild.bgs_channel_id && guild.bgs_channel_id.length > 0) {
60+
if (this.jobs.findIndex(element => {
61+
return element.guildid === guild.guild_id;
62+
}) !== -1) {
63+
this.editJob(guild, client);
64+
} else {
65+
try {
66+
let cronPattern = `${guild.bgs_time.split(':')[2]} ${guild.bgs_time.split(':')[1]} ${guild.bgs_time.split(':')[0]} * * *`;
67+
let cronJob = new CronJob(cronPattern, () => {
68+
let bgsChannel: GuildChannel = client.guilds.get(guild.guild_id).channels.get(guild.bgs_channel_id);
69+
if (bgsChannel && bgsChannel.type === 'text') {
70+
let bgsReport = new BGSReport();
71+
bgsReport.getBGSReportEmbed(guild.guild_id)
72+
.then(embed => {
73+
(bgsChannel as TextChannel).send(embed);
74+
});
75+
} else {
76+
console.log(`Guild ${guild.guild_id} has not been set up`)
77+
}
78+
});
79+
this.jobs.push({
80+
cronJob: cronJob,
81+
guildid: guild.guild_id,
82+
time: guild.bgs_time
83+
});
84+
cronJob.start();
85+
}
86+
catch (err) {
87+
console.log(err);
88+
console.log(`Time ${guild.bgs_time} is not a suitable cron time`);
89+
}
90+
}
91+
}
92+
}
93+
94+
public static editJob(guild: IGuildModel, client: Client) {
95+
if (guild.bgs_time && guild.bgs_time.length > 0 && guild.bgs_channel_id && guild.bgs_channel_id.length > 0) {
96+
let indexOfJob = this.jobs.findIndex(element => {
97+
return element.guildid === guild.guild_id;
98+
});
99+
if (indexOfJob === -1) {
100+
this.createJob(guild, client);
101+
} else {
102+
let existingCronJob = this.jobs[indexOfJob].cronJob;
103+
existingCronJob.stop();
104+
try {
105+
let cronPattern = `${guild.bgs_time.split(':')[2]} ${guild.bgs_time.split(':')[1]} ${guild.bgs_time.split(':')[0]} * * *`;
106+
let cronJob = new CronJob(cronPattern, () => {
107+
let bgsChannel: GuildChannel = client.guilds.get(guild.guild_id).channels.get(guild.bgs_channel_id);
108+
if (bgsChannel && bgsChannel.type === 'text') {
109+
let bgsReport = new BGSReport();
110+
bgsReport.getBGSReportEmbed(guild.guild_id)
111+
.then(embed => {
112+
(bgsChannel as TextChannel).send(embed);
113+
});
114+
} else {
115+
console.log(`Guild ${guild.guild_id} has not been set up`)
116+
}
117+
});
118+
this.jobs[indexOfJob].cronJob = cronJob;
119+
this.jobs[indexOfJob].time = guild.bgs_time;
120+
cronJob.start();
121+
}
122+
catch (err) {
123+
console.log(err);
124+
console.log(`Time ${guild.bgs_time} is not a suitable cron time`);
125+
}
126+
}
127+
}
128+
}
129+
130+
public static deleteJob(guild: IGuildModel, client: Client) {
131+
let indexOfJob = this.jobs.findIndex(element => {
132+
return element.guildid === guild.guild_id;
133+
});
134+
if (indexOfJob !== -1) {
135+
let existingCronJob = this.jobs[indexOfJob].cronJob;
136+
existingCronJob.stop();
137+
this.jobs.splice(indexOfJob);
138+
}
139+
}
140+
}

src/modules/cron/index.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* KodeBlox Copyright 2017 Sayak Mukhopadhyay
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http: //www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export * from './autoReport';

0 commit comments

Comments
 (0)