Skip to content
This repository has been archived by the owner on Nov 11, 2020. It is now read-only.

Commit

Permalink
First commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
AKPWebDesign committed May 10, 2016
0 parents commit 8eed071
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Logs
!logs
logs/*.log
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# 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 directory
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# VS Code
.vscode

config.json
certs
*.crt
*.key
42 changes: 42 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Logs
!logs
logs/*.log
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# 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 directory
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# VS Code
.vscode

config.json
certs
*.crt
*.key
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
This plugin allows AKP48Squared to connect to Twitch chat servers.

# Installation

This plugin is included by default on new installations of AKP48Squared. No further installation is needed.

# Config

You'll need to add a new server to your configuration file for each Twitch bot you want to run. An example is shown below.

```
"servers": [
{
"plugin": "twitch",
"config": {
"connection": {
"reconnect": true // should we reconnect automatically?
},
"identity": {
"username": "twitch_bot", // See https://help.twitch.tv/customer/portal/articles/1302780-twitch-irc for more info.
"password": "oauth:we2sm49se21ln6tfkxzvbco7z8uv1s"
},
"channels": [
"#mytwitchchannel"
]
}
}
]
```

# Issues

If you come across any issues, you can report them on this GitHub repo [here](https://github.com/AKP48Squared/akp48-plugin-discord-server/issues).
160 changes: 160 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
'use strict';
const ServerConnectorPlugin = require('../../lib/ServerConnectorPlugin');
const irc = require('tmi.js');

class Twitch extends ServerConnectorPlugin {
constructor(config, id, AKP48, persistentObjects) {
super('Twitch', AKP48);
this._id = id;
this._config = config;
this._defaultCommandDelimiters = ['!', '.'];
var self = this;
if(!config || !config.channels) {
global.logger.error(`${self._pluginName}|${self._id}: Required channels option missing from config!`);
this._error = true;
return;
}

if(!config.identity || !config.identity.username || !config.identity.password) {
global.logger.warn(`${self._pluginName}|${self._id}: No complete identity found in config! Are you sure this is what you want?`);
}

if(persistentObjects) {
this._client = persistentObjects.client;
this._client.removeAllListeners('chat');
this._client.removeAllListeners('whisper');
this._client.removeAllListeners('connecting');
this._client.removeAllListeners('connected');
this._client.removeAllListeners('disconnected');
this._connected = true;
} else {
this._client = new irc.client(config);
}

this._client.on('chat', function(to, user, text, sentFromSelf) {
if(to === self._client.getUsername()) { to = user.username; }
if(!sentFromSelf) {
self._AKP48.onMessage(text, self.createContexts(to, user, text));
}
});

this._client.on('whisper', function(user, text) {
var to = user.username;
self._AKP48.onMessage(text, self.createContexts(to, user, text, true));
});

this._client.on('connecting', function(address, port) {
global.logger.verbose(`${self._pluginName}|${self._id}: Connecting to ${address}:${port}.`);
});

this._client.on('connected', function(address, port) {
global.logger.debug(`${self._pluginName}|${self._id}: Connected to ${address}:${port}.`);
self._AKP48.emit('serverConnect', self._id, self);
});

this._client.on('disconnected', function(reason) {
global.logger.debug(`${self._pluginName}|${self._id}: Disconnected from server for "${reason}".`);
});

this._client.on('error', function(message) {
global.logger.error(`${self._pluginName}|${self._id}: Error received from ${message.server}! ${message.command}: ${message.args}`);
});

this._AKP48.on('msg_'+this._id, function(to, message, context) {
if(!context.noPrefix) {message = `@${context.nick}: ${message}`;}
if(context.isWhisper) {
self._client.whisper(to, message);
} else {
self._client.say(to, message);
}
self._AKP48.sentMessage(to, message, context);
});

this._AKP48.on('emote_'+this._id, function(to, message, context) {
self._client.action(to, message);
self._AKP48.sentMessage(to, message, context);
});
}

connect() {
if(this._error) {
global.logger.error(`${this._pluginName}|${this._id}: Cannot connect. Check log for errors.`);
return;
}
if(this._connected) {
global.logger.debug(`${this._pluginName}|${this._id}: Using previous connection.`);
this._connected = false;
} else {
this._client.connect();
}
}

disconnect(msg) {
if(this._error) {
global.logger.error(`${this._pluginName}|${this._id}: Cannot disconnect. Check log for errors.`);
return;
}
this._client.disconnect(msg || 'Goodbye.');
}
}

Twitch.prototype.createContexts = function (to, user, text, isWhisper) {
var textArray = text.split(/[^\\]\|/);
var ctxs = [];

for (var i = 0; i < textArray.length; i++) {
textArray[i] = textArray[i].trim();
var delimiterLength = this.isTextACommand(textArray[i], to);
if(delimiterLength) {
textArray[i] = textArray[i].slice(delimiterLength).trim();
}

var ctx = {
rawMessage: text,
nick: user['display-name'],
user: user.username,
rawText: text,
text: textArray[i].trim(),
to: to,
myNick: this._client.getUsername(),
instanceId: this._id,
instanceType: 'twitch',
instance: this,
isCmd: delimiterLength ? true : false
};

if(isWhisper) {
ctx.isWhisper = true;
}

ctxs.push(ctx);
}

ctxs[ctxs.length-1].last = true;

return ctxs;
};

Twitch.prototype.getChannelConfig = function (channel) {
if(!this._config.chanConfig) {return {};}
return this._config.chanConfig[channel] || {};
};

Twitch.prototype.isTextACommand = function (text, channel) {
var delimit = this.getChannelConfig(channel).commandDelimiters || this._config.commandDelimiters || this._defaultCommandDelimiters;
for (var i = 0; i < delimit.length; i++) {
if(text.toLowerCase().startsWith(delimit[i].toLowerCase())) {
return delimit[i].length;
}
}

return false;
};

Twitch.prototype.getPersistentObjects = function () {
return {
client: this._client
};
};

module.exports = Twitch;
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "akp48-plugin-twitch",
"version": "1.0.0",
"description": "Twitch plugin for AKP48.",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"preversion": "git checkout master && git pull && npm ls",
"publish-patch": "npm run preversion && npm version patch && git push origin master --tags && npm publish",
"publish-minor": "npm run preversion && npm version minor && git push origin master --tags && npm publish",
"publish-major": "npm run preversion && npm version major && git push origin master --tags && npm publish"
},
"author": "Austin Peterson <[email protected]> (http://akpwebdesign.com)",
"contributors": [
"Alan H (Feildmaster) <[email protected]> (http://feildmaster.com/)",
"Austin Peterson <[email protected]> (http://akpwebdesign.com/)"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/AKP48Squared/twitch-server.git"
},
"bugs": {
"url": "https://github.com/AKP48Squared/twitch-server/issues"
},
"homepage": "https://github.com/AKP48Squared/twitch-server",
"dependencies": {
"tmi.js": "0.0.29"
}
}
6 changes: 6 additions & 0 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "twitch",
"type": "ServerConnector",
"description": "Used to connect AKP48 to Twitch IRC.",
"main": "main.js"
}

0 comments on commit 8eed071

Please sign in to comment.