Skip to content

Commit

Permalink
Fixed some things + status command (untested)
Browse files Browse the repository at this point in the history
  • Loading branch information
MeLlamoPablo committed Jan 12, 2017
1 parent 5594b93 commit 07a80e5
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 35 deletions.
22 changes: 5 additions & 17 deletions lib/commands/admin/force-lobby-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,13 @@ module.exports = new Clapp.Command({
fn: (argv, context) => {
return new Promise((fulfill, reject) => {
db.events.get(argv.args.event).then(event => {
if (event === null) {
fulfill("Error: the specified event `" + argv.args.event + "` doesn't exist.");
} else {
if (event !== null) {
db.events.getLobbyBotId(event)
// Here, "closing" actually means "starting". TODO NOT TRUE LUL
.then(botID => context.dotaHandler.closeLobby(botID, true))
.then(botID => context.dotaHandler.forceLobbyStart(botID))
.then(() => fulfill(`Forced lobby start for the event ${event.id}.`))
.catch(err => {
if (err.ECloseLobbyError) {
switch (err.ECloseLobbyError) {
case ECloseLobbyError.BOT_NOT_IN_LOBBY:
fulfill(`The lobby for the event ${event.id} has not \n` +
`been created yet.`);
break;
}
} else {
reject(err);
}
});
.catch(reject);
} else {
fulfill("Error: the specified event `" + argv.args.event + "` doesn't exist.");
}
}).catch(reject);
});
Expand Down
23 changes: 12 additions & 11 deletions lib/commands/admin/remove-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ module.exports = new Clapp.Command({
let eventName = event.name;
let deleteSummaryPromise = context.summaryHandler.deleteSummary(event);
let deleteEventPromise = event.deleteEvent();
// TODO this needs updating!
let closeLobbyPromise = new Promise((fulfill, reject) => {
// If the event has an active lobby, close it
if (
context.dotaHandler.currentLobbyEvent &&
context.dotaHandler.currentLobbyEvent.id === event.id
) {
context.dotaHandler.closeLobby(true).then(fulfill).catch(reject);
} else {
fulfill();
}
let closeLobbyPromise = new Promise((fulfill2, reject2) => {
db.getLobbyBotId(event)
.then(botID => {
if (context.dotaHandler.isBotInLobby(botID)) {
context.dotaHandler.closeLobby(botID, true)
.then(fulfill2)
.catch(reject2);
} else {
fulfill2();
}
})
.catch(reject2);
});

Promise.all(
Expand Down
40 changes: 40 additions & 0 deletions lib/commands/admin/status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"use strict";

const Clapp = require("../../modules/clapp-discord");

module.exports = new Clapp.Command({
name: "status",
desc: "Gets information about the Discord bot and Steam bots sent to you on a DM",
fn: (argv, context) => {
return new Promise((fulfill, reject) => {
let bots = context.dotaHandler.bots;

let msg = "Hello! Here's the bot status information:\n\n";

let genValueString = (key, value, newline = true) => {
return `- **${key}**: \`${value}\`${newline ? "\n" : ""}`;
};

msg += genValueString("Uptime", context.summaryHandler.bot.uptime);

for (let i = 0; i < bots.length; i++) {
let bot = bots[i];

msg += genValueString(
`Bot #${bot.id}`,
context.dotaHandler.isBotInLobby(bot.id)
? `In lobby: ${bot.currentLobby.name} (Event #${bot.currentLobby.event.id})`
: `Not in lobby`,
i === bots.length
);
}

context.msg.author.sendMessage(msg)
.then(() => fulfill("The information you requested was sent to you in a DM."))
.catch(reject);
});
},
args: [
require("./shared/event")
]
});
4 changes: 1 addition & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,4 @@ Promise.all(startupPromises).then(values => {
}).catch(err => {
console.error(err);
process.exit(1);
});

// TODO add status admin command?
});
1 change: 0 additions & 1 deletion lib/modules/dotahandler/DotaClientX.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ class DotaClientX extends Dota2.Dota2Client {
this.inviteAll().catch(reject);
fulfill();
} else {
// TODO this is giving error 2 on lobby creation
reject(new Error("[DOTA] Error creating lobby - Error code: " + err
+ "\nList of error codes: https://github.com/Arcana/node-dota2/blob/86f02" +
"0e01dba86af3ec02323a8325243b8d9d0c2/handlers/helper.js#L5\n\n" +
Expand Down
30 changes: 30 additions & 0 deletions lib/modules/dotahandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ class DotaHandler { // TODO !!!! make sure that all commands use the correct api
});
}

/**
* @param {number} botID botID The id of the bot hosting the lobby.
* @returns {Promise}
*/
forceLobbyStart(botID) {
return new Promise((fulfill, reject) => {
let client = this._getDotaClientById(botID);

if (client !== null) {
client.forceStart().then(fulfill).catch(reject);
} else {
reject(new Error(`The specified bot with ID ${botID} doesn't exist.`));
}
});
}

/**
* Closes a lobby. This makes the current lobby be null, makes the bot leave the lobby, and
* executes ScheduledEvent.setLobbyStatus();
Expand Down Expand Up @@ -126,6 +142,20 @@ class DotaHandler { // TODO !!!! make sure that all commands use the correct api
}
}

/**
* @param {number} botID botID The id of the bot hosting the lobby.
* @returns {boolean} Whether or not the requested bot is in a lobby.
*/
isBotInLobby(botID) {
let client = this._getDotaClientById(botID);

if (client !== null) {
return client.isInLobby();
} else {
throw new Error(`The bot with ID ${botID} doesn't exist.`);
}
}

/**
* @returns {DotaClientX|null} An available DotaClientX or null if none is available.
* @private
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/steambotshandler/SteamBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ class SteamBot {

fulfill();
} else {
let err = new Error(this.prefix + " Login failed. Re-check your credentials.");
let err = new Error(this.prefix.replace("SECTION", "STEAM")
+ " Login failed. Re-check your credentials.");
err.steamResponse = response;
reject(err);
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/setup-steam.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,13 @@ function removeBot(id, client) {
process.exit(1);
}

console.log("\nThis bot has been successfully removed from the" +
console.log("\nThis bot has been successfully removed from the " +
"database.");
process.exit();

});
} else {
console.log("\nThis bot has been successfully removed from the" +
console.log("\nThis bot has been successfully removed from the " +
"database.");
process.exit();
}
Expand Down

0 comments on commit 07a80e5

Please sign in to comment.