-
Notifications
You must be signed in to change notification settings - Fork 3
/
TwitchClient.ts
66 lines (58 loc) · 1.87 KB
/
TwitchClient.ts
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
import * as TwitchIrc from "https://deno.land/x/[email protected]/mod.ts";
import { Privmsg } from "https://deno.land/x/[email protected]/lib/message/privmsg.ts";
import EventEmitter from "https://deno.land/x/[email protected]/mod.ts";
export class TwitchClient extends EventEmitter<{
chat: (message: string, user: string, raw: Privmsg) => void;
bits: (bits: number, message: string, user: string, raw: Privmsg) => void;
redeem: (reward: string, message: string, user: string, raw: Privmsg) => void;
raw: (raw: Privmsg) => void;
}> {
public ircClient = new TwitchIrc.Client();
public channel: string;
public debug = false;
constructor({ channel, debug }: { channel: string; debug?: boolean }) {
super();
if (debug) {
this.debug = debug;
}
this.channel = channel;
this.ircClient.on("privmsg", (e) => this.handleMessage(e));
}
async connect() {
await new Promise<void>((resolve) => {
this.ircClient.on("open", async () => {
await this.ircClient.join(`#${this.channel}`);
this.log(`Connected to chat for ${this.channel}`);
resolve();
});
});
}
handleMessage(event: Privmsg) {
this.emit("raw", event);
if (this.debug) this.log("Raw:", event.raw);
if (event.raw.tags?.customRewardId) {
this.log("Redeem Used:", event.raw.tags?.customRewardId);
this.emit(
"redeem",
event.raw.tags?.customRewardId,
event.message,
event.user.displayName!,
event,
);
} else if (event.raw.tags?.bits) {
this.emit(
"bits",
parseInt(event.raw.tags!.bits, 10),
event.message,
event.user.displayName!,
event,
);
} else {
this.emit("chat", event.message, event.user.displayName!, event);
}
}
// deno-lint-ignore no-explicit-any
log(...data: any[]) {
console.log("[TwitchClient]:", ...data);
}
}