git subrepo clone git@github.com:benji7425/shell-discord-bot.git wrapper

subrepo:
  subdir:   "wrapper"
  merged:   "4946884"
upstream:
  origin:   "git@github.com:benji7425/shell-discord-bot.git"
  branch:   "master"
  commit:   "4946884"
git-subrepo:
  version:  "0.3.1"
  origin:   "???"
  commit:   "???"
This commit is contained in:
benji7425 2017-04-22 22:41:44 +01:00
parent 45b6d080a8
commit 5f64fac0a5
7 changed files with 257 additions and 0 deletions

30
wrapper/.eslintrc.json Normal file
View File

@ -0,0 +1,30 @@
{
"env": {
"browser": true,
"node": true,
"commonjs": true,
"es6": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"indent": [
"warn",
"tab",
{ "SwitchCase": 1}
],
"quotes": [
"warn",
"double"
],
"semi": [
"error",
"always"
],
"no-undef": "error",
"no-unused-vars": "warn",
"eqeqeq": ["error", "always"]
}
}

59
wrapper/.gitignore vendored Normal file
View File

@ -0,0 +1,59 @@
# Project specific
token.json
# Created by https://www.gitignore.io/api/node
### Node ###
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules
jspm_packages
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# End of https://www.gitignore.io/api/node
# Project-specific cache for use with shrinkpack - https://github.com/JamieMason/shrinkpack

11
wrapper/.gitrepo Normal file
View File

@ -0,0 +1,11 @@
; DO NOT EDIT (unless you know what you are doing)
;
; This subdirectory is a git "subrepo", and this file is maintained by the
; git-subrepo command. See https://github.com/git-commands/git-subrepo#readme
;
[subrepo]
remote = git@github.com:benji7425/shell-discord-bot.git
branch = master
commit = 4946884723f9c66d3c2713f109aab2cf60bd81f4
parent = 258eff2d63b5804e1e423e9418b117ad4212d8ff
cmdver = 0.3.1

3
wrapper/.npmrc Normal file
View File

@ -0,0 +1,3 @@
save=true
sace-exact=true
cache=node_cache

83
wrapper/README.md Normal file
View File

@ -0,0 +1,83 @@
# Shell Discord bot
The purpose of this is to act as a shell for other bot modules, so that a single bot user account can be used for a multi-function bot.
## Setup
- Fork/clone/merge this repo into a new one
- `npm install --save discord.io`
- `npm shrinkwrap --dev`
- `shrinkpack .`
- Create *token.json* with your discord token: `{ "token": "1234567890" }`
## Creating a bot module
Interfacing with each bot module is done by the properties in its module.exports. Available properties are:
| property | property type | parameters | description |
|--------------|---------------|---------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
| onReady | method | none | called on the bot ready event |
| onDisconnect | method | none | called when the bot disconnects |
| onMessage | method | bot, user, userID, channelID, message | called when the bot receives a message - identical to the 'action' property of a command, but triggered on every message (see below) |
| commands | array | N/A | commands a user can invoke - like the onMessage event, but only triggered on expected commands (see below) |
### Commands
A command object contains a command, and an action to invoke if the message contains that command. Each command object needs a certain set of properties:
| property | optional? | value | description |
|------------|-----------|--------------------------|----------------------------------------------------------------------------------------------------|
| command | required | string | Command to look for in the message |
| type | required | "equals" or "startsWith" | Describes whether we are looking for the message to be the exact command, or just to start with it |
| action | required | function | Callback to invoke if the command is matched (see below) |
| channelIDs | optional | array of strings | If this property is present, the command will only be triggered if sent in one of these channels |
| userIDs | optional | array of strings | If this property is present, the command will only be triggered if made by one of these users |
#### Actions
An action is a callback function called if the specified command is found. It must take these parameters:
| parameter | type | description |
|-----------|--------|----------------------------------------------------------------------|
| bot | object | The [discord.io](https://github.com/izy521/discord.io) client object |
| user | string | Username of the user who sent the message |
| userID | string | User ID of the user who sent the message |
| channelID | string | Channel ID of the channel the message was sent in |
| message | string | The message/command that was sent |
Example 1:
```JavaScript
{
command: "ping",
type: "equals",
action: (bot, user, userID, channelID, message) => {
bot.sendMessage({
to: channelID,
message: "pong"
})
},
userIDs: ["1234567890"]
}
```
The above example will only call *action* if the user with ID 1234567890 sends "ping"
Example 2:
```JavaScript
{
command: "!define",
type: "startsWith",
action: (bot, user, userID, channelID, message) => {
bot.sendMessage({
to: channelID,
message: getDefinition(message.split(" ")[1])
})
}
}
```
The above example expects the user to type '!define something', ie only checking for the message to start with '!define'. You are still passed the full message, so can split it up and read it however you want.
*action* will only be called if the message begins with '!define', but has no restrictions on which channel(s) or user(s) use it

67
wrapper/index.js Normal file
View File

@ -0,0 +1,67 @@
//node imports
const Console = require("console");
//external module imports
var Discord = require("discord.io");
var BotModules = [];
var bot;
var EventHandlers = {
onReady: () => {
Console.info("Registered bot " + bot.username + " with id " + bot.id);
for (let i = 0, len = BotModules.length; i < len; i++) {
let botModule = BotModules[i];
if (botModule.onReady) botModule.onReady();
}
},
onDisconnect: (err, code) => {
Console.error("Bot was disconnected!", err, code);
for (let i = 0, len = BotModules.length; i < len; i++) {
let botModule = BotModules[i];
if (botModule.onReady) botModule.onDisconnect();
}
bot.connect();
},
onMessage: (user, userID, channelID, message) => {
for (let i = 0, iLen = BotModules.length; i < iLen; i++) {
let botModule = BotModules[i];
if (botModule.commands) {
for (let j = 0, jLen = botModule.commands.length; j < jLen; j++) {
let messageTrigger = botModule.commands[j];
if ((!messageTrigger.channelIDs && !messageTrigger.userIDs) //if we have neither channel nor user restraint, pass
|| (messageTrigger.channelIDs && messageTrigger.channelIDs.includes(channelID)) //otherwise, if we have a channel constraint, pass if we're allowed to respond in this channel
|| (messageTrigger.userIDs && messageTrigger.userIDs.includes(userID))) //otherwise, if we have a user constraint, pass if we're allowed to respond to this user
switch (messageTrigger.type) {
case "startsWith":
if (message.startsWith(messageTrigger.command))
messageTrigger.action(bot, user, userID, channelID, message);
break;
default:
if (message === messageTrigger.command)
messageTrigger.action(bot, user, userID, channelID, message);
}
}
}
if (botModule.onMessage) botModule.onMessage(bot, user, userID, channelID, message);
}
}
};
(() => {
bot = new Discord.Client({
token: require("./token.json").token,
autorun: true
});
bot.on("ready", EventHandlers.onReady);
bot.on("disconnect", EventHandlers.onDisconnect);
bot.on("message", EventHandlers.onMessage);
})();

4
wrapper/npm-shrinkwrap.json generated Normal file
View File

@ -0,0 +1,4 @@
{
"name": "node-boilerplate",
"version": "1.0.0"
}