Skip to content

Commit

Permalink
Change settings handling to config
Browse files Browse the repository at this point in the history
btw, did you know that hjson is awesome
  • Loading branch information
xsduan committed Jan 13, 2018
1 parent 4052599 commit 4a15223
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 49 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ typings/
.env

# for the love of god don't include this
# auth.json contains all private data such as ssh keys and tokens
auth.json
# this contains all private data such as ssh keys and tokens
config/default.hjson

# autorun
autorun.sh
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ rest in pisces connie the bot (201x - 2017)
#### here in ground control

- download and install [node.js and npm](https://nodejs.org/) if you haven't already (duh)
- set up `auth.json` using your bot token (an example with blank credentials can be found in `auth-example.json`)
- maybe look at `settings.json`?
- set up `default.hjson` using your bot token (an example can be found in `default-example.hjson`)
- [hjson tutorial](http://hjson.org/) – different from json but makes writing these things a lot easier
- maybe look at the other settings?

### deployment

Expand Down
3 changes: 0 additions & 3 deletions auth-example.json

This file was deleted.

33 changes: 18 additions & 15 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@

// libraries
const Discord = require('discord.js')
var cfg = require('config')

// local modules
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')

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

Expand Down Expand Up @@ -66,7 +63,7 @@ function command (message) {
var promise = null

// commands
const prefixRegex = new RegExp('(?:^' + settings.prefix + ')(\\S*)')
const prefixRegex = new RegExp('(?:^' + cfg.get('prefix') + ')(\\S*)')
var command = message.content.match(prefixRegex)
if (command !== null) {
command = command[1]
Expand Down Expand Up @@ -132,16 +129,16 @@ function x2iExec (message) {
function x2iSend (channel, results) {
if (results !== undefined && results.length !== 0) {
var response = new Discord.RichEmbed()
.setColor(settings.embeds.colors.success)
.setColor(cfg.get('embeds.colors.success'))
var logCode = 'all'

// check timeout
var timedOut = results.length > settings.embeds.timeoutChars
var timedOut = results.length > cfg.get('embeds.timeoutChars')
if (timedOut) {
results = results.slice(0, settings.embeds.timeoutChars - 1) + '…'
results = results.slice(0, cfg.get('embeds.timeoutChars') - 1) + '…'

response.addField('Timeout', settings.embeds.timeoutMessage)
.setColor(settings.embeds.colors.warning)
response.addField('Timeout', cfg.get('embeds.timeoutChars'))
.setColor(cfg.get('embeds.colors.warning'))

logCode = 'partial'
}
Expand All @@ -163,10 +160,12 @@ function x2iSend (channel, results) {
bot.on('ready', () => {
console.log('Bot ready. Setting up...')

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))
if (cfg.has('activeMessage')) {
console.log('Changing game status...')
bot.user.setGame(cfg.get('activeMessage'))
.then(() => console.log('Set game status.'))
.catch(err => console.log('Game couldn\'t be set. ' + err))
}
})

bot.on('message', message => {
Expand All @@ -183,4 +182,8 @@ bot.on('message', message => {
console.log()
})

bot.login(auth.token)
if (!cfg.has('token')) {
console.error('Couldn\'t find a token to connect to the token.')
}

bot.login(cfg.get('token'))
31 changes: 31 additions & 0 deletions config/default-example.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

{
// guide for setting up your own token here:
// https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-a-token
token: 000000000000000000000000.000000.000000000000000000000000000

# you can just leave this off if you want, but I suggest including it because it's useful
activeMessage: x/help for info
prefix: x/
embeds:
{
# if false then all embeds get converted into
active: true

# currently absolute timeout is 2048 because the largest field is 2048 characters long,
# and the longest normal message is only allowed to be 2000 characters (I think?)
# however if you really wanted to you could set it farther, but you'll have some rejections
# from the api server. (or you somehow purchased an override from Discord)
timeoutChars: 2040
timeoutMessage: Could not send all output as there were too many in a single message.

# you can also use hex values (in decimal form)
colors:
{
help: [52, 152, 219]
success: [152, 219, 52]
warning: [219, 119, 52]
fail: [219, 52, 152]
}
}
}
6 changes: 3 additions & 3 deletions embed/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

// data files
const settings = require('../settings.json')
var cfg = require('config')

/*
* functions
Expand Down Expand Up @@ -58,13 +58,13 @@ function handleBody (message, headersImportant) {
}

/**
* Choose appropriate message format depending on settings.embeds.active
* Choose appropriate message format depending on if settings is active
* @param {RichEmbed} message Message to convert
* @param {boolean} [headersImportant] Should keep headers?
* @returns {(RichEmbed|String)} Message converted to appropriate format
*/
function output (message, headersImportant = true) {
return settings.embeds.active
return cfg.get('embeds.active')
? message
: strip(message, headersImportant)
}
Expand Down
8 changes: 4 additions & 4 deletions help/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ const Discord = require('discord.js')
const embed = require('../embed/embed.js')

// data files
const settings = require('../settings.json')
var cfg = require('config')

const help = [
['x,z,p[phonetic] or x,z,p/phonemic/',
'Converts XSAMPA, ZSAMPA, or APIE to IPA. Hopefully.'],
[settings.prefix + 'xsampa, ' + settings.prefix + 'zsampa, or ' + settings.prefix + 'apie',
[cfg.get('prefix') + 'xsampa, ' + cfg.get('prefix') + 'zsampa, or ' + cfg.get('prefix') + 'apie',
'Converts the rest of the message into their respective formats.'],
[settings.prefix + 'help',
[cfg.get('prefix') + 'help',
'Reply with this message.'],
['\u200B',
'found a bug or want to suggest a feature?\ngithub: https://github.com/xsduan/conniebot']
Expand All @@ -35,7 +35,7 @@ const help = [
*/
function createEmbed (user) {
var helpEmbed = new Discord.RichEmbed()
.setColor(settings.embeds.colors.success)
.setColor(cfg.get('embeds.colors.success'))
.setAuthor(user.username, user.avatarURL)
.setTitle('Commands')

Expand Down
22 changes: 20 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"main": "bot.js",
"author": "Shane Duan",
"dependencies": {
"discord.js": "^11.2.1"
"config": "^1.29.2",
"discord.js": "^11.2.1",
"hjson": "^3.1.0"
},
"devDependencies": {
"standard": "*"
Expand Down
15 changes: 0 additions & 15 deletions settings.json

This file was deleted.

4 changes: 2 additions & 2 deletions x2i/x2i.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

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

// consts
// regex match indices: 2 = key (to lower), 3 = bracket left, 4 = body, 5 = bracket right, (end)
Expand Down Expand Up @@ -80,7 +80,7 @@ exports.grab = function (content) {
var matches = []
var match
var length = 0
while (length < settings.embeds.timeoutChars && (match = regex.exec(content))) {
while (length < cfg.get('embeds.timeoutChars') && (match = regex.exec(content))) {
match = match.slice(2)
if (match[1] !== '') {
const converted = exports.force(...match)
Expand Down

0 comments on commit 4a15223

Please sign in to comment.