1+ import
2+ irc, times, terminal, random, strutils, os
3+ from rawsockets import Port
4+
5+ const
6+ CHAT_URL = " irc.chat.twitch.tv"
7+ CHAT_PORT = Port (6667 )
8+
9+ # helper function to clean up some (messier than this) code
10+ proc colorPrint (s: string , c: ForegroundColor ): void =
11+ setForegroundColor (c)
12+ echo (s)
13+ setForegroundColor (fgWhite)
14+
15+ # add a hash to the channel name if there isn't one there already
16+ proc addHash (s: string ): string =
17+ var str = s
18+ if s.startsWith ('#' ):
19+ return str
20+ else :
21+ str = " #" & str
22+ return str
23+
24+ # anonymous account generation (justinfan + 14 random numbers)
25+ proc randomTwitchUser (): string =
26+ random.randomize (epochTime ().int )
27+ var name = " justinfan"
28+ for i in 0 .. 13 :
29+ name &= strutils.intToStr (random.random (9 ))
30+ return name
31+
32+ var chans = newSeq [string ](0 )
33+ if paramCount () == 0 :
34+ echo (" Type the usernames of the channels to join, seperated by a space: " )
35+ var channel: string = readLine (stdin)
36+ for chan in splitWhitespace (channel):
37+ chans.add (addHash (chan))
38+ elif paramCount () >= 1 :
39+ for param in commandLineParams ():
40+ chans.add (addHash (param))
41+
42+ var username = randomTwitchUser ()
43+ var t = irc.newIrc (CHAT_URL , CHAT_PORT , username, username,
44+ joinChans = chans)
45+ var curtime: string
46+
47+ t.connect ()
48+ # this gives us things such as userlist, joins, parts and mod status:
49+ t.send (" CAP REQ :twitch.tv/membership" , false )
50+
51+ while true :
52+ var event: IrcEvent
53+ if t.poll (event):
54+ curtime = " [" & getClockStr ()& " ] "
55+ case event.typ
56+ of EvConnected : echo (curtime & " - [INFO] Connected to server" )
57+ of EvDisconnected :
58+ colorPrint (curtime & " - [ERR] Timeout, reconnecting..." , fgRed)
59+ t.reconnect ()
60+ of EvTimeout :
61+ colorPrint (curtime & " - [ERR] Timeout, reconnecting..." , fgRed)
62+ t.reconnect ()
63+ of EvMsg :
64+ case event.cmd
65+ of MPrivMsg :
66+ colorPrint (curtime & event.origin & " - [MSG] " & event.nick & " : " & event.params[1 ], fgWhite)
67+ of MJoin :
68+ colorPrint (curtime & event.origin & " - [JOIN] " & event.nick, fgYellow)
69+ of MPart :
70+ colorPrint (curtime & event.origin & " - [PART] " & event.nick, fgYellow)
71+ of MMode :
72+ if event.params[1 ] == " +o" :
73+ colorPrint (curtime & event.origin & " - [+MOD] " & event.params[2 ], fgCyan)
74+ elif event.params[1 ] == " -o" :
75+ colorPrint (curtime & event.origin & " - [-MOD] " & event.params[2 ], fgCyan)
76+ else :
77+ discard
0 commit comments