-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from SayakMukhopadhyay/wip
Closes #10
- Loading branch information
Showing
15 changed files
with
1,605 additions
and
832 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,42 @@ | ||
{ | ||
"name": "bgsbot", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"license": "Apache-2.0", | ||
"scripts": { | ||
"start": "gulp scripts && cross-env PORT=4002 pm2 start process.json --env production", | ||
"startdev": "cross-env NODE_ENV=development gulp" | ||
}, | ||
"dependencies": { | ||
"ajv": "^5.2.0", | ||
"body-parser": "^1.17.2", | ||
"body-parser": "^1.18.2", | ||
"cookie-parser": "^1.4.3", | ||
"debug": "^2.6.8", | ||
"discord.js": "^11.1.0", | ||
"express": "^4.15.3", | ||
"moment": "^2.18.1", | ||
"mongoose": "^4.11.1", | ||
"mongoose-paginate": "^5.0.3", | ||
"morgan": "^1.8.2", | ||
"request": "^2.81.0", | ||
"serve-favicon": "^2.4.3", | ||
"zmq": "^2.15.3" | ||
"cron": "^1.3.0", | ||
"debug": "^3.1.0", | ||
"discord.js": "^11.2.1", | ||
"express": "^4.16.2", | ||
"moment": "^2.19.2", | ||
"mongoose": "^4.13.4", | ||
"morgan": "^1.9.0", | ||
"request": "^2.83.0" | ||
}, | ||
"devDependencies": { | ||
"@types/body-parser": "^1.16.4", | ||
"@types/cookie-parser": "^1.3.30", | ||
"@types/debug": "0.0.29", | ||
"@types/express": "^4.0.36", | ||
"@types/mongoose": "^4.7.18", | ||
"@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", | ||
"@types/mongoose-paginate": "^5.0.6", | ||
"@types/morgan": "^1.7.32", | ||
"@types/node": "^8.0.8", | ||
"@types/request": "0.0.45", | ||
"@types/serve-favicon": "^2.2.28", | ||
"@types/zmq": "0.0.20", | ||
"cross-env": "^5.0.1", | ||
"@types/morgan": "^1.7.35", | ||
"@types/node": "^8.0.53", | ||
"@types/request": "^2.0.8", | ||
"@types/serve-favicon": "^2.2.30", | ||
"@types/zmq": "0.0.22", | ||
"cross-env": "^5.1.1", | ||
"gulp": "^3.9.1", | ||
"gulp-nodemon": "^2.2.1", | ||
"gulp-sourcemaps": "^2.6.0", | ||
"gulp-typescript": "^3.1.7", | ||
"pm2": "^2.5.0", | ||
"typescript": "^2.4.1" | ||
"gulp-sourcemaps": "^2.6.1", | ||
"gulp-typescript": "^3.2.3", | ||
"pm2": "^2.7.2", | ||
"typescript": "^2.6.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.