forked from robotty/dank-twitch-irc
-
Notifications
You must be signed in to change notification settings - Fork 5
/
hosttarget.ts
112 lines (96 loc) · 3.04 KB
/
hosttarget.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
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
import { reasonForValue } from "../../utils/reason-for-value";
import { ChannelIRCMessage } from "../irc/channel-irc-message";
import { IRCMessageData, requireParameter } from "../irc/irc-message";
import { ParseError } from "../parser/parse-error";
export function parseHostedChannelName(
rawHostedChannelName: string | undefined
): string | undefined {
if (rawHostedChannelName == null || rawHostedChannelName.length <= 0) {
throw new ParseError(
`Malformed channel part in HOSTTARGET message: ${reasonForValue(
rawHostedChannelName
)}`
);
}
if (rawHostedChannelName === "-") {
return undefined;
} else {
return rawHostedChannelName;
}
}
export function parseViewerCount(
rawViewerCount: string | undefined
): number | undefined {
if (rawViewerCount == null || rawViewerCount.length <= 0) {
throw new ParseError(
`Malformed viewer count part in HOSTTARGET message: ${reasonForValue(
rawViewerCount
)}`
);
}
if (rawViewerCount === "-") {
return undefined;
}
const numberValue = parseInt(rawViewerCount);
if (isNaN(numberValue)) {
throw new ParseError(
`Malformed viewer count part in HOSTTARGET message: ${reasonForValue(
rawViewerCount
)}`
);
}
return numberValue;
}
export function parseHosttargetParameter(rawParameter: string): {
hostedChannelName: string | undefined;
viewerCount: number | undefined;
} {
const split = rawParameter.split(" ");
if (split.length !== 2) {
throw new ParseError(
"HOSTTARGET accepts exactly 2 arguments in second parameter, " +
`given: ${reasonForValue(rawParameter)}`
);
}
const [rawHostedChannelName, rawViewerCount] = split;
return {
hostedChannelName: parseHostedChannelName(rawHostedChannelName),
viewerCount: parseViewerCount(rawViewerCount),
};
}
export class HosttargetMessage extends ChannelIRCMessage {
/**
* channel name if now hosting channel,
*
* null if host mode was exited.
*/
public readonly hostedChannelName: string | undefined;
/**
* The viewer count of the enabled host.
*
* null if viewercount is unknown or host mode was exited.
*/
public readonly viewerCount: number | undefined;
public constructor(message: IRCMessageData) {
super(message);
const parsedSecondParameter = parseHosttargetParameter(
requireParameter(this, 1)
);
this.hostedChannelName = parsedSecondParameter.hostedChannelName;
this.viewerCount = parsedSecondParameter.viewerCount;
}
public wasHostModeExited(): this is ExitHostModeHosttargetMessage {
return this.hostedChannelName == null;
}
public wasHostModeEntered(): this is ExitedHostModeHosttargetMessage {
return this.hostedChannelName != null;
}
}
export interface ExitHostModeHosttargetMessage extends HosttargetMessage {
readonly hostedChannelName: undefined;
readonly viewerCount: undefined;
}
export interface ExitedHostModeHosttargetMessage extends HosttargetMessage {
readonly hostedChannelName: string;
readonly viewerCount: number | undefined;
}