-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmi.js
More file actions
149 lines (133 loc) · 4.5 KB
/
tmi.js
File metadata and controls
149 lines (133 loc) · 4.5 KB
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
const TwitchJs = require("twitch-js"),
Commands = require("./commands"),
Exception = require("./exception"),
Log = require("./log"),
settings = require("./settings"),
Warning = require("./warning"),
messageParse = /^!(?<cmd>[^ ]+)(?: +(?<args>.+[^ ]))? *$/,
tmi = new TwitchJs.Client(settings.tmi);
/**
* @type {Commands}
*/
let commands;
// ##### #
// #
// # ## # ##
// # # # # #
// # # # # #
// # # # # #
// # # # ###
/**
* A class that handles calls to twitch-js.
*/
class Tmi {
// #
// #
// ## ## # # # # ### ### ### ###
// # # # #### #### # # # # # # ##
// # # # # # # # # ## # # # # ##
// ## ## # # # # # # # # ### ###
/**
* Gets the commands object.
* @returns {Commands} The commands object.
*/
static get commands() {
return commands;
}
// # #
// # #
// ### ### ### ### ### # # ###
// ## # # # # # # # # # #
// ## # # ## # # # # # #
// ### ## # # # ## ### ###
// #
/**
* Starts up the connection to tmi.
* @returns {void}
*/
static startup() {
commands = new Commands(Tmi);
tmi.on("connected", () => {
Log.log("Connected to tmi.");
});
tmi.on("disconnected", (ev) => {
Log.exception("Disconnected from tmi...", ev);
});
tmi.on("message", async (channel, userstate, text, self) => {
if (!self && channel === "#roncli") {
await Tmi.message(userstate["display-name"], text);
}
});
}
// #
// #
// ## ## ### ### ## ## ###
// # # # # # # # # ## # #
// # # # # # # # ## # #
// ## ## # # # # ## ## ##
/**
* Connects to tmi.
* @returns {Promise} A promise that resolves when tmi is connected.
*/
static async connect() {
Log.log("Connecting to tmi...");
try {
await tmi.connect();
} catch (err) {
Log.exception("tmi connection failed.", err);
}
}
// # # ## ### ### ### ### ##
// #### # ## ## ## # # # # # ##
// # # ## ## ## # ## ## ##
// # # ## ### ### # # # ##
// ###
/**
* Parses a message.
* @param {string} user The user who sent the message.
* @param {string} text The text of the message.
* @returns {Promise} A promise that resolves when the message is parsed.
*/
static async message(user, text) {
if (messageParse.test(text)) {
const {groups: {cmd, args}} = messageParse.exec(text),
command = cmd.toLocaleLowerCase();
if (Object.getOwnPropertyNames(Commands.prototype).filter((p) => typeof Commands.prototype[p] === "function" && p !== "constructor").indexOf(command) !== -1) {
let success;
try {
await Tmi.commands[command](user, args);
} catch (err) {
if (err instanceof Warning) {
Log.warning(`${user}: ${text}\n${err}`);
} else if (err instanceof Exception) {
Log.exception(err.message, err.innerError);
} else {
Log.exception("Unhandled error found.", err);
}
return;
}
if (success) {
Log.log(`${user}: ${text}`);
}
}
}
}
// ### # # ## # # ##
// # # # # # ## # # # ##
// # # # # ## # # ##
// ### ### ## ### ##
// #
/**
* Queues a message to be sent.
* @param {string} message The message to be sent.
* @returns {Promise} A promise that resolves when the message is sent.
*/
static async queue(message) {
try {
await tmi.say("sixgaminggg", message);
} catch (err) {
Log.exception("Say command failed.", err);
}
}
}
module.exports = Tmi;