Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.gitignore/
data/config.js
node_modules/
package-lock.json
Expand Down
6 changes: 3 additions & 3 deletions src/commands/command.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
Just random command info
*/
module.exports = class Comamnd {
module.exports = class Command {
/*
* @param {String} description Description of command
* @param {String} example Example useage of command
* @param {String} example Example usage of command
* @param {String or function(MessageInfo)} action Command action
*/
constructor (description, example, action) {
this.description = description;
this.example = example;
this.action = action;
}
}
};
13 changes: 9 additions & 4 deletions src/commands/command_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ module.exports = class CommandHandlerBase {

if (this.simpleCommands.has(command)) {
message.channel.send(this.simpleCommands.get(command).action);
return;
}
else if (this.commands.has(command)) {

if (this.commands.has(command)) {
args.splice(0, 1);//remove command name
const cmd = this.commands.get(command);
cmd.action(message, args, client);
Expand Down Expand Up @@ -76,8 +78,11 @@ module.exports = class CommandHandlerBase {
.setColor("#09f228");

function addOutput(m) {
m.forEach(function(command, commandName, _) {
if (commandName === "help") return;
m.forEach((command, commandName, _) => {
if (commandName === "help") {
return;
}

output.addField(`**__${commandName}__**`,
`Description: ${command.description}\nExample: *${command.example}*`);
});
Expand All @@ -86,4 +91,4 @@ module.exports = class CommandHandlerBase {
addOutput(this.commands);
msgInfo.channel.send(output);
}
}
};
7 changes: 3 additions & 4 deletions src/commands/default_command_handler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const CommandHandler = require('./command_handler');
const Util = require('../util')
const Util = require('../util');

module.exports = class DefaultCommands extends CommandHandler {
constructor() {
Expand All @@ -17,11 +17,10 @@ module.exports = class DefaultCommands extends CommandHandler {
)*/
}


getCommands() {
return new Map([...this.simpleCommands, ...this.commands])
}
}
};

//8-ball command
const BALL_RESULTS = ["Yes.", "Reply hazy, try again.", "Without a doubt.",
Expand Down Expand Up @@ -50,4 +49,4 @@ function eightball(message, args)
// Get result
const RESPONSE_INDEX = Util.getRandomInt(0, BALL_RESULTS.length);
message.channel.send(`🎱 The Magic 8-Ball says: "${BALL_RESULTS[RESPONSE_INDEX]}" 🎱`);
}
}
13 changes: 8 additions & 5 deletions src/commands/poll_command_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = class PollCommandHandler extends CommandHandler {
pollOptions
);
}
}
};

/**
* Sends a poll message for a yes/no question
Expand All @@ -35,6 +35,7 @@ module.exports = class PollCommandHandler extends CommandHandler {
*/
function pollYesno(message, args) {
const question = args.join(" ");

if (question == "" || question == " ") {
createHopsonPollingStationEmbed(message.channel, "Please add a question.");
return;
Expand All @@ -57,18 +58,19 @@ function pollYesno(message, args) {
* @param {[String]} args List of string, the command arguments
*/
function pollOptions(message, args) {
// Make sure there's at least two choises
// Make sure there's at least two choices
const channel = message.channel;
if (validationDoesNotPass(args.length < 1, 'Not enough known to create a poll, please provide a question with options eg `">poll option "How many stars is my food?" 1 2 3 4 5"`', channel)) {
return;
}

if (validationDoesNotPass(!args[0].startsWith("\""), 'The question should be wrapped between two " characters.', channel)) {
return;
}
}

//Extract question
let question = "";
let full = args.join(" ").slice(1)
let full = args.join(" ").slice(1);
let isQuestion = false;
for (const c of full) {
full = full.slice(1);
Expand Down Expand Up @@ -125,6 +127,7 @@ function validationDoesNotPass(validation, errorMessage, channel) {
);
return true;
}

return false;
}

Expand All @@ -134,7 +137,7 @@ function validationDoesNotPass(validation, errorMessage, channel) {
* @param {Number} n The emoji to send, number from the array above
*/
function delayedReactWithNumber(message, n) {
// 0.5s timeout seems to be the best when theres a large number of options
// 0.5s timeout seems to be the best when there's a large number of options
setTimeout(function() {
message.react(NUM_EMOJIS[n]);
}, 500*n);
Expand Down
20 changes: 10 additions & 10 deletions src/commands/ref_command_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = class ReferenceCommandHandler extends CommandHandler {
cppReference
);
}
}
};

function cppReference(message, args) {
const channel = message.channel;
Expand Down Expand Up @@ -69,16 +69,16 @@ function cppReference(message, args) {
}

channel.send(msg);
return;
}
else {
channel.send({embed: {
color: 16525315,
fields: [{
name: "Error",
value: `I cannot find anything in C++ with ${args[0]}`
}]
}});
}

channel.send({embed: {
color: 16525315,
fields: [{
name: "Error",
value: `I cannot find anything in C++ with ${args[0]}`
}]
}});
})
.catch((error) => {
console.log(`Error: ${error}`);
Expand Down
48 changes: 31 additions & 17 deletions src/commands/role_command_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = class RoleEventHandler extends CommandHandler {
removeRoles
)
}
}
};

/**
* Outpus number of members to a single discord role
Expand All @@ -50,6 +50,7 @@ function listRoles(message) {
break;
}
}

message.channel.send(output);
}

Expand All @@ -66,12 +67,14 @@ function countRole(message, args) {
return role.name.toLowerCase() === args[0];
});

let output = '';

if (role === null) {
var output = `Role '${args[0]} does not exist.`;
}
else {
var output = `Number of users with role "**${args[0].toUpperCase()}**": ${role.members.size}`;
output = `Role '${args[0]} does not exist.`;
} else {
output = `Number of users with role "**${args[0].toUpperCase()}**": ${role.members.size}`;
}

message.channel.send(output);
}

Expand All @@ -98,22 +101,27 @@ function modifyRoles(message, args, action) {
}

if (roleLists.validRoles.length > 0) {
let verb = '';
let dir = '';

//Add/ Remove the roles
if (action === "add") {
for (const role of roleLists.validRoles) {
member.addRole(role)
.then (console.log("Role add successful"));
}
var verb = "added";
var dir = "to";

verb = "added";
dir = "to";
}
else if (action === "remove") {
for (role of roleLists.validRoles) {
member.removeRole(role)
.then (console.log("Role remove successful"));
}
var verb = "removed";
var dir = "from";

verb = "removed";
dir = "from";
}
//Send result
const output = createOutput(roleLists.validRoles, message.author.id.toString(), verb, dir);
Expand All @@ -132,15 +140,18 @@ function createOutput(rolesAdded, userID, verb, dir) {
if (rolesAdded.length === 0) {
return;
}
const sp = rolesAdded.length == 1 ? "role" : "roles";

const sp = rolesAdded.length === 1 ? "role" : "roles";
const roleNames = rolesAdded.map((role) => {
return role.name;
});

let output = `I have **${verb}** the following ${sp} ${dir} **<@${userID}>**:\n> ${roleNames.join("\n>")}\n\n`;
if (rolesAdded.length == 1) {
if (rolesAdded.length === 1) {
output += `Psst... Are you aware you can have multiple roles ${verb} at once? Give it a go!\n`;
output += `Example: \`>role add/remove C++ Java Rust\``;
}

return output;
}

Expand All @@ -153,23 +164,26 @@ function extractRoles(guild, roleList) {
const result = {
validRoles: [],
invalidRoles: []
}
};

const modifiableRoles = Config.modifiableRoles.map(val => val.toLowerCase());
for (const roleName of roleList) {
if (modifiableRoles.indexOf(roleName) > -1) {
const role = guild.roles.find((roleToFind) => {
return roleToFind.name.toLowerCase() === roleName;
});

if (role !== null) {
result.validRoles.push(role);
}
else {
result.invalidRoles.push(roleName);
continue;
}
}
else {

result.invalidRoles.push(roleName);
continue;
}

result.invalidRoles.push(roleName);
}

return result;
}
14 changes: 7 additions & 7 deletions src/deprecated/game_command_handler.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const CommandHandler = require ("./command_handler")
const CommandHandler = require ("./command_handler");
const GuessingGame = require ('../games/guessing_game');

const GAME_GUESS_NUM = 0;

module.exports = class GameCommandHandler extends CommandHandler {
constructor(gameSessions) {
super('game');
this.games = []
this.games = [];
this.initCommands();
this.gameSessions = gameSessions
}
Expand All @@ -21,13 +21,13 @@ module.exports = class GameCommandHandler extends CommandHandler {
)
}

guessNumber(message, args) {
guessNumber(message) {
if (!this.games[GAME_GUESS_NUM]) {
this.games[GAME_GUESS_NUM] = true;
this.gameSessions.push(new GuessingGame(message.channel, this.gameSessions));
return;
}
else {
message.channel.send("Sorry but guessing number game is already active.");
}

message.channel.send("Sorry but guessing number game is already active.");
}
}
};
38 changes: 22 additions & 16 deletions src/deprecated/games/guessing_game.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


module.exports = class GuessingGame {
constructor(channel, sessions) {
this.channel = channel;
Expand All @@ -11,19 +9,27 @@ module.exports = class GuessingGame {

update(message) {
const channel = message.channel;
if (channel === this.channel) {
if (!isNaN(message.content)) {
const n = Number(message.content);
if (n === this.number) {
channel.send(`${message.author} guessed correct! The number was ${this.number}`);
}
else if (n > this.number) {
channel.send(`${n} guessed, but it is too big!`)
}
else {
channel.send(`${n} guessed, but it is too small!`)
}
}

if (channel !== this.channel) {
return;
}

if (isNaN(message.content)) {
return;
}

const n = Number(message.content);

if (n === this.number) {
channel.send(`${message.author} guessed correct! The number was ${this.number}`);
return;
}

if (n > this.number) {
channel.send(`${n} guessed, but it is too big!`);
return;
}

channel.send(`${n} guessed, but it is too small!`);
}
}
};
Loading