|
| 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 | +} |
0 commit comments