1
- console . log ( 'All of the lights, all of the lights!' ) ;
1
+ var dotenv = require ( 'dotenv' ) ;
2
+ dotenv . _getKeysAndValuesFromEnvFilePath ( 'config/.env' ) ;
3
+ dotenv . _setEnvs ( ) ;
4
+
5
+ var twitch_user = process . env . TWITCH_USER ,
6
+ twitch_oauth = process . env . TWITCH_OAUTH ,
7
+ twitch_channels = process . env . TWITCH_channels ;
8
+
9
+ var irc = require ( 'twitch-irc' ) ,
10
+ hue = require ( "node-hue-api" ) ,
11
+ async = require ( 'async' ) ,
12
+ _ = require ( 'lodash' ) ,
13
+ Datastore = require ( 'nedb' ) ;
14
+
15
+ // initialize the db
16
+ var db = { } ;
17
+ db . hue = new Datastore ( { filename : 'config/hue.db' , autoload : true } ) ;
18
+ db . twitch = new Datastore ( { filename : 'config/twitch.db' , autoload : true } ) ;
19
+
20
+ // Variables go here
21
+ var hueClass = hue . HueApi ,
22
+ hueApi = new hueClass ( ) ;
23
+
24
+ var displayError = function ( err ) {
25
+ console . error ( err ) ;
26
+ } ;
27
+
28
+ async . waterfall ( [
29
+ function ( callback ) {
30
+ db . hue . find ( { _id : 'bridge' } , function ( err , docs ) {
31
+ if ( err ) displayError ( err ) ;
32
+ if ( docs . length === 0 ) {
33
+ // Bridge not found, let's find and add it
34
+ hue . nupnpSearch ( ) . then ( function ( result ) {
35
+ delete result [ 0 ] [ 'id' ] ;
36
+ result [ 0 ] . _id = 'bridge' ;
37
+ // insert it into the db
38
+ db . hue . insert ( result , function ( err , newDoc ) {
39
+ if ( err ) displayError ( err ) ;
40
+ callback ( null , newDoc . hostname ) ;
41
+ } ) ;
42
+ } ) . done ( ) ;
43
+ } else {
44
+ callback ( null , docs [ 0 ] [ 'ipaddress' ] ) ;
45
+ } ;
46
+ } ) ;
47
+ } ,
48
+ function ( hostname , callback ) {
49
+ db . hue . find ( { _id : 'username' } , function ( err , docs ) {
50
+ if ( err ) displayError ( err ) ;
51
+ if ( docs . length === 0 ) {
52
+ newHue . registerUser ( hostname , null , 'Prettylights Twitch Bot' )
53
+ . then ( function ( result ) {
54
+ // insert it into the db
55
+ var doc = { } ;
56
+ doc . _id = 'username' ;
57
+ doc . value = result ;
58
+ db . hue . insert ( doc , function ( err , newDoc ) {
59
+ if ( err ) displayError ( err ) ;
60
+ callback ( null , hostname , newDoc . value ) ;
61
+ } ) ;
62
+ } )
63
+ . fail ( displayError )
64
+ . done ( ) ;
65
+ } else {
66
+ callback ( null , hostname , docs [ 0 ] [ 'value' ] ) ;
67
+ }
68
+ } ) ;
69
+ }
70
+ ] , function ( err , hostname , username ) {
71
+ var lightsApi = new hueClass ( hostname , username ) ;
72
+
73
+ lightsApi . getFullState ( ) . then ( function ( result ) {
74
+ _ . forEach ( result , function ( value , name ) {
75
+ var doc = value ;
76
+ doc . _id = name ;
77
+ db . hue . insert ( doc , function ( err , newDoc ) {
78
+ if ( err ) return ;
79
+ } ) ;
80
+ } ) ;
81
+
82
+ twitchBot ( lightsApi ) ;
83
+ } ) . done ( ) ;
84
+ } ) ;
85
+
86
+ // Add the bot to channel(s) and listen for commands
87
+ var twitchBot = function ( lightsApi ) {
88
+ var lightState = hue . lightState ,
89
+ state = lightState . create ( ) ;
90
+ var test = state . hsl
91
+
92
+ // Twitch client options...
93
+ var client = new irc . client ( {
94
+ options : {
95
+ debug : true ,
96
+ debugIgnore : [ 'ping' , 'chat' , 'action' ] ,
97
+ tc : 3
98
+ } ,
99
+ identity : {
100
+ username : twitch_user ,
101
+ password : twitch_oauth
102
+ } ,
103
+ channels : [ twitch_channels ]
104
+ } ) ;
105
+
106
+ // Connect to the chat server..
107
+ client . connect ( ) ;
108
+
109
+ // Chat event listeners
110
+ client . addListener ( 'chat' , function ( channel , user , message ) {
111
+ if ( message . indexOf ( '!lights help' ) === 0 ) {
112
+ client . say ( channel , '!lights on - !lights off - !lights pretty - !lights <color> (white, red, blue, yellow, green, purple)' ) ;
113
+ }
114
+ } ) ;
115
+
116
+ client . addListener ( 'chat' , function ( channel , user , message ) {
117
+ if ( message . indexOf ( '!lights off' ) === 0 ) {
118
+ lightsApi . setGroupLightState ( 0 , { 'on' : false } )
119
+ . then ( )
120
+ . fail ( displayError )
121
+ . done ( ) ;
122
+ }
123
+ } ) ;
124
+
125
+ client . addListener ( 'chat' , function ( channel , user , message ) {
126
+ if ( message . indexOf ( '!lights on' ) === 0 ) {
127
+ lightsApi . setGroupLightState ( 0 , { 'on' : true , 'ct' : 200 , 'bri' : 180 , 'effect' : 'none' } )
128
+ . then ( )
129
+ . fail ( displayError )
130
+ . done ( ) ;
131
+ }
132
+ } ) ;
133
+
134
+ client . addListener ( 'chat' , function ( channel , user , message ) {
135
+ if ( message . indexOf ( '!lights white' ) === 0 ) {
136
+ lightsApi . setGroupLightState ( 0 , { 'on' : true , 'ct' : 200 , 'bri' : 180 , 'effect' : 'none' } )
137
+ . then ( )
138
+ . fail ( displayError )
139
+ . done ( ) ;
140
+ }
141
+ } ) ;
142
+
143
+ client . addListener ( 'chat' , function ( channel , user , message ) {
144
+ if ( message . indexOf ( '!lights red' ) === 0 ) {
145
+ lightsApi . setGroupLightState ( 0 , { 'on' : true , 'hue' : 65534 , 'bri' : 180 , 'sat' : 255 , 'effect' : 'none' } )
146
+ . then ( )
147
+ . fail ( displayError )
148
+ . done ( ) ;
149
+ }
150
+ } ) ;
151
+
152
+ client . addListener ( 'chat' , function ( channel , user , message ) {
153
+ if ( message . indexOf ( '!lights green' ) === 0 ) {
154
+ lightsApi . setGroupLightState ( 0 , { 'on' : true , 'hue' : 25500 , 'bri' : 180 , 'sat' : 255 , 'effect' : 'none' } )
155
+ . then ( )
156
+ . fail ( displayError )
157
+ . done ( ) ;
158
+ }
159
+ } ) ;
160
+
161
+ client . addListener ( 'chat' , function ( channel , user , message ) {
162
+ if ( message . indexOf ( '!lights blue' ) === 0 ) {
163
+ lightsApi . setGroupLightState ( 0 , { 'on' : true , 'hue' : 46920 , 'bri' : 180 , 'sat' : 255 , 'effect' : 'none' } )
164
+ . then ( )
165
+ . fail ( displayError )
166
+ . done ( ) ;
167
+ }
168
+ } ) ;
169
+
170
+ client . addListener ( 'chat' , function ( channel , user , message ) {
171
+ if ( message . indexOf ( '!lights purple' ) === 0 ) {
172
+ lightsApi . setGroupLightState ( 0 , { 'on' : true , 'hue' : 56100 , 'bri' : 180 , 'sat' : 255 , 'effect' : 'none' } )
173
+ . then ( )
174
+ . fail ( displayError )
175
+ . done ( ) ;
176
+ }
177
+ } ) ;
178
+
179
+ client . addListener ( 'chat' , function ( channel , user , message ) {
180
+ if ( message . indexOf ( '!lights yellow' ) === 0 ) {
181
+ lightsApi . setGroupLightState ( 0 , { 'on' : true , 'hue' : 16750 , 'bri' : 180 , 'sat' : 200 , 'effect' : 'none' } )
182
+ . then ( )
183
+ . fail ( displayError )
184
+ . done ( ) ;
185
+ }
186
+ } ) ;
187
+
188
+ client . addListener ( 'chat' , function ( channel , user , message ) {
189
+ if ( message . indexOf ( '!lights pretty' ) === 0 ) {
190
+ lightsApi . setGroupLightState ( 0 , { 'on' : true , 'effect' : 'colorloop' , 'bri' : 180 } )
191
+ . then ( )
192
+ . fail ( displayError )
193
+ . done ( ) ;
194
+ }
195
+ } ) ;
196
+ }
0 commit comments