Skip to content

Commit

Permalink
Added some config options. Final v1.2 commit-
Browse files Browse the repository at this point in the history
  • Loading branch information
MeLlamoPablo committed Jan 24, 2017
1 parent 64e97d7 commit c4fcd1b
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 62 deletions.
17 changes: 15 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ module.exports = {
// This also requires the "manage messages" permission
disallow_talking: true,

// If false, all (non-blacklisted) users will be able to add inhouses to created events.
// If true, only admins will be able to do so.
add_inhouse_is_admin_command: false,

// quick-inhouse command
// This command creates an instant lobby and adds an inhouse with the default values.
// It is the equivalent of running "@ScheduleBot create (event_name) now" and
Expand Down Expand Up @@ -106,7 +110,13 @@ module.exports = {
// --server flag to the add-inhouse command.
// Go to that command's file (Or type -schedulebot add-inhouse --help)
// to see possible values.
defaultServer: "Luxembourg",
default_server: "Luxembourg",

// If false, lobbies will be started automatically when ten people join it
// (spectators, casters and unassigned players not counted).
// If true, the games will only be able to be started with the admin command
// schedulebot-admin force-lobby-start (event)
disable_autostart: false,

// If enabled is true, the bot will ticket any lobbies using the provided league id.
// Make sure that the steam bot is an admin of that league.
Expand Down Expand Up @@ -135,7 +145,10 @@ module.exports = {
},

// In a competitive league, you might want to change this with "match" or "game",
// since it's not technically an inhouse. TODO apply this in more locations
// since it's not technically an inhouse.
//
// CAREFUL! This will change the name of the command "add-inhouse"
// to "add-whatever_you_write"
game_generic_name: "inhouse"
}
};
Expand Down
26 changes: 14 additions & 12 deletions lib/commands/general/add-inhouse.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"use strict";

const Clapp = require('../../modules/clapp-discord/index')
, cfg = require('../../../config')
, db = require('../../modules/dbhandler');
const Clapp = require('../../modules/clapp-discord/index')
, cfg = require('../../../config')
, db = require('../../modules/dbhandler')
, shouldAddN = require('../../modules/helpers/shouldAddN')
;

module.exports = new Clapp.Command({
name: "add-inhouse",
desc: "Adds a Dota 2 inhouse to an event, or edits the configuration of the already created" +
" inhouse.\n" +
name: "add-" + cfg.dota.game_generic_name,
desc: `Adds a Dota 2 ${cfg.dota.game_generic_name} to an event, or edits the configuration ` +
` of the already created ${cfg.dota.game_generic_name}.\n` +
"The event will only be able to be joined by people who have linked their steam with the" +
" link-steam command." +
"\nWhen the event time comes, every attendant will be invited to the event.",
Expand Down Expand Up @@ -47,14 +49,14 @@ module.exports = new Clapp.Command({
}).catch(reject);
context.summaryHandler.updateSummary(event).catch(reject);

fulfill("The inhouse has been added to the event `#"
+ argv.args.id + "`\n" +
fulfill("The " + cfg.dota.game_generic_name + " has been added to " +
"the event `#" + argv.args.id + "`\n" +
"If there were any people who confirmed their attendance, but " +
"didn't have their Steam account linked, they have been removed.");
}).catch(reject);
} else {
fulfill("The event's player limit must be 10 or greater in order to add" +
" an inhouse.");
` a${shouldAddN() ? "n" : ""} ${cfg.dota.game_generic_name}.`);
}
} else {
fulfill("The event `#" + argv.args.id + "` doesn't exist.");
Expand All @@ -68,7 +70,7 @@ module.exports = new Clapp.Command({
flags: [
new Clapp.Flag({
name: "gamemode",
desc: "The inhouse game mode. Possible values are:\n" +
desc: `The ${cfg.dota.game_generic_name} game mode. Possible values are:\n` +
"Captains Mode, All Pick, Ranked All Pick, Captains Draft, Random Draft, Single" +
" Draft, All Random",
type: "string",
Expand Down Expand Up @@ -98,11 +100,11 @@ module.exports = new Clapp.Command({
}),
new Clapp.Flag({
name: "server",
desc: "The inhouse server. Possible values are:\n" +
desc: `The ${cfg.dota.game_generic_name} server. Possible values are:\n` +
"US West, US East, Luxembourg, Australia, Stockholm, Singapore, Dubai, Austria," +
" Brazil, South Africa, Chile, Peru, India, Japan.",
type: "string",
default: cfg.dota.defaultServer,
default: cfg.dota.default_server,
alias: "s",
validations: [
{
Expand Down
4 changes: 3 additions & 1 deletion lib/commands/general/confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Clapp = require('../../modules/clapp-discord/index')
, db = require('../../modules/dbhandler/index')
, getMmr = require('../../modules/dotahandler/mmr')
, ELobbyStatus = require('../../structures/enums/ELobbyStatus')
, shouldAddN = require('../../modules/helpers/shouldAddN')
;

module.exports = new Clapp.Command({
Expand Down Expand Up @@ -125,7 +126,8 @@ module.exports = new Clapp.Command({
fulfill("Sorry, the event is full.");
}
} else {
fulfill("Error: this event is an inhouse, but you haven't linked" +
fulfill("Error: this event is a" + shouldAddN() ? "n" : "" + " " +
cfg.quick_inhouse.event_name + ", but you haven't linked" +
" your Steam account.\n" +
"Use the `link-steam` command to link it, then try to confirm" +
" again.");
Expand Down
20 changes: 12 additions & 8 deletions lib/commands/general/create.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"use strict";

const Clapp = require('../../modules/clapp-discord/index');
const moment = require('moment-timezone');
const db = require('../../modules/dbhandler/index').events;
const cfg = require('../../../config');
const Clapp = require('../../modules/clapp-discord/index')
, moment = require('moment-timezone')
, db = require('../../modules/dbhandler/index').events
, cfg = require('../../../config')
, shouldAddN = require('../../modules/helpers/shouldAddN')
;

module.exports = new Clapp.Command({
name: "create",
Expand Down Expand Up @@ -42,10 +44,12 @@ module.exports = new Clapp.Command({
}).catch(reject);
} else {
db.addInstant(argv.args.name, argv.flags.limit).then(id => {
fulfill("Your event `" + argv.args.name + "` was created with ID #`"
+ id + "`.\n" +
"The lobby will be started after you add an inhouse using the" +
" command `" + cfg.readable_prefix + " add-inhouse " + id + "`");
let msg =
`Your event \`${argv.args.name}\` was created with ID #\`${id}\`.\n` +
`The lobby will be started after you add a${shouldAddN() ? "n" : ""} ` +
`${cfg.dota.game_generic_name} using the command ` +
`\`${cfg.readable_prefix} add-${cfg.dota.game_generic_name} ${id}\``;
fulfill(msg);

// Post the event summary
db.get(id).then(event => {
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/general/link-steam.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ module.exports = new Clapp.Command({
.catch(console.error);

fulfill("Your steam account has been linked!\n" +
"You may now join any inhouse events.", context);
"You may now join any " + cfg.dota.game_generic_name +
" events.", context);
}).catch(reject);
} else {
fulfill("The code you introduced is not correct");
Expand Down
18 changes: 11 additions & 7 deletions lib/commands/general/quick-inhouse.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
"use strict";

const Clapp = require('../../modules/clapp-discord/index')
, cfg = require('../../../config')
, db = require('../../modules/dbhandler');
const Clapp = require('../../modules/clapp-discord/index')
, cfg = require('../../../config')
, db = require('../../modules/dbhandler')
, shouldAddN = require('../../modules/helpers/shouldAddN')
;

module.exports = new Clapp.Command({
name: cfg.quick_inhouse.command_name,
desc: "Quickly creates an inhouse with the default values. This is the equivalent of running " +
desc: `Quickly creates a${shouldAddN() ? "n" : ""} ${cfg.dota.game_generic_name} with the ` +
`default values. This is the equivalent of running ` +
`\`${cfg.readable_prefix} create ${cfg.quick_inhouse.event_name} now\` and then ` +
`\`${cfg.readable_prefix} add-inhouse (id)\`.` ,
`\`${cfg.readable_prefix} add-${cfg.dota.game_generic_name} (id)\`.` ,
fn: (argv, context) => {
return new Promise((fulfill, reject) => {
let inhouseProps = {
gameMode: "captainsmode",
server: cfg.dota.defaultServer.toLowerCase().replace(" ", ""),
server: cfg.dota.default_server.toLowerCase().replace(" ", ""),
cmPick: "random",
autoBalance: true
};
Expand All @@ -26,7 +29,8 @@ module.exports = new Clapp.Command({
.then(msgId => event.updateMsgId(msgId))
.then(() => fulfill2(event.id)).catch(reject2);
})).then(id => {
fulfill(`Your inhouse has been created with id \`#${id}\`.`);
fulfill(`Your ${cfg.quick_inhouse.event_name} has been created ` +
`with id \`#${id}\`.`);
}).catch(reject);
});
}
Expand Down
4 changes: 3 additions & 1 deletion lib/commands/general/resend-invite.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ module.exports = new Clapp.Command({
if (confirms.indexOf(context.msg.author.id) !== -1) {
db.getLobbyBotId(event).then(botID => {
if (botID === null) {
fulfill("Error: the specified event doesn't have an inhouse " +
fulfill("Error: the specified event doesn't have " +
"a" + shouldAddN() ? "n" : "" + " " +
cfg.dota.game_generic_name + " " +
"associated or the lobby hasn't been created yet.");
} else {
if (context.dotaHandler.isBotInLobby(botID)) {
Expand Down
47 changes: 34 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ let generalApp = new Clapp.App({
prefix: cfg.prefix,
version: pkg.version + "-DotaEdition",
onReply: (msg, context) => {
let send = function(content) {
masterChannel.sendMessage(content).then(botMessage => {
if (cfg.delete_after_reply.enabled) {
botMessage.delete(cfg.delete_after_reply.time).catch(console.error);
}
}).catch(console.error);
};

context.msg.reply("\n").then(botResponse => {
if (cfg.delete_after_reply.enabled) {
context.msg.delete(cfg.delete_after_reply.time).catch(console.error);
Expand All @@ -50,12 +42,12 @@ let generalApp = new Clapp.App({
}).catch(console.error);

if (typeof msg === "string") {
send(msg);
sendMessage(msg);
} else {
// Discord has a 2000 message character limit
// We overcome that limit by sending an array of messages to post
for (let i = 0; i < msg.length; i++) {
send(msg[i]);
sendMessage(msg[i]);
}
}

Expand All @@ -71,12 +63,23 @@ let adminApp = new Clapp.App({
prefix: cfg.admin_app.prefix,
version: pkg.version + "-DotaEdition",
onReply: (msg, context) => {
context.msg.reply('\n' + msg).then(bot_response => {
context.msg.reply("\n").then(bot_response => {
if (cfg.delete_after_reply.enabled) {
context.msg.delete(cfg.delete_after_reply.time).catch(console.error);
bot_response.delete(cfg.delete_after_reply.time).catch(console.error);
}
}).catch(console.error);

if (typeof msg === "string") {
sendMessage(msg);
} else {
// Discord has a 2000 message character limit
// We overcome that limit by sending an array of messages to post
for (let i = 0; i < msg.length; i++) {
sendMessage(msg[i]);
}
}

botAdmins = context.botAdmins;
blacklist = context.blacklist;
}
Expand Down Expand Up @@ -148,7 +151,17 @@ startupPromises.push(
if (!err) {
files.forEach(file => {
if (file.match(/(?:.+).js/) && bannedCommands.indexOf(file) === -1) {
generalApp.addCommand(require("./commands/general/" + file));
switch (file) {
case "add-inhouse.js":
if (cfg.add_inhouse_is_admin_command) {
adminApp.addCommand(require("./commands/general/" + file));
} else {
generalApp.addCommand(require("./commands/general/" + file));
}
break;
default:
generalApp.addCommand(require("./commands/general/" + file));
}
}
});
fulfill();
Expand Down Expand Up @@ -344,4 +357,12 @@ Promise.all(startupPromises).then(values => {
}).catch(err => {
console.error(err);
process.exit(1);
});
});

function sendMessage(content) {
masterChannel.sendMessage(content).then(botMessage => {
if (cfg.delete_after_reply.enabled) {
botMessage.delete(cfg.delete_after_reply.time).catch(console.error);
}
}).catch(console.error);
};
13 changes: 10 additions & 3 deletions lib/modules/clapp-discord/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ constructor(options) {
_getHelp() {
const LINE_WIDTH = 175;

let response = [];

var r =
this.name + (typeof this.version !== 'undefined' ? ' v' + this.version : '') + '\n' +
this.desc + '\n\n' +
Expand All @@ -21,6 +23,9 @@ _getHelp() {
str.help_cmd_list + '\n\n'
;

response.push(r);
r = "";

// Command list
var table = new Table({
chars: {
Expand All @@ -30,8 +35,8 @@ _getHelp() {
'middle': ''
},
colWidths: [
Math.round(0.15*LINE_WIDTH), // We round it because providing a decimal number would
Math.round(0.65*LINE_WIDTH) // break cli-table2
Math.round(0.13*LINE_WIDTH), // We round it because providing a decimal number would
Math.round(0.67*LINE_WIDTH) // break cli-table2
],
wordWrap: true
});
Expand All @@ -45,7 +50,9 @@ _getHelp() {
str.help_further_help + this.prefix + ' ' + str.help_command + ' --help'
;

return r;
response.push(r);

return response;
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/modules/dotahandler/DotaClientX.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ class DotaClientX extends Dota2.Dota2Client {
return e.team === 0 || e.team === 1
}).length;

if (!this.currentLobby.starting) {
if (!cfg.dota.disable_autostart && !this.currentLobby.starting) {
this.sendMessageToLobby(DotaClientX.generateStatusMessage(people));
}

this.currentLobby.enoughPeople = (people >= 10);
if (this.currentLobby.enoughPeople && !this.currentLobby.starting) {
if (!cfg.dota.disable_autostart &&
this.currentLobby.enoughPeople &&
!this.currentLobby.starting) {
this.start();
}

Expand Down Expand Up @@ -192,8 +194,6 @@ class DotaClientX extends Dota2.Dota2Client {
options.leaguid = cfg.dota.ticketing.league_id;
}

console.log(options);

this.createPracticeLobby(
this.currentLobby.password,
options,
Expand Down
7 changes: 7 additions & 0 deletions lib/modules/helpers/shouldAddN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const cfg = require("../../../config");

function shouldAddN(word = cfg.dota.game_generic_name) {
return ["a", "e", "i", "o", "u"].indexOf(word.charAt(0).toLocaleLowerCase()) !== -1;
}

module.exports = shouldAddN;
3 changes: 2 additions & 1 deletion lib/modules/summaryhandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class SummaryHandler {
break;
}
} else {
summary += "Waiting for the inhouse to be configured.\n\n";
summary += `Waiting for the ${cfg.dota.game_generic_name} to be ` +
`configured.\n\n`;
}
} else {
switch (inhouseProps.gameMode) {
Expand Down
Loading

0 comments on commit c4fcd1b

Please sign in to comment.