Skip to content

Commit

Permalink
Loads of changes
Browse files Browse the repository at this point in the history
Moved character database to SQLite
Made command run async
Refactored existing database stuff
Changed the way command errors are handled
Added double interruption handling
Other minor changes
  • Loading branch information
Gawdl3y committed Aug 2, 2016
1 parent 1254b39 commit 8eaf8dd
Show file tree
Hide file tree
Showing 36 changed files with 369 additions and 257 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"presets": ["es2015-node6"]
"presets": ["es2015-node6"],
"plugins": ["transform-async-to-generator"]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
npm-debug.log
/lib
/rpbot-storage
rpbot.sqlite3
settings.yml
rpbot.log
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/rpbot-storage
rpbot.sqlite3
settings.yml
rpbot.log
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Run `sudo npm install -g discord-rpbot --no-optional`.
There is no identifiable or potentially private/unsafe information sent whatsoever.
The only things that are being shared is the name of commands being run (no message contents), and an event for the bot starting up.
This is so that I know how many people are using the bot, and what commands are being used the most.
If you don't want anything being sent at all, use the `analytics` configuration option.
If you don't want anything being sent at all, run RPBot with the `--no-analytics` option, or set `analytics` in your config file.

## Chat commands
| Command | Description |
Expand Down
15 changes: 15 additions & 0 deletions migrations/001-initial-schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Up
CREATE TABLE characters (server_id INTEGER NOT NULL, name TEXT NOT NULL COLLATE NOCASE, info TEXT, user_id INTEGER NOT NULL);
CREATE INDEX characters_index ON characters (server_id, name);
/*CREATE TABLE mod_roles (server_id INTEGER NOT NULL, role_id INTEGER NOT NULL);
CREATE INDEX mod_roles_index ON mod_roles (server_id, role_id);
CREATE TABLE settings (server_id INTEGER, key TEXT NOT NULL, value TEXT);
CREATE INDEX settings_index ON settings (server_id, key);*/

-- Down
DROP TABLE characters;
DROP INDEX characters_index;
/*DROP TABLE mod_roles;
DROP INDEX mod_roles_index;
DROP TABLE settings;
DROP INDEX settings_index;*/
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"string-argv": "^0.0.2",
"common-tags": "^1.3.0",
"node-localstorage": "^1.3.0",
"sqlite": "^2.2.0",
"escape-string-regexp": "^1.0.0",
"dice-expression-evaluator": "^0.1.0",
"universal-analytics": "^0.4.0"
Expand All @@ -42,7 +43,8 @@
"eslint": "^3.1.0",
"babel-cli": "^6.11.0",
"babel-eslint": "^6.1.0",
"babel-preset-es2015-node6": "^0.2.0"
"babel-preset-es2015-node6": "^0.2.0",
"babel-plugin-transform-async-to-generator": "^6.8.0"
},
"engines": {
"node": ">=6.0.0"
Expand Down
12 changes: 6 additions & 6 deletions src/commands/characters/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import stringArgv from 'string-argv';
import Character from '../../database/character';
import database from '../../database/characters';
import CommandFormatError from '../../util/errors/command-format';

const newlinesPattern = /\n/g;
const newlinesReplacement = '{!~NL~!}';
Expand All @@ -26,8 +26,8 @@ export default {
return !!message.server;
},

run(message, args) {
if(!args[0]) return false;
async run(message, args) {
if(!args[0]) throw new CommandFormatError(this);
if(mentionsPattern.test(args[0])) {
message.reply('Please do not use mentions in your character name or information.');
return;
Expand All @@ -51,11 +51,11 @@ export default {
}

// Add or update the character
const result = database.saveCharacter(new Character(message.server, message.author, name, info));
const result = await Character.save(new Character(message.server, message.author, name, info));
if(result) {
message.reply(`${result === 1 ? 'Added' : 'Updated'} character "${name}".`);
message.reply(`${result.new ? 'Added' : 'Updated'} character "${name}".`);
} else {
message.reply(`Unable to update character "${info}". You are not the owner.`);
message.reply(`Unable to update character "${name}". You are not the owner.`);
}
}
};
13 changes: 7 additions & 6 deletions src/commands/characters/delete.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use babel';
'use strict';

import database from '../../database/characters';
import Character from '../../database/character';
import disambiguation from '../../util/disambiguation';
import * as usage from '../../util/command-usage';
import CommandFormatError from '../../util/errors/command-format';

export default {
name: 'deletecharacter',
Expand All @@ -20,12 +21,12 @@ export default {
return !!message.server;
},

run(message, args) {
if(!args[0]) return false;
const characters = database.findCharactersInServer(message.server, args[0]);
async run(message, args) {
if(!args[0]) throw new CommandFormatError(this);
const characters = await Character.findInServer(message.server, args[0]);
if(characters.length === 1) {
if(database.deleteCharacter(characters[0])) {
message.reply(`Deleted character "${characters[0].name}."`);
if(await Character.delete(characters[0])) {
message.reply(`Deleted character "${characters[0].name}".`);
} else {
message.reply(`Unable to delete character "${characters[0].name}". You are not the owner.`);
}
Expand Down
11 changes: 6 additions & 5 deletions src/commands/characters/list.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use babel';
'use strict';

import database from '../../database/characters';
import Character from '../../database/character';
import config from '../../config';
import paginate from '../../util/pagination';
import * as usage from '../../util/command-usage';
Expand All @@ -20,10 +20,11 @@ export default {
return !!message.server;
},

run(message, args) {
const search = args.length >= 2 || isNaN(args[0]) ? args[0] : '';
const page = args.length >= 2 ? parseInt(args[1]) : (!isNaN(args[0]) ? parseInt(args[0]) : 1);
let characters = database.findCharactersInServer(message.server, search, false);
async run(message, args) {
const last = args.length >= 1 ? args.length - 1 : 0;
const page = !isNaN(args[last]) ? parseInt(args.pop()) : 1;
const search = args.join(' ');
let characters = await Character.findInServer(message.server, search, false);
if(characters.length > 0) {
characters.sort((a, b) => a.name < b.name ? -1 : (a.name > b.name ? 1 : 0));
const paginated = paginate(characters, page, Math.floor(config.paginationItems));
Expand Down
9 changes: 5 additions & 4 deletions src/commands/characters/view.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use babel';
'use strict';

import database from '../../database/characters';
import Character from '../../database/character';
import disambiguation from '../../util/disambiguation';
import * as usage from '../../util/command-usage';
import CommandFormatError from '../../util/errors/command-format';

export default {
name: 'character',
Expand All @@ -20,9 +21,9 @@ export default {
return !!message.server;
},

run(message, args) {
if(!args[0]) return false;
const characters = database.findCharactersInServer(message.server, args[0]);
async run(message, args) {
if(!args[0]) throw new CommandFormatError(this);
const characters = await Character.findInServer(message.server, args[0]);
if(characters.length === 1) {
const owner = message.client.users.get('id', characters[0].owner);
const ownerName = owner ? owner.name + '#' + owner.discriminator : 'Unknown';
Expand Down
5 changes: 3 additions & 2 deletions src/commands/dice/max.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import DiceExpression from 'dice-expression-evaluator';
import logger from '../../util/logger';
import CommandFormatError from '../../util/errors/command-format';

export default {
name: 'maxroll',
Expand All @@ -18,8 +19,8 @@ export default {
return true;
},

run(message, args) {
if(!args[0]) return false;
async run(message, args) {
if(!args[0]) throw new CommandFormatError(this);
try {
const maxRoll = new DiceExpression(args[0]).max();
message.reply(`The maximum possible roll is **${maxRoll}**.`);
Expand Down
5 changes: 3 additions & 2 deletions src/commands/dice/min.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import DiceExpression from 'dice-expression-evaluator';
import logger from '../../util/logger';
import CommandFormatError from '../../util/errors/command-format';

export default {
name: 'minroll',
Expand All @@ -18,8 +19,8 @@ export default {
return true;
},

run(message, args) {
if(!args[0]) return false;
async run(message, args) {
if(!args[0]) throw new CommandFormatError(this);
try {
const minRoll = new DiceExpression(args[0]).min();
message.reply(`The minimum possible roll is **${minRoll}**.`);
Expand Down
5 changes: 3 additions & 2 deletions src/commands/dice/roll.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import DiceExpression from 'dice-expression-evaluator';
import nbsp from '../../util/nbsp';
import logger from '../../util/logger';
import CommandFormatError from '../../util/errors/command-format';

const pattern = /^(.+?)(?:(>|<)\s*([0-9]+?))?\s*$/;

Expand All @@ -25,8 +26,8 @@ export default {
return true;
},

run(message, args, fromPattern) {
if(!args[0]) return false;
async run(message, args, fromPattern) {
if(!args[0]) throw new CommandFormatError(this);
try {
const matches = fromPattern ? args : pattern.exec(args[0]);
const dice = new DiceExpression(matches[1]);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/general/about.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {
return true;
},

run(message) {
async run(message) {
const owner = message.client.users.get('id', config.owner);
const servers = message.client.servers.length.toLocaleString(), users = message.client.users.length.toLocaleString();
const serversLabel = servers != 1 ? 'servers' : 'server', usersLabel = users != 1 ? 'users' : 'user';
Expand Down
9 changes: 4 additions & 5 deletions src/commands/general/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import * as commands from '..';
import storage from '../../database/local-storage';
import Character from '../../database/character';
import Setting from '../../database/setting';
import CharDB from '../../database/characters';
import SettingDB from '../../database/settings';
import ModRolesDB from '../../database/mod-roles';
import search from '../../util/search';
import disambiguation from '../../util/disambiguation';
import pagination from '../../util/pagination';
Expand All @@ -26,6 +23,8 @@ import checkForUpdate from '../../util/update-check';
import * as permissions from '../../util/permissions';
import * as usage from '../../util/command-usage';
import * as nbsp from '../../util/nbsp';
import FriendlyError from '../../util/errors/friendly';
import CommandFormatError from '../../util/errors/command-format';
/* eslint-enable no-unused-vars */

export default {
Expand All @@ -41,8 +40,8 @@ export default {
return message.author.id === config.owner;
},

run(msg, args) {
if(!args[0]) return false;
async run(msg, args) {
if(!args[0]) throw new CommandFormatError(this);
try {
msg.reply(`Result: \`${util.inspect(eval(args[0]), {depth: 0})}\``);
} catch(e) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/general/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default {
return true;
},

run(message, args) {
async run(message, args) {
const commands = findCommands(args[0], message);
if(args[0]) {
if(commands.length === 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/general/list-roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {
return message.server && permissions.isAdministrator(message.server, message.author);
},

run(message) {
async run(message) {
const roleList = message.server.roles.map(element => `${element.name} (ID: ${element.id})`).join('\n');
message.reply(`Server roles:\n${roleList}`);
}
Expand Down
11 changes: 5 additions & 6 deletions src/commands/general/prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import config from '../../config';
import buildCommandPattern from '../../util/command-pattern';
import * as permissions from '../../util/permissions';
import * as usage from '../../util/command-usage';
import SettingsDatabase from '../../database/settings';
import Setting from '../../database/setting';

export default {
Expand All @@ -23,7 +22,7 @@ export default {
return true;
},

run(message, args) {
async run(message, args) {
if(args[0] && message.server) {
// Only allow administrators
if(!permissions.isAdministrator(message.server, message.author)) {
Expand All @@ -36,10 +35,10 @@ export default {
const prefix = lowercase === 'none' ? '' : args[0];
let response;
if(lowercase === 'default') {
SettingsDatabase.deleteSetting('command-prefix', message.server);
Setting.delete('command-prefix', message.server);
response = `Reset the command prefix to default (currently "${config.commandPrefix}").`;
} else {
SettingsDatabase.saveSetting(new Setting(message.server, 'command-prefix', prefix));
Setting.save(new Setting(message.server, 'command-prefix', prefix));
response = prefix ? `Set the command prefix to "${args[0]}".` : 'Removed the command prefix entirely.';
}

Expand All @@ -49,8 +48,8 @@ export default {

message.reply(`${response} To run commands, use ${usage.long('command', message.server)}.`);
} else {
const prefix = message.server ? SettingsDatabase.getSettingValue('command-prefix', config.commandPrefix, message.server) : config.commandPrefix;
message.reply(`${prefix ? `The command prefix is "${prefix}".` : 'There is no command prefix.'} To run commands, use ${usage.long('command', message.server)}`);
const prefix = message.server ? Setting.getValue('command-prefix', config.commandPrefix, message.server) : config.commandPrefix;
message.reply(`${prefix ? `The command prefix is "${prefix}".` : 'There is no command prefix.'} To run commands, use ${usage.long('command', message.server)}.`);
}
}
};
9 changes: 5 additions & 4 deletions src/commands/mod-roles/add.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use babel';
'use strict';

import database from '../../database/mod-roles';
import ModRole from '../../database/mod-role';
import search from '../../util/search';
import disambiguation from '../../util/disambiguation';
import * as usage from '../../util/command-usage';
import * as permissions from '../../util/permissions';
import CommandFormatError from '../../util/errors/command-format';

const pattern = /^(?:<@&)?(.+?)>?$/;

Expand All @@ -24,15 +25,15 @@ export default {
return message.server && permissions.isAdministrator(message.server, message.author);
},

run(message, args) {
if(!args[0]) return false;
async run(message, args) {
if(!args[0]) throw new CommandFormatError(this);
const matches = pattern.exec(args[0]);
let roles;
const idRole = message.server.roles.get('id', matches[1]);
if(idRole) roles = [idRole]; else roles = search(message.server.roles, matches[1]);

if(roles.length === 1) {
if(database.saveRole(roles[0])) {
if(ModRole.save(roles[0])) {
message.reply(`Added "${roles[0].name}" to the moderator roles.`);
} else {
message.reply(`Unable to add "${roles[0].name}" to the moderator roles. It already is one.`);
Expand Down
11 changes: 6 additions & 5 deletions src/commands/mod-roles/delete.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use babel';
'use strict';

import database from '../../database/mod-roles';
import ModRole from '../../database/mod-role';
import disambiguation from '../../util/disambiguation';
import * as usage from '../../util/command-usage';
import * as permissions from '../../util/permissions';
import CommandFormatError from '../../util/errors/command-format';

const pattern = /^(?:<@&)?(.+?)>?$/;

Expand All @@ -23,15 +24,15 @@ export default {
return message.server && permissions.isAdministrator(message.server, message.author);
},

run(message, args) {
if(!args[0]) return false;
async run(message, args) {
if(!args[0]) throw new CommandFormatError(this);
const matches = pattern.exec(args[0]);
let roles;
const idRole = message.server.roles.get('id', matches[1]);
if(idRole) roles = [idRole]; else roles = database.findRolesInServer(message.server, matches[1]);
if(idRole) roles = [idRole]; else roles = ModRole.findInServer(message.server, matches[1]);

if(roles.length === 1) {
if(database.deleteRole(roles[0])) {
if(ModRole.delete(roles[0])) {
message.reply(`Removed "${roles[0].name}" from the moderator roles.`);
} else {
message.reply(`Unable to remove "${roles[0].name}" from the moderator roles. It isn\'t one.`);
Expand Down
Loading

0 comments on commit 8eaf8dd

Please sign in to comment.