Skip to content

Commit

Permalink
Merge pull request #13 from SayakMukhopadhyay/wip
Browse files Browse the repository at this point in the history
Closes #10
  • Loading branch information
SayakMukhopadhyay authored Nov 22, 2017
2 parents d460863 + afbf980 commit 72f6fe2
Show file tree
Hide file tree
Showing 15 changed files with 1,605 additions and 832 deletions.
1,353 changes: 768 additions & 585 deletions package-lock.json

Large diffs are not rendered by default.

54 changes: 26 additions & 28 deletions package.json
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"
}
}
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';
57 changes: 57 additions & 0 deletions src/modules/discord/commands/adminRoles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,61 @@ export class AdminRoles {
message.channel.send(Responses.getResponse(Responses.INSUFFICIENTPERMS));
})
}

list(message: discord.Message, argsArray: string[]) {
Access.has(message.member, [Access.ADMIN, Access.FORBIDDEN])
.then(() => {
if (argsArray.length === 1) {
let guildId = message.guild.id;

this.db.model.guild.findOne({ guild_id: guildId })
.then(guild => {
if (guild) {
if (guild.admin_roles_id && guild.admin_roles_id.length !== 0) {
let embed = new discord.RichEmbed();
embed.setTitle("Admin Roles");
embed.setColor([255, 0, 255]);
let idList = "";
guild.admin_roles_id.forEach(id => {
idList += `${id} - @${message.guild.roles.get(id).name}\n`;
});
embed.addField("Ids and Names", idList);
embed.setTimestamp(new Date());
message.channel.send({ embed })
.catch(err => {
console.log(err);
});
} else {
message.channel.send(Responses.getResponse(Responses.FAIL))
.then(() => {
message.channel.send("You don't have any admin roles set up");
})
.catch(err => {
console.log(err);
});
}
} else {
message.channel.send(Responses.getResponse(Responses.FAIL))
.then(() => {
message.channel.send("Your guild is not set yet");
})
.catch(err => {
console.log(err);
});
}
})
.catch(err => {
message.channel.send(Responses.getResponse(Responses.FAIL));
console.log(err);
})
} else if (argsArray.length > 1) {
message.channel.send(Responses.getResponse(Responses.TOOMANYPARAMS));
} else {
message.channel.send(Responses.getResponse(Responses.NOPARAMS));
}
})
.catch(() => {
message.channel.send(Responses.getResponse(Responses.INSUFFICIENTPERMS));
})
}
}
54 changes: 54 additions & 0 deletions src/modules/discord/commands/bgsChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,58 @@ export class BGSChannel {
message.channel.send(Responses.getResponse(Responses.INSUFFICIENTPERMS));
})
}

show(message: discord.Message, argsArray: string[]) {
Access.has(message.member, [Access.ADMIN, Access.FORBIDDEN])
.then(() => {
if (argsArray.length === 1) {
let guildId = message.guild.id;

this.db.model.guild.findOne({ guild_id: guildId })
.then(guild => {
if (guild) {
if (guild.bgs_channel_id && guild.bgs_channel_id.length !== 0) {
let embed = new discord.RichEmbed();
embed.setTitle("BGS Channel");
embed.setColor([255, 0, 255]);
let id = `${guild.bgs_channel_id} - @${message.guild.roles.get(guild.bgs_channel_id).name}\n`;
embed.addField("Ids and Names", id);
embed.setTimestamp(new Date());
message.channel.send({ embed })
.catch(err => {
console.log(err);
});
} else {
message.channel.send(Responses.getResponse(Responses.FAIL))
.then(() => {
message.channel.send("You don't have a bgs channel set up");
})
.catch(err => {
console.log(err);
});
}
} else {
message.channel.send(Responses.getResponse(Responses.FAIL))
.then(() => {
message.channel.send("Your guild is not set yet");
})
.catch(err => {
console.log(err);
});
}
})
.catch(err => {
message.channel.send(Responses.getResponse(Responses.FAIL));
console.log(err);
})
} else if (argsArray.length > 1) {
message.channel.send(Responses.getResponse(Responses.TOOMANYPARAMS));
} else {
message.channel.send(Responses.getResponse(Responses.NOPARAMS));
}
})
.catch(() => {
message.channel.send(Responses.getResponse(Responses.INSUFFICIENTPERMS));
})
}
}
Loading

0 comments on commit 72f6fe2

Please sign in to comment.