diff options
| author | ilotterytea <iltsu@alright.party> | 2025-10-31 17:59:00 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-10-31 17:59:00 +0500 |
| commit | f0da8b89f8168a8a65fcc27a2344440045c09c89 (patch) | |
| tree | b64ee898aa9dfb37fd9846ea54b2c0140c218f9b | |
| parent | aed413b1bed8dc2883a94262e4c6fa64e7e986de (diff) | |
feat: parse irc messages
| -rw-r--r-- | scripts/chat.js | 61 |
1 files changed, 56 insertions, 5 deletions
diff --git a/scripts/chat.js b/scripts/chat.js index b78f946..166a4c2 100644 --- a/scripts/chat.js +++ b/scripts/chat.js @@ -1,3 +1,45 @@ +function parseIRCMessage(line) { + line = line.replace(/\r?\n$/, ''); + let tags = {}; + let prefix = null; + let nick = null; + let command = null; + const params = []; + + // tags + if (line[0] === '@') { + const end = line.indexOf(' '); + const rawTags = line.slice(1, end); + for (const tag of rawTags.split(';')) { + const [k, v = true] = tag.split('='); + tags[k] = v === true ? true : v; + } + line = line.slice(end + 1); + } + + // prefix + if (line[0] === ':') { + const end = line.indexOf(' '); + prefix = line.slice(1, end); + const bang = prefix.indexOf('!'); + if (bang !== -1) nick = prefix.slice(0, bang); + line = line.slice(end + 1); + } + + // command and params + const parts = line.split(' '); + command = parts.shift(); + for (let i = 0; i < parts.length; i++) { + if (parts[i][0] === ':') { + params.push(parts.slice(i).join(' ').slice(1)); + break; + } + if (parts[i].length) params.push(parts[i]); + } + + return { tags, prefix, nick, command, params }; +} + function displayMessage(data) { const messages = document.getElementById("window-messages"); @@ -58,13 +100,22 @@ class TwitchIRCClient { if (line.trim().length == 0) { continue; } - if (line.includes("001")) { - for (const c of this.joinedChannels) { - this.join(c); + + const msg = parseIRCMessage(line); + + switch (msg.command) { + case "001": { + for (const c of this.joinedChannels) { + this.join(c); + } + displaySystemMessage("*", "Connected!"); + break; + } + case "PRIVMSG": { + displayMessage(msg); } - displaySystemMessage("*", "Connected!"); + default: break; } - displaySystemMessage("forsen", line, "Message"); } }); |
