-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathglobaluserstate.ts
More file actions
78 lines (72 loc) · 2.5 KB
/
globaluserstate.ts
File metadata and controls
78 lines (72 loc) · 2.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
import type { TwitchBadgesList } from "../badges";
import type { Color } from "../color";
import type { TwitchEmoteSets } from "../parser/emote-sets";
import { type IRCMessageData, IRCMessage } from "../irc/irc-message";
import { tagParserFor } from "../parser/tag-values";
/**
* Global state of the logged in user.
*/
export interface GlobalUserState {
badgeInfo: TwitchBadgesList;
badgeInfoRaw: string;
badges: TwitchBadgesList;
badgesRaw: string;
color: Color | undefined;
colorRaw: string;
displayName: string;
emoteSets: TwitchEmoteSets;
emoteSetsRaw: string;
userID: string;
}
export class GlobaluserstateMessage
extends IRCMessage
implements GlobalUserState
{
public readonly badgeInfo: TwitchBadgesList;
public readonly badgeInfoRaw: string;
public readonly badges: TwitchBadgesList;
public readonly badgesRaw: string;
public readonly color: Color | undefined;
public readonly colorRaw: string;
public readonly displayName: string;
public readonly emoteSets: TwitchEmoteSets;
public readonly emoteSetsRaw: string;
public readonly userID: string;
/** @deprecated Use {@link userID} instead. */
public get userId(): string {
return this.userID;
}
public constructor(message: IRCMessageData) {
super(message);
const tagParser = tagParserFor(this.ircTags);
this.badgeInfo = tagParser.requireBadges("badge-info");
this.badgeInfoRaw = tagParser.requireString("badge-info");
this.badges = tagParser.requireBadges("badges");
this.badgesRaw = tagParser.requireString("badges");
this.color = tagParser.getColor("color");
this.colorRaw = tagParser.requireString("color");
// trim: Twitch workaround for unsanitized data, see https://github.com/robotty/dank-twitch-irc/issues/33
this.displayName = tagParser.requireString("display-name").trim();
this.emoteSets = tagParser.requireEmoteSets("emote-sets");
this.emoteSetsRaw = tagParser.requireString("emote-sets");
this.userID = tagParser.requireString("user-id");
}
/**
* Extracts a plain object only containing the fields defined by the
* {@link GlobalUserState} interface.
*/
public extractGlobalUserState(): GlobalUserState {
return {
badgeInfo: this.badgeInfo,
badgeInfoRaw: this.badgeInfoRaw,
badges: this.badges,
badgesRaw: this.badgesRaw,
color: this.color,
colorRaw: this.colorRaw,
displayName: this.displayName,
emoteSets: this.emoteSets,
emoteSetsRaw: this.emoteSetsRaw,
userID: this.userID,
};
}
}