Skip to content

Commit

Permalink
Reformat code to be compliant with standardjs
Browse files Browse the repository at this point in the history
  • Loading branch information
xsduan committed Jan 7, 2018
1 parent 97ff147 commit 6176ab6
Show file tree
Hide file tree
Showing 6 changed files with 1,874 additions and 218 deletions.
196 changes: 96 additions & 100 deletions bot.js
Original file line number Diff line number Diff line change
@@ -1,133 +1,129 @@
'use strict';
'use strict'

//-----------
// vars
//-----------
/*
* vars
*/

// libraries
const Discord = require('discord.js');
const Discord = require('discord.js')

// local modules
const embed = require('./embed/embed.js');
const x2i = require('./x2i/x2i.js');
const help = require('./help/help.js');
const embed = require('./embed/embed.js')
const x2i = require('./x2i/x2i.js')
const help = require('./help/help.js')

// data files
const auth = require('./auth.json');
const settings = require('./settings.json');
const auth = require('./auth.json')
const settings = require('./settings.json')

// lifetime objects
const bot = new Discord.Client();
const guild = new Discord.Guild();
const bot = new Discord.Client()

//-----------
// functions
//-----------
/*
* functions
*/

const logMessage = function (status, message = null) {
var log = '(' + status + ')';
function logMessage (status, message = null) {
var log = '(' + status + ')'

if (message != null) {
log += ' ' + message;
}
if (message != null) {
log += ' ' + message
}

console.log(log);
console.log(log)
}

const parse = function (message) {
command(message);
x2iExec(message);
function parse (message) {
command(message)
x2iExec(message)
}

const command = function (message) {
// commands
const prefixRegex = new RegExp('(?:^' + settings.prefix + ')(\\S*)');
var command;
if (command = message.content.match(prefixRegex)) {
command = command[1];
var tokens = message.content.split(' ');

switch (command) {
// ping command is special case response.
case 'ping':
ping(message);
break;
// all the following commands are handled the same.
case 'help':
help.help(message.channel, bot.user)
.then(() => logMessage('success:command/help'))
.catch(err => logMessage('error:command/help', err));
}

logMessage('processed:command/' + command);
function command (message) {
// commands
const prefixRegex = new RegExp('(?:^' + settings.prefix + ')(\\S*)')
var command = message.content.match(prefixRegex)
if (command !== null) {
command = command[1]

switch (command) {
// ping command is special case response.
case 'ping':
ping(message)
break
// all the following commands are handled the same.
case 'help':
help.help(message.channel, bot.user)
.then(() => logMessage('success:command/help'))
.catch(err => logMessage('error:command/help', err))
}

logMessage('processed:command/' + command)
}
}

const ping = function (message) {
const elapsed = new Date().getTime() - message.createdTimestamp;
message.channel.send('I\'m alive! (' + elapsed + ' ms)')
.then(() => logMessage('success:command/ping/' + elapsed + 'ms'))
.catch(err => logMessage('error:command/ping', err));
function ping (message) {
const elapsed = new Date().getTime() - message.createdTimestamp
message.channel.send('I\'m alive! (' + elapsed + ' ms)')
.then(() => logMessage('success:command/ping/' + elapsed + 'ms'))
.catch(err => logMessage('error:command/ping', err))
}

const x2iExec = function (message) {
var results = x2i.grab(message.content);

if (results.length !== 0) {
var response = {
color: settings.embeds.colors.success
};
var logCode = 'all';

// check timeout
var timedOut = results.length > settings.embeds.timeoutChars;
if (timedOut) {
response.fields = [{ name: 'Timeout', value: settings.embeds.timeoutMessage }]
results = results.slice(0, settings.embeds.timeoutChars - 1) + '…';

logCode = 'partial';
response.color = settings.embeds.colors.warning;
}

response.description = results;

message.channel.send(embed.output({ embed: response }))
.then(() => logMessage('success:x2i/' + logCode))
.catch(err => logMessage('error:x2i/' + logCode, err));

logMessage('processed:x2i');
} else {
logMessage('ignored:x2i');
function x2iExec (message) {
var results = x2i.grab(message.content)

if (results.length !== 0) {
var response = {
color: settings.embeds.colors.success
}
var logCode = 'all'

// check timeout
var timedOut = results.length > settings.embeds.timeoutChars
if (timedOut) {
response.fields = [{ name: 'Timeout', value: settings.embeds.timeoutMessage }]
results = results.slice(0, settings.embeds.timeoutChars - 1) + '…'

logCode = 'partial'
response.color = settings.embeds.colors.warning
}

response.description = results

message.channel.send(embed.output({ embed: response }))
.then(() => logMessage('success:x2i/' + logCode))
.catch(err => logMessage('error:x2i/' + logCode, err))

logMessage('processed:x2i')
} else {
logMessage('ignored:x2i')
}
}

//-----------
// main
//-----------
/*
* main
*/

bot.on('ready', () => {
console.log('Bot ready. Setting up...');
console.log('Bot ready. Setting up...')

console.log('Changing game status...');
bot.user.setGame(settings.activeMessage)
.then(() => console.log('Set game status.'))
.catch(function (error) {
console.log('Game couldn\'t be set.');
});
});
console.log('Changing game status...')
bot.user.setGame(settings.activeMessage)
.then(() => console.log('Set game status.'))
.catch(err => console.log('Game couldn\'t be set. ' + err))
})

bot.on('message', message => {
logMessage('-start ' + message.createdTimestamp + '-')
if (!message.author.bot) {
parse(message);
} else {
logMessage('ignored:bot');
}
logMessage('-start ' + message.createdTimestamp + '-')
if (!message.author.bot) {
parse(message)
} else {
logMessage('ignored:bot')
}

logMessage('-processed ' + message.createdTimestamp + '-');
logMessage('-processed ' + message.createdTimestamp + '-')

// separate message return statuses in logs
console.log();
});
// separate message return statuses in logs
console.log()
})

bot.login(auth.token);
bot.login(auth.token)
74 changes: 37 additions & 37 deletions embed/embed.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
'use strict';
'use strict'

//-----------
// vars
//-----------
/*
* vars
*/

// data files
const settings = require('../settings.json');

//-----------
// functions
//-----------

const handleTitle = function (message) {
var title = '';
if (message.embed.title !== undefined) {
title = '**' + message.embed.title + '**\n';
}
return title;
const settings = require('../settings.json')

/*
* functions
*/

function handleTitle (message) {
var title = ''
if (message.embed.title !== undefined) {
title = '**' + message.embed.title + '**\n'
}
return title
}

const handleDescription = function (message) {
return message.embed.description !== undefined
? message.embed.description + '\n\n'
: '\n';
function handleDescription (message) {
return message.embed.description !== undefined
? message.embed.description + '\n\n'
: '\n'
}

const handleBody = function (message, headersImportant) {
var body = '';
if (message.embed.fields !== undefined) {
message.embed.fields.forEach(function (field) {
var fieldString = '';
function handleBody (message, headersImportant) {
var body = ''
if (message.embed.fields !== undefined) {
message.embed.fields.forEach(function (field) {
var fieldString = ''

if (headersImportant) {
fieldString += '**' + field.name + '**\n';
}
if (headersImportant) {
fieldString += '**' + field.name + '**\n'
}

body += fieldString + field.value + '\n';
});
}
return body;
body += fieldString + field.value + '\n'
})
}
return body
}

//-----------
// exports
//-----------
/*
* exports
*/

exports.output = function (message, headersImportant = true) {
return settings.embeds.active ? message : exports.strip(message, headersImportant);
return settings.embeds.active ? message : exports.strip(message, headersImportant)
}

exports.strip = function (message, headersImportant = true) {
return handleTitle(message) + handleDescription(message) + handleBody(message, headersImportant);
return handleTitle(message) + handleDescription(message) + handleBody(message, headersImportant)
}
Loading

0 comments on commit 6176ab6

Please sign in to comment.