Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
added event for member added to server to be messaged
  • Loading branch information
drubiodev committed Mar 4, 2018
commit d234d5e116b78edb1974bac8dd5686ea9ae48997
6 changes: 1 addition & 5 deletions bot/baseCommand.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
class BaseCommand {
constructor(
command, example, title,
description, action, responseType = 'reply',
adminOnly = false, showWithHelp = true, allowInDM = false,
) {
constructor(command, example, title, description, action, responseType = 'reply', adminOnly = false, showWithHelp = true, allowInDM = false ) {
this.command = command;
this.example = example;
this.title = title;
Expand Down
66 changes: 40 additions & 26 deletions bot/baseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class BaseController {
// Creates an empty object container
const messageContainer = {};
// Tokenizes the message by space characters, adds to container
messageContainer.parsed = message.content.split(' ');
messageContainer.parsed = message
.content
.split(' ');
// Adds message object to container
return Object.assign(message, messageContainer);
}
Expand All @@ -22,49 +24,61 @@ class BaseController {
if (commandResponse !== null) {
// Determine how to respond to message
if (command.responseType === 'reply') {
return this.message.reply(commandResponse);
return this
.message
.reply(commandResponse);
} else if (command.responseType === 'dm') {
return this.message.author.send(commandResponse);
return this
.message
.author
.send(commandResponse);
}
}
// Fail safe
return false;
}

onError(errorMessage = 'I Broke... Beep...Boop...Beep') {
return this.message.reply(errorMessage);
return this
.message
.reply(errorMessage);
}

// Execute the command's functionality
run() {
// Loop through each command and map to key
util.log('Looping through controller commands', 0);
Object.keys(this.commands).map((key) => {
// If command matches message
if (this.message.parsed[0].toLowerCase() === this.commands[key].command.toLowerCase()) {
util.log('Matching command found', this.commands[key].command, 2);
Object
.keys(this.commands)
.map((key) => {
// If command matches message
if (this.message.parsed[0].toLowerCase() === this.commands[key].command.toLowerCase()) {
util.log('Matching command found', this.commands[key].command, 2);

// If user messages the bot a channel-only command
if (!this.commands[key].allowInDM && !this.message.guild) {
return this.onError('Please don\'t use this command directly. Instead use it in a channel on a server. :beers:');
}
// If user messages the bot a channel-only command
if (!this.commands[key].allowInDM && !this.message.guild) {
return this.onError('Please don\'t use this command directly. Instead use it in a channel on a server' +
'. :beers:');
}

// If non-admin enters admin command
if (this.commands[key].adminOnly && !isAdmin(this.message.member)) {
return this.onError('You don\'t have permissions to run this command.');
}
// If non-admin enters admin command
if (this.commands[key].adminOnly && !isAdmin(this.message.member)) {
return this.onError('You don\'t have permissions to run this command.');
}

// Execute command's action
const commandResponse = this.commands[key].action();
// Handle if command responds or breaks
if (commandResponse) {
this.onSuccess(commandResponse, this.commands[key]);
} else {
this.onError();
// Execute command's action
const commandResponse = this
.commands[key]
.action();
// Handle if command responds or breaks
if (commandResponse) {
this.onSuccess(commandResponse, this.commands[key]);
} else {
this.onError();
}
}
}
return this.commands[key];
});
return this.commands[key];
});
}
}
module.exports = BaseController;
8 changes: 6 additions & 2 deletions bot/botUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ exports.generateCode = (n) => {
// Checks if person is an admin user, use GuildMember object
exports.isAdmin = (member) => {
const adminRoles = [
'Admin', 'Armada Officers', 'Armada Officer', 'Fleet Officer',
'Moderator', 'Tester',
'Admin',
'Armada Officers',
'Armada Officer',
'Fleet Officer',
'Moderator',
'Tester',
];
if (adminRoles.some(role => member.roles.find('name', role))) {
return true;
Expand Down
62 changes: 36 additions & 26 deletions bot/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const util = require('apex-util');
const { isAdmin } = require('./botUtils.js');

// If production server, set default debug mode to production setting
if (process.env.NODE_ENV === 'production' && !process.env.DEBUG_MODE) process.env.DEBUG_MODE = 0;
if (process.env.NODE_ENV === 'production' && !process.env.DEBUG_MODE) {
process.env.DEBUG_MODE = 0;
}

const client = new Discord.Client();

Expand All @@ -12,7 +14,7 @@ const controllers = require('./controllers')();

// Alert when ready
client.on('ready', () => {
util.log('Bot Online and Ready', 0);
util.log('Bots Online and Ready', 0);
});

// Listen for messages
Expand All @@ -25,30 +27,34 @@ client.on('message', (message) => {
let helpString = 'v1.4.0 Discovered Commands:\n\n\t**<> - Required Item\t\t[] - Optional Item**';

// Process message against every controller
Object.keys(controllers).forEach((key) => {
// Instantiate the controller
const controllerInstance = new controllers[key](message);
util.log('Controller instance', controllerInstance, 5);
// Runs commands after constructor is called
controllerInstance.run();

// Loop through commands if help command and add to string
if (message.content.toLowerCase() === '!help') {
Object.keys(controllerInstance.commands).forEach((commandKey) => {
const commandInstance = controllerInstance.commands[commandKey];
// Check if command should be shown in help menu
if (commandInstance.showWithHelp) {
if (commandInstance.adminOnly && isAdmin(message.member)) {
helpString += `\n\n \`${commandInstance.example}\` **- Admin Only** \n\t ${commandInstance.description}`;
} else if (commandInstance.adminOnly && !isAdmin(message.member)) {
helpString += '';
} else {
helpString += `\n\n \`${commandInstance.example}\` \n\t ${commandInstance.description}`;
}
}
});
}
});
Object
.keys(controllers)
.forEach((key) => {
// Instantiate the controller
const controllerInstance = new controllers[key](message);
util.log('Controller instance', controllerInstance, 5);
// Runs commands after constructor is called
controllerInstance.run();

// Loop through commands if help command and add to string
if (message.content.toLowerCase() === '!help') {
Object
.keys(controllerInstance.commands)
.forEach((commandKey) => {
const commandInstance = controllerInstance.commands[commandKey];
// Check if command should be shown in help menu
if (commandInstance.showWithHelp) {
if (commandInstance.adminOnly && isAdmin(message.member)) {
helpString += `\n\n \`${commandInstance.example}\` **- Admin Only** \n\t ${commandInstance.description}`;
} else if (commandInstance.adminOnly && !isAdmin(message.member)) {
helpString += '';
} else {
helpString += `\n\n \`${commandInstance.example}\` \n\t ${commandInstance.description}`;
}
}
});
}
});

// If help command called, display string
if (message.content.toLowerCase() === '!help') {
Expand All @@ -57,4 +63,8 @@ client.on('message', (message) => {
}
});

client.on('guildMemberAdd', (member) => {
member.send(`Welcome to the server ${member}`);
});

client.login(process.env.TOKEN);
36 changes: 15 additions & 21 deletions bot/controllers/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,39 @@ class ChannelController extends BaseController {
super(message);
const controller = this;
this.commands = [
new Command(
'!channels',
'!channels',
'List All Channels',
'List all available Armada channels.',
this.channelsAction.bind(controller),
'dm',
),
new Command(
'!announce',
'!announce <channel_name>,[channel_name] <message>',
'Announce To Channels',
'Broadcast to multiple channels. Channels are case-sensitive.',
this.announceAction.bind(controller),
'reply',
true,
),
new Command('!channels', '!channels', 'List All Channels', 'List all available Armada channels.', this.channelsAction.bind(controller), 'dm'),
new Command('!announce', '!announce <channel_name>,[channel_name] <message>', 'Announce To Channels', 'Broadcast to multiple channels. Channels are case-sensitive.', this.announceAction.bind(controller), 'reply', true),
];
}

channelsAction() {
const { message } = this;
const channels = [];
message.guild.channels.map(channel => channels.push(channel.name));
message
.guild
.channels
.map(channel => channels.push(channel.name));
return 'List of all Armada Channels: \n\n' + channels.join('\n');
}

announceAction() {
const { message } = this;
const channels = message.parsed[1].split(',');
const channels = message
.parsed[1]
.split(',');
util.log('Multiple Channels Parsing', channels, 4);

channels.map((channel) => {
const targetChannel = message.guild.channels.find('name', channel);
const targetChannel = message
.guild
.channels
.find('name', channel);
const sender = message.author.username;
util.log('Asking API for Channel', targetChannel, 4);

if (targetChannel === null) {
return '"' + channel + '" is not a known channel. Try `!channels` to get a list of all Channels (They are case-sensitive)';
return '"' + channel + '" is not a known channel. Try `!channels` to get a list of all Channels (They ar' +
'e case-sensitive)';
}

// Set parsed value to 2 for message.
Expand Down
32 changes: 18 additions & 14 deletions bot/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@ module.exports = () => {
const validExtensions = ['js'];

// Auto-load controller files
fs.readdirSync(targetPath).forEach((file) => {
// Skip self-recursive loading
if (file === 'index.js') return false;
fs
.readdirSync(targetPath)
.forEach((file) => {
// Skip self-recursive loading
if (file === 'index.js') {
return false;
}

// Split File name on "."
const splitFile = file.split('.');
// Split File name on "."
const splitFile = file.split('.');

// Isolate last position of the splitFile, it should be the File Ext
const currentExt = splitFile[splitFile.length - 1];
// Isolate last position of the splitFile, it should be the File Ext
const currentExt = splitFile[splitFile.length - 1];

// Check if extension is on approved array
if (validExtensions.includes(currentExt)) {
// Add controller to list
controllers.push(require(`./${file}`));
}
return true;
});
// Check if extension is on approved array
if (validExtensions.includes(currentExt)) {
// Add controller to list
controllers.push(require(`./${file}`));
}
return true;
});

util.log('Controllers found within target path' + targetPath, controllers, 3);
return controllers;
Expand Down
Loading