Skip to content

Commit

Permalink
General refactor, new Service API, general messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
martindale committed Mar 27, 2018
1 parent 20ec468 commit ad90f22
Show file tree
Hide file tree
Showing 18 changed files with 437 additions and 305 deletions.
98 changes: 22 additions & 76 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ projects and events.

## Quick Start
1. Fork (optional) and clone:
`git clone https://github.com/FabricLabs/doorman.git`
`git clone https://github.com/FabricLabs/doorman.git`
2. Run `cp config/index.json.example config/index.json` & edit to your liking
2. Run `cp config/auth.json.example config/auth.json` & add your [auth tokens](#auth)
3. Run `npm install`
Expand Down Expand Up @@ -57,8 +57,8 @@ in `config/index.json` (without the `doorman-` prefix):

```js
{
// slice of larger JSON file
"plugins": ["catfacts", "misc", "wikipedia", "urbandictionary"]
// slice of larger JSON file
"plugins": ["catfacts", "misc", "wikipedia", "urbandictionary"]
}
```

Expand All @@ -67,13 +67,13 @@ file:

```js
{
// slice of larger JSON file
"dependencies": {
"doorman-urbandictionary": "FabricLabs/doorman-urbandictionary",
"doorman-wikipedia": "FabricLabs/doorman-wikipedia",
"doorman-misc": "FabricLabs/doorman-misc",
"doorman-catfact": "FabricLabs/doorman-catfact"
},
// slice of larger JSON file
"dependencies": {
"doorman-urbandictionary": "FabricLabs/doorman-urbandictionary",
"doorman-wikipedia": "FabricLabs/doorman-wikipedia",
"doorman-misc": "FabricLabs/doorman-misc",
"doorman-catfact": "FabricLabs/doorman-catfact"
},
}
```

Expand Down Expand Up @@ -104,85 +104,31 @@ List of external plugins for you to include with your installation (if you wish)
- [datefact](https://github.com/FabricLabs/doorman-datefact) => spits out a random date fact
- [remaeusfact](https://github.com/FabricLabs/doorman-remaeusfact) => spits out a random fact about [Remaeus](https://github.com/martindale)

### Writing Plugin
### Writing Plugins
To write a Doorman plugin, create a new NPM module that exports an array named `commands` of triggers your bot will respond to. You can use a simple callback to display your message in both Slack and Discord, depending on the features you added:

```js
module.exports = (Doorman) => {
return {
commands: [
'hello'
],
hello: {
description: 'responds with hello!',
process: (msg, suffix, isEdit, cb) => { cb('hello!', msg); }
}
};
return {
commands: [
'hello'
],
hello: {
description: 'responds with hello!',
process: (msg, suffix, isEdit, cb) => { cb('hello!', msg); }
}
};
};
```

If you think your plugin is amazing, please let us know! We'd love to add it to our list. Currently, the bot is configured to work with external repositories with the `doorman-` prefix.

## Installation

Written in Node.JS.

0. Install prereqs, see below for your OS.
1. Clone the repo.
2. Run `npm install` in the repo directory.

For music playback (on Discord), you will need [ffmpeg](https://www.ffmpeg.org/download.html) installed and in your path variables.

### Prereqs:

#### On Unix

* `python` (`v2.7` recommended, `v3.x.x` is __*not*__ supported)
* `make`
* A proper C/C++ compiler toolchain, like [GCC](https://gcc.gnu.org)
* `npm install -g node-gyp`

#### On Mac OS X

* `python` (`v2.7` recommended, `v3.x.x` is __*not*__ supported) (already installed on Mac OS X)
* [Xcode](https://developer.apple.com/xcode/download/)
* You also need to install the `Command Line Tools` via Xcode. You can find this under the menu `Xcode -> Preferences -> Downloads`
* This step will install `gcc` and the related toolchain containing `make`
* `npm install -g node-gyp`

#### On Windows

##### Option 1

Install all the required tools and configurations using Microsoft's [windows-build-tools](https://github.com/felixrieseberg/windows-build-tools) using `npm install --global --production windows-build-tools` from an elevated PowerShell or CMD.exe (run as Administrator).

##### Option 2

Install tools and configuration manually:
* Visual C++ Build Environment:
* Option 1: Install [Visual C++ Build Tools](http://landinghub.visualstudio.com/visual-cpp-build-tools) using the **Default Install** option.

* Option 2: Install [Visual Studio 2015](https://www.visualstudio.com/products/visual-studio-community-vs) (or modify an existing installation) and select *Common Tools for Visual C++* during setup. This also works with the free Community and Express for Desktop editions.

> :bulb: [Windows Vista / 7 only] requires [.NET Framework 4.5.1](http://www.microsoft.com/en-us/download/details.aspx?id=40773)
* Install [Python 2.7](https://www.python.org/downloads/) (`v3.x.x` is not supported), and run `npm config set python python2.7` (or see below for further instructions on specifying the proper Python version and path.)
* Launch cmd, `npm config set msvs_version 2015`

If the above steps didn't work for you, please visit [Microsoft's Node.js Guidelines for Windows](https://github.com/Microsoft/nodejs-guidelines/blob/master/windows-environment.md#compiling-native-addon-modules) for additional tips.

Then, install node-gyp using `npm install -g node-gyp`

### Customization
The `/examples/` directory contains example files for the configs.! These files need to be renamed, without the .example extension, and placed in the `/config/` folder.

## Running
Before first run you will need to create an `auth.json` file. A bot token is required for the bot to connect to the different services. The other credentials are not required for the bot to run, but highly recommended as commands that depend on them will malfunction. See `auth.json.example`.

To start the bot just run
`node start`.
To start the bot, just run `npm start`.

## Updates
If you update the bot, please run `npm update` before starting it again. If you have
issues with this, you can try deleting your node_modules folder and then running
`npm install` again. Please see [Installation](#Installation).
`npm install` again.
31 changes: 0 additions & 31 deletions config/index.js

This file was deleted.

55 changes: 7 additions & 48 deletions doorman.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,11 @@
'use strict';

const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const config = require('./config');
const Doorman = require('./lib/doorman');

console.log(chalk.green(`Starting Doorman...`));
console.log(chalk.green(`Node version: ${process.version}`));
function main () {
let doorman = new Doorman(config);
doorman.start();
}

// Helpers
exports.require = function (filePath) {
delete require.cache[path.join(path.dirname(require.main.filename), filePath)];
return require(path.join(path.dirname(require.main.filename), filePath))(this);
};

exports.getFileContents = function (filePath) {
try {
return fs.readFileSync(path.join(path.dirname(require.main.filename), filePath), 'utf-8');
} catch (err) {
return '';
}
};

exports.getFileArray = function (srcPath) {
try {
srcPath = path.join(path.dirname(require.main.filename), srcPath);
return fs.readdirSync(srcPath).filter(file => fs.statSync(path.join(srcPath, file)).isFile());
} catch (err) {
return [];
}
};

exports.getJsonObject = function (filePath) {
return JSON.parse(exports.getFileContents(filePath));
};

exports.resolveMention = function (usertxt) {
let userid = usertxt;
if (usertxt.startsWith('<@!')) {
userid = usertxt.substr(3, usertxt.length - 4);
} else if (usertxt.startsWith('<@')) {
userid = usertxt.substr(2, usertxt.length - 3);
}
return userid;
};

exports.config = require('./config');
exports.auth = require('./config/auth');

// Now for the good stuff!
require('./lib/maki')(this);
require('./lib/doorman')(this);
main();
20 changes: 14 additions & 6 deletions lib/discord/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
'use strict';

const path = require('path');
const util = require('util');
const chalk = require('chalk');
const commandDirectory = './plugins';

module.exports = doorman => {
const commandFiles = doorman.getFileArray(commandDirectory);
const Discord = require('discord.js');
doorman.Discord = new Discord.Client();
const DiscordJS = require('discord.js');
const Plugin = require('../plugin');

const helpers = require('../helpers');
const commandFiles = helpers.getFileArray('./plugins');

function Discord (config) {
doorman.discord = new DiscordJS.Client();

console.log(chalk.magenta(`Discord Enabled... Starting.\nDiscord.js version: ${Discord.version}`));
if (doorman.auth.discord && doorman.auth.discord.bot_token) {
console.log('Logging in to Discord...');
doorman.Discord.login(doorman.auth.discord.bot_token);
doorman.discord.login(doorman.auth.discord.bot_token);

// TODO: make these configurable...
require('./onEvent/disconnected')(doorman);
Expand Down Expand Up @@ -52,3 +56,7 @@ module.exports = doorman => {
console.log(`Loaded ${doorman.commandCount()} base commands`);
console.log(`Loaded ${Object.keys(doorman.discordCommands).length} Discord commands`);
};

util.inherits(Discord, Plugin);

module.exports = Discord;
13 changes: 13 additions & 0 deletions lib/disk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const fs = require('fs');

function Disk () {
this.type = 'Disk';
}

Disk.prototype.exists = function (path) {
return fs.existsSync(path);
};

module.exports = Disk;
Loading

0 comments on commit ad90f22

Please sign in to comment.