This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
159 lines (134 loc) · 4.87 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict';
const irc = require('tmi.js');
class Twitch extends global.AKP48.pluginTypes.ServerConnector {
constructor(AKP48, config, id, persistentObjects) {
super('Twitch', AKP48, config, id);
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;
module.exports.type = 'ServerConnector';
module.exports.pluginName = 'twitch';