From 0f94428d2461ea5b53fe9a81e1ed906faf63241c Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Sat, 16 Jul 2016 03:17:14 -0400 Subject: [PATCH] Initial commit --- .babelrc | 3 +++ .eslintrc.json | 22 +++++++++++++++++ .gitignore | 4 ++++ LICENSE | 21 ++++++++++++++++ README.md | 3 +++ package.json | 34 ++++++++++++++++++++++++++ settings.example.yaml | 2 ++ src/commands/dice-roll.js | 31 ++++++++++++++++++++++++ src/commands/test.js | 14 +++++++++++ src/main.js | 50 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 184 insertions(+) create mode 100644 .babelrc create mode 100644 .eslintrc.json create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 package.json create mode 100644 settings.example.yaml create mode 100644 src/commands/dice-roll.js create mode 100644 src/commands/test.js create mode 100644 src/main.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..7b37dd5 --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["es2015"] +} diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..b4bb789 --- /dev/null +++ b/.eslintrc.json @@ -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" + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3dca555 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/node_modules +npm-debug.log +/lib +settings.yaml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5dbeed5 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..de01753 --- /dev/null +++ b/README.md @@ -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. diff --git a/package.json b/package.json new file mode 100644 index 0000000..f9e1c99 --- /dev/null +++ b/package.json @@ -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 (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" + } +} diff --git a/settings.example.yaml b/settings.example.yaml new file mode 100644 index 0000000..cc61b22 --- /dev/null +++ b/settings.example.yaml @@ -0,0 +1,2 @@ +email: SomeGuy@SomeSite.com +password: SomeCrazyPassword123 diff --git a/src/commands/dice-roll.js b/src/commands/dice-roll.js new file mode 100644 index 0000000..9756b2c --- /dev/null +++ b/src/commands/dice-roll.js @@ -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; + } + } +} diff --git a/src/commands/test.js b/src/commands/test.js new file mode 100644 index 0000000..8d75bd7 --- /dev/null +++ b/src/commands/test.js @@ -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!'); + } +} diff --git a/src/main.js b/src/main.js new file mode 100644 index 0000000..7d80fc3 --- /dev/null +++ b/src/main.js @@ -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);