From 4a15223fd109b73103accc4d6b159de89fcfb21f Mon Sep 17 00:00:00 2001 From: Shane Duan Date: Fri, 12 Jan 2018 22:06:44 -0800 Subject: [PATCH] Change settings handling to config btw, did you know that hjson is awesome --- .gitignore | 4 ++-- README.md | 5 +++-- auth-example.json | 3 --- bot.js | 33 ++++++++++++++++++--------------- config/default-example.hjson | 31 +++++++++++++++++++++++++++++++ embed/embed.js | 6 +++--- help/help.js | 8 ++++---- package-lock.json | 22 ++++++++++++++++++++-- package.json | 4 +++- settings.json | 15 --------------- x2i/x2i.js | 4 ++-- 11 files changed, 86 insertions(+), 49 deletions(-) delete mode 100644 auth-example.json create mode 100644 config/default-example.hjson delete mode 100644 settings.json diff --git a/.gitignore b/.gitignore index 858c784..2c1781c 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/README.md b/README.md index 3c2954a..881bf71 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/auth-example.json b/auth-example.json deleted file mode 100644 index 77d0d6a..0000000 --- a/auth-example.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "token": "000000000000000000000000.000000.000000000000000000000000000" -} \ No newline at end of file diff --git a/bot.js b/bot.js index dc00a09..4d93980 100644 --- a/bot.js +++ b/bot.js @@ -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() @@ -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] @@ -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' } @@ -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 => { @@ -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')) diff --git a/config/default-example.hjson b/config/default-example.hjson new file mode 100644 index 0000000..fbfc05f --- /dev/null +++ b/config/default-example.hjson @@ -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] + } + } +} \ No newline at end of file diff --git a/embed/embed.js b/embed/embed.js index 97dd778..d230bd6 100644 --- a/embed/embed.js +++ b/embed/embed.js @@ -5,7 +5,7 @@ */ // data files -const settings = require('../settings.json') +var cfg = require('config') /* * functions @@ -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) } diff --git a/help/help.js b/help/help.js index 1db7c2c..e7eadba 100644 --- a/help/help.js +++ b/help/help.js @@ -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'] @@ -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') diff --git a/package-lock.json b/package-lock.json index 530a60a..639f078 100644 --- a/package-lock.json +++ b/package-lock.json @@ -217,6 +217,15 @@ "typedarray": "0.0.6" } }, + "config": { + "version": "1.29.2", + "resolved": "https://registry.npmjs.org/config/-/config-1.29.2.tgz", + "integrity": "sha1-Lr3JJjnrnQb//TAvHuMuKtDpThE=", + "requires": { + "json5": "0.4.0", + "os-homedir": "1.0.2" + } + }, "contains-path": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/contains-path/-/contains-path-0.1.0.tgz", @@ -816,6 +825,11 @@ "integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q=", "dev": true }, + "hjson": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/hjson/-/hjson-3.1.0.tgz", + "integrity": "sha512-32Xt9W+25uH3/nKUkAGTrwRj0pbibWUI25pUzM13QPSRMRAHSoXgjQ1NS/tscpZ53h9CwrSy7woAlzJDCydWig==" + }, "ignore": { "version": "3.3.7", "resolved": "https://registry.npmjs.org/ignore/-/ignore-3.3.7.tgz", @@ -998,6 +1012,11 @@ "jsonify": "0.0.0" } }, + "json5": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/json5/-/json5-0.4.0.tgz", + "integrity": "sha1-BUNS5MTIDIbAkjh31EneF2pzLI0=" + }, "jsonify": { "version": "0.0.0", "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz", @@ -1185,8 +1204,7 @@ "os-homedir": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz", - "integrity": "sha1-/7xJiDNuDoM94MFox+8VISGqf7M=", - "dev": true + "integrity": "sha1-/7xJiDNuDoM94MFox+8VISGqf7M=" }, "p-limit": { "version": "1.2.0", diff --git a/package.json b/package.json index 0ba273f..c296d73 100644 --- a/package.json +++ b/package.json @@ -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": "*" diff --git a/settings.json b/settings.json deleted file mode 100644 index 89551ba..0000000 --- a/settings.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "activeMessage": "x/help for info", - "prefix": "x/", - "embeds": { - "active": true, - "timeoutChars": 2040, - "timeoutMessage": "Could not send all output as there were too many in a single message.", - "colors": { - "help": [52, 152, 219], - "success": [152, 219, 52], - "warning": [219, 119, 52], - "fail": [219, 52, 152] - } - } -} \ No newline at end of file diff --git a/x2i/x2i.js b/x2i/x2i.js index 25045ff..0dfb846 100644 --- a/x2i/x2i.js +++ b/x2i/x2i.js @@ -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) @@ -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)