Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gawdl3y committed Jul 16, 2016
0 parents commit 0f94428
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
22 changes: 22 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"parser": "babel-eslint",
"env": {
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"rules": {
"indent": ["error", "tab", { "SwitchCase": 1 }],
"keyword-spacing": ["error", {
"overrides": {
"if": { "after": false },
"for": { "after": false },
"while": { "after": false },
"catch": { "after": false },
"switch": { "after": false }
}
}],
"no-else-return": "off",
"no-console": "off"
}
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/node_modules
npm-debug.log
/lib
settings.yaml
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Schuyler Cebulskie

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Discord RPBot
This is a simple bot that currently only contains one useful command: !roll.
More information will hopefully come at some point.
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "discord-rpbot",
"version": "0.1.0",
"description": "A Discord bot that contains commands useful for text roleplaying",
"license": "MIT",
"author": "Schuyler Cebulskie <[email protected]> (https://gawdl3y.com/)",
"repository": {
"type": "git",
"url": "https://github.com/Gawdl3y/discord-rpbot.git"
},
"bugs": {
"url": "https://github.com/Gawdl3y/discord-rpbot/issues"
},
"keywords": [
"discord",
"bot",
"roleplaying",
"dice"
],
"scripts": {
"build": "babel src -d lib"
},
"dependencies": {
"discord.js": "^8.0.0",
"js-yaml": "^3.6.1",
"dice-expression-evaluator": "^0.1.2"
},
"devDependencies": {
"eslint": "^3.0.0",
"babel-eslint": "^6.0.4",
"babel-cli": "^6.0.0",
"babel-preset-es2015": "^6.9.0"
}
}
2 changes: 2 additions & 0 deletions settings.example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
email: [email protected]
password: SomeCrazyPassword123
31 changes: 31 additions & 0 deletions src/commands/dice-roll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use babel';
'use strict';

import DiceExpression from 'dice-expression-evaluator';

export default class DiceRollCommand {
constructor() {
this.matches = [
/^!roll\s+([0-9d+ -]+)(>\s*([0-9]+))?\s*$/i,
/\(\s*roll:\s*([0-9d+ -]+)(>\s*([0-9]+))?\s*\)/i
];
}

run(message, matchResult) {
try {
const dice = new DiceExpression(matchResult[1]);
const result = dice.roll();
console.log(result);
if(matchResult.length >= 4 && matchResult[3]) {
const target = parseInt(matchResult[3]);
message.client.sendMessage(message, message.author + (target <= result.roll ? ' has succeeded. ' : ' has failed. ') + ' (Rolled ' + result.roll + ', target ' + target + ')');
} else {
message.client.sendMessage(message, message.author + ' rolled ' + result.roll + '.');
}
} catch(e) {
console.log(e);
message.client.sendMessage(message, message.author + ' specified an invalid dice expression.');
return;
}
}
}
14 changes: 14 additions & 0 deletions src/commands/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use babel';
'use strict';

export default class TestCommand {
constructor() {
this.matches = [
/^!test(\s.*)?$/i
];
}

run(message) {
message.client.sendMessage(message, 'Oh boy, the test command is working!');
}
}
50 changes: 50 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use babel';
'use strict';

import Discord from 'discord.js';
import YAML from 'js-yaml';
import fs from 'fs';

// Commands
import TestCommand from './commands/test';
import DiceRollCommand from './commands/dice-roll';
const commands = [
new TestCommand(),
new DiceRollCommand()
];

// Parse config
const config = YAML.safeLoad(fs.readFileSync('settings.yaml'));

// Create client
const bot = new Discord.Client();
bot.on('ready', () => {
console.log('Bot is ready; logged in as ' + bot.user.username + '#' + bot.user.discriminator + ' (ID ' + bot.user.id + ')');
});
bot.on('error', e => {
console.log('ERROR: ' + e);
});
bot.on('warn', e => {
console.log('WARNING: ' + e);
});
bot.on('disconnected', () => {
console.log('Disconnected!');
process.exit(1);
})

// Set up commands
bot.on('message', message => {
commandLoop: for(const command of commands) {
for(const match of command.matches) {
const matchResult = match.exec(message.content);
if(matchResult) {
console.log('Running ' + command.constructor.name + ': message="' + message + '" matchResult="' + matchResult + '"');
command.run(message, matchResult);
break commandLoop;
}
}
}
});

// Log in
bot.login(config.email, config.password);

0 comments on commit 0f94428

Please sign in to comment.