Skip to content

Commit

Permalink
Merge pull request #36 from SayakMukhopadhyay/chart-commands
Browse files Browse the repository at this point in the history
Added chart genaration and its themes settings
  • Loading branch information
SayakMukhopadhyay authored Oct 23, 2018
2 parents 9cc8ade + 8faffde commit 47c37f6
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 29 deletions.
38 changes: 15 additions & 23 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "bgsbot",
"version": "1.3.0",
"version": "1.4.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": {
"content-disposition": "^0.5.2",
"cron": "^1.4.1",
"debug": "^4.1.0",
"discord.js": "^11.4.2",
Expand All @@ -17,6 +18,7 @@
"request": "^2.88.0"
},
"devDependencies": {
"@types/content-disposition": "^0.5.2",
"@types/cron": "^1.3.0",
"@types/debug": "0.0.31",
"@types/express": "^4.16.0",
Expand Down
9 changes: 5 additions & 4 deletions src/db/interfaces/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ export interface IGuild {
bgs_time: string,
sort: string,
sort_order: number,
theme: string,
admin_roles_id: string[],
forbidden_roles_id: string[],
created_at: Date,
updated_at: Date,
monitor_systems: [{
monitor_systems: {
primary: boolean,
system_name: string,
system_name_lower: string,
Expand All @@ -34,12 +35,12 @@ export interface IGuild {
y: number,
z: number
}
}],
monitor_factions: [{
}[],
monitor_factions: {
primary: boolean,
faction_name: string,
faction_name_lower: string
}],
}[],
custom: {
set: boolean,
requester_user_id: string
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 @@ -26,6 +26,7 @@ export let guildSchema: Schema = new Schema({
bgs_time: String,
sort: String,
sort_order: Number, // 1 of increasing and -1 for decreasing and 0 for disable
theme: String,
admin_roles_id: [String],
forbidden_roles_id: [String],
created_at: {
Expand Down
4 changes: 3 additions & 1 deletion src/modules/discord/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as discord from 'discord.js';
import { DiscordSecrets } from '../../secrets';
import { Responses } from './responseDict';
import { Hi, Help, MyGuild, BGSRole, AdminRoles, ForbiddenRoles, BGSChannel, MonitorSystems, MonitorFactions, SystemStatus, FactionStatus, BGSReport, Sort } from './commands';
import { Hi, Help, MyGuild, BGSRole, AdminRoles, ForbiddenRoles, BGSChannel, MonitorSystems, MonitorFactions, SystemStatus, FactionStatus, BGSReport, Sort, Chart, Theme } from './commands';
import { HouseKeeping } from './houseKeeping';
import { HelpSchema } from '../../interfaces/typings';
import App from '../../server';
Expand Down Expand Up @@ -109,6 +109,8 @@ export class DiscordClient {
this.commandsMap.set("factionstatus", new FactionStatus());
this.commandsMap.set("bgsreport", new BGSReport());
this.commandsMap.set("sort", new Sort());
this.commandsMap.set("chart", new Chart());
this.commandsMap.set("theme", new Theme());
}

private initiateCustom(): void {
Expand Down
107 changes: 107 additions & 0 deletions src/modules/discord/commands/chart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* KodeBlox Copyright 2018 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 * as discord from 'discord.js';
import * as request from 'request';
import * as contentDisposition from 'content-disposition';
import App from '../../../server';
import { Responses } from '../responseDict';
import { DB } from '../../../db/index';
import { Access } from './../access';
import { OptionsWithUrl } from 'request';

export class Chart {
db: DB;
constructor() {
this.db = App.db;
}
exec(message: discord.Message, commandArguments: string): void {
let argsArray: string[] = [];
if (commandArguments.length !== 0) {
argsArray = commandArguments.split(" ");
}
if (argsArray.length > 0) {
let command = argsArray[0].toLowerCase();
if (this[command]) {
this[command](message, argsArray);
} else {
message.channel.send(Responses.getResponse(Responses.NOTACOMMAND));
}
} else {
message.channel.send(Responses.getResponse(Responses.NOPARAMS));
}
}

get(message: discord.Message, argsArray: string[]): void {
Access.has(message.member, [Access.ADMIN, Access.BGS, Access.FORBIDDEN])
.then(() => {
if (argsArray.length >= 4) {
let systemName: string = argsArray.slice(3).join(" ").toLowerCase();
let timenow = Date.now();

this.db.model.guild.findOne({ guild_id: message.guild.id })
.then(guild => {
if (guild) {
let theme = 'light';
if (guild.theme) {
theme = guild.theme;
}
let requestOptions: OptionsWithUrl = {
url: `http://elitebgs.kodeblox.com/chartgenerator/${argsArray[1]}/${argsArray[2]}`,
method: "GET",
qs: {
name: systemName,
timemin: timenow - 10 * 24 * 60 * 60 * 1000,
timemax: timenow,
theme: theme
},
encoding: null
}

request(requestOptions, (error, response, body: Buffer) => {
if (!error && response.statusCode == 200) {
let attachment = new discord.Attachment(body, contentDisposition.parse(response.headers['content-disposition']).parameters.filename);
message.channel.send(attachment);
} else {
if (error) {
console.log(error);
} else {
console.log(response.statusMessage);
}
}
});
}
})
.catch(err => {
message.channel.send(Responses.getResponse(Responses.FAIL));
console.log(err);
});
}
})
}

help() {
return [
'chart',
'Generates a chart for the last 7 days',
'chart get <factions|systems> <influence|state> <system name|faction name>',
[
'`@BGSBot chart get systems influence qa\'wakana`',
'`@BGSBot chart get factions state knights of karma`'
]
];
}
}
2 changes: 2 additions & 0 deletions src/modules/discord/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ export * from './systemStatus';
export * from './factionStatus';
export * from './bgsReport';
export * from './sort';
export * from './chart';
export * from './theme';
Loading

0 comments on commit 47c37f6

Please sign in to comment.