1010namespace Twitch ;
1111
1212use Twitch \Commands ;
13- use React \EventLoop \Factory ;
1413use React \Socket \ConnectionInterface ;
1514use React \Socket \Connector ;
1615use Evenement \EventEmitterTrait ;
@@ -60,8 +59,8 @@ function __construct(array $options = [])
6059 $ this ->loop = $ options ['loop ' ];
6160 $ this ->secret = $ options ['secret ' ];
6261 $ this ->nick = $ options ['nick ' ];
63- foreach ( $ options [ ' channels ' ] as $ channel ) $ this ->channels [] = strtolower ( $ channel ) ;
64- if (is_null ($ this ->channels )) $ this ->channels = array ( $ options ['nick ' ]) ;
62+ $ this ->channels = $ options [ ' channels ' ] ;
63+ if (is_null ($ this ->channels )) $ this ->channels [ $ options ['nick ' ]] = [] ;
6564 $ this ->commandsymbol = $ options ['commandsymbol ' ] ?? array ('! ' );
6665
6766 foreach ($ options ['whitelist ' ] as $ whitelist ) $ this ->whitelist [] = $ whitelist ;
@@ -70,8 +69,8 @@ function __construct(array $options = [])
7069 $ this ->restricted_functions = $ options ['restricted_functions ' ] ?? array ();
7170 $ this ->private_functions = $ options ['private_functions ' ] ?? array ();
7271
73- if (isset ($ options ['verbose ' ])) $ this ->verbose = $ options ['verbose ' ];
7472 if (isset ($ options ['socket_options ' ])) $ this ->socket_options = $ options ['socket_options ' ];
73+ if (isset ($ options ['verbose ' ])) $ this ->verbose = $ options ['verbose ' ];
7574 if (isset ($ options ['debug ' ])) $ this ->debug = $ options ['debug ' ];
7675 if (isset ($ options ['logger ' ]) && $ options ['logger ' ] instanceof \Monolog \Logger) $ this ->logger = $ options ['logger ' ];
7776 else {
@@ -81,11 +80,6 @@ function __construct(array $options = [])
8180
8281 if (isset ($ options ['discord ' ])) $ this ->discord = $ options ['discord ' ];
8382 if (isset ($ options ['discord_output ' ])) $ this ->discord_output = $ options ['discord_output ' ];
84- if (isset ($ options ['guild_id ' ]) && isset ($ options ['channel_id ' ])) {
85- $ this ->guild_channel_ids = [$ options ['guild_id ' ] => $ options ['channel_id ' ]];
86- $ this ->logger ->warning ('guild_id and channel_id in $options is deprecated! Please use an associative array for `$options[ \'guild_channel_ids \'] = [guild_id => channel_id]` instead ' );
87- }
88- if (isset ($ options ['guild_channel_ids ' ])) $ this ->guild_channel_ids = $ options ['guild_channel_ids ' ];
8983
9084 $ this ->connector = new Connector ($ this ->loop , $ options ['socket_options ' ]);
9185
@@ -109,61 +103,68 @@ public function close(bool $closeLoop = true): void
109103 if ($ this ->verbose ) $ this ->logger ->info ('[CLOSE] ' );
110104 if ($ this ->running ) {
111105 $ this ->running = false ;
112- foreach ($ this ->channels as $ channel ) $ this ->leaveChannel ($ channel );
106+ foreach ($ this ->channels as $ twitch_channel => $ arr ) $ this ->leaveChannel ($ twitch_channel );
113107 }
114108 if ($ closeLoop ) {
115109 if ($ this ->verbose ) $ this ->logger ->info ('[LOOP->STOP] ' );
116110 $ this ->loop ->stop ();
117111 }
118112 }
119113
114+ public function write (string $ string ): void
115+ {
116+ if ($ this ->debug ) $ this ->logger ->debug ("[WRITE] $ string " );
117+ $ this ->connection ->write ($ string );
118+ }
119+
120120 public function sendMessage (string $ data , ?string $ channel = null ): bool
121121 {
122122 if (isset ($ this ->connection ) && ($ this ->connection !== false )) {
123123 if ($ channel ) $ this ->lastchannel = $ channel ;
124- $ this ->connection ->write ("PRIVMSG # " . ($ this ->lastchannel ?? current ($ this ->channels )) . " : $ data \n" );
125- $ this ->logger ->info ('[REPLY] # ' . ($ this ->lastchannel ?? current ($ this ->channels )) . ' - ' . $ data );
126- return true ;
124+ if ($ this ->lastchannel ) {
125+ $ this ->write ("PRIVMSG # {$ this ->lastchannel }: $ data \n" );
126+ $ this ->logger ->info ("[REPLY] # {$ this ->lastchannel } - $ data " );
127+ return true ;
128+ }
127129 }
128130 return false ;
129131 }
130132
131- public function joinChannel (string $ string = "" )
133+ public function joinChannel (string $ string = "" , ? string $ guild_id = '' , ? string $ channel_id = '' )
132134 {
133135 if ($ this ->verbose ) $ this ->logger ->info ('[VERBOSE] [JOIN CHANNEL] ` ' . $ string . '` ' );
134136 if (! isset ($ this ->connection ) || $ this ->connection === false ) return ;
135137 if (! $ string ) return ;
136138
137139 $ string = strtolower ($ string );
138- $ this ->connection ->write ("JOIN # " . $ string . " \n" );
139- if (!in_array ( $ string, $ this ->channels )) $ this ->channels [] = $ string ;
140+ /*if (!isset( $this->channels[$string]))*/ $ this ->write ("JOIN # $ string \n" );
141+ if (!isset ( $ this -> channels [ $ string]) || ! isset ( $ this ->channels [ $ string ][ $ guild_id ] )) $ this ->channels [$ string ][ $ guild_id ] = $ channel_id ;
140142 }
141143
142144 /*
143145 * Commands.php should never send a string so as to prevent users from being able to tell the bot to leave someone else's channel
144146 * This command is exposed so other ReactPHP applications can call it, but those applications should always attempt to pass a valid string
145147 * getChannels has also been exposed for the purpose of checking if the string exists before attempting to call this function
146148 */
147- public function leaveChannel (?string $ string ): void
149+ public function leaveChannel (?string $ string, ? string $ guild_id = '' , ? string $ channel_id = '' ): bool
148150 {
149- if ($ this ->verbose ) $ this ->logger ->info ('[VERBOSE] [LEAVE CHANNEL] ` ' . $ string . '` ' );
150- if (! isset ($ this ->connection ) || $ this ->connection === false ) return ;
151151 $ string = strtolower ($ string ?? $ this ->lastchannel );
152- if (in_array ($ string , $ this ->channels )) {
153- $ this ->connection ->write ("PART # $ string \n" );
154- foreach ($ this ->channels as &$ channel ) if ($ channel == $ string ) {
155- unset ($ channel );
156- break ;
157- }
158- }
152+ if (! isset ($ this ->channels [$ string ])) return false ;
153+ if (! isset ($ this ->connection ) || $ this ->connection === false ) return false ;
154+ if ($ this ->verbose ) $ this ->logger ->info ("[VERBOSE] [LEAVE CHANNEL] ` $ string - $ guild_id - $ channel_id` " );
155+
156+ if (! $ guild_id ) unset($ this ->channels [$ string ]);
157+ else if ($ this ->discord ->guilds ->get ('id ' , $ guild_id ) && isset ($ this ->channels ['guild_id ' ])) unset($ this ->channels ['guild_id ' ]);
158+
159+ if (! isset ($ this ->channels [$ string ]) || empty ($ this ->channels [$ string ])) $ this ->write ("PART # $ string \n" );
159160 }
160161
161162 public function ban ($ username , $ reason = '' ): bool
162163 {
163164 if ($ this ->verbose ) $ this ->logger ->info ("[BAN] $ username - $ reason " );
164165 if (! isset ($ this ->connection ) || $ this ->connection === false ) return false ;
165- if ($ username != $ this ->nick && !in_array ($ username , $ this ->channels )) {
166- $ this ->connection -> write ("/ban $ username $ reason " );
166+ if ($ username != $ this ->nick && !in_array ($ username , array_keys ( $ this ->channels ) )) {
167+ $ this ->write ("/ban $ username $ reason " );
167168 return true ;
168169 }
169170 return false ;
@@ -201,6 +202,7 @@ protected function connect()
201202 function (ConnectionInterface $ connection ) {
202203 $ this ->initIRC ($ this ->connection = $ connection );
203204 $ connection ->on ('data ' , function ($ data ) {
205+ if ($ this ->debug ) $ this ->logger ->debug ("[DATA] $ data " );
204206 if ($ this ->connection !== false ) $ this ->process ($ data , $ this ->connection );
205207 });
206208 $ connection ->on ('close ' , function () {
@@ -215,27 +217,26 @@ function (\Exception $exception) {
215217 }
216218 protected function initIRC (ConnectionInterface $ connection ): void
217219 {
218- $ connection ->write ("PASS " . $ this ->secret . "\n" );
219- $ connection ->write ("NICK " . $ this ->nick . "\n" );
220- $ connection ->write ("CAP REQ :twitch.tv/membership \n" );
221- foreach ($ this ->channels as $ channel ) $ this ->joinChannel ( $ channel );
220+ $ this ->write ("PASS " . $ this ->secret . "\n" );
221+ $ this ->write ("NICK " . $ this ->nick . "\n" );
222+ $ this ->write ("CAP REQ :twitch.tv/membership \n" );
223+ foreach (array_keys ( $ this ->channels ) as $ twitch_channel ) $ this ->write ( " JOIN # $ twitch_channel \n" );
222224 if ($ this ->verbose ) $ this ->logger ->info ('[INIT IRC] ' );
223225 }
224226
225227 protected function pingPong (ConnectionInterface $ connection ): void
226228 {
227229 if ($ this ->debug ) $ this ->logger ->debug ('[ ' . date ('h:i:s ' ) . '] PING :tmi.twitch.tv ' );
228- $ connection ->write ("PONG :tmi.twitch.tv \n" );
230+ $ this ->write ("PONG :tmi.twitch.tv \n" );
229231 if ($ this ->debug ) $ this ->logger ->debug ('[ ' . date ('h:i:s ' ) . '] PONG :tmi.twitch.tv ' );
230232 }
231233
232234 protected function process (string $ data , ConnectionInterface $ connection )
233235 {
234- if ($ this ->debug ) $ this ->logger ->debug ("[DEBUG] [DATA] $ data " );
235236 if (trim ($ data ) == 'PING :tmi.twitch.tv ' ) return $ this ->pingPong ($ connection );
236237 if (preg_match ('/PRIVMSG/ ' , $ data )) {
237238 if ($ response = $ this ->parseCommand ($ data )) {
238- $ this ->discordRelay ("[REPLY] # {$ this ->lastchannel } - $ response " );
239+ $ this ->discordRelay ("[REPLY] # {$ this ->lastchannel } - $ response " );
239240 if (!$ this ->sendMessage ("@ {$ this ->lastuser }, $ response \n" )) $ this ->logger ->warning ('[FAILED TO SEND MESSAGE TO TWITCH] ' );
240241 }
241242 }
@@ -257,7 +258,6 @@ protected function parseCommand(string $data): string
257258 $ this ->lastmessage = trim (substr ($ data , strpos ($ data , 'PRIVMSG ' )+11 +strlen ($ this ->lastchannel )));
258259
259260 $ msg = "# {$ this ->lastchannel } - {$ this ->lastuser }: {$ this ->lastmessage }" ;
260- if ($ this ->debug ) $ this ->logger ->debug ("[DATA] ` $ data'` " );
261261 if ($ this ->verbose ) $ this ->logger ->info ("[PRIVMSG] $ msg " );
262262 if (!empty ($ this ->badwords ) && $ this ->badwordsCheck ($ this ->lastmessage ) && $ this ->lastuser != $ this ->nick ) {
263263 $ this ->ban ($ this ->lastuser );
@@ -367,14 +367,13 @@ public function linkDiscord($discord): void
367367
368368 public function discordRelay ($ payload ): bool
369369 {
370- if (! $ this ->discord_output ) return false ;
370+ if (! $ this ->discord_output || ! $ discord = $ this -> discord ) return false ;
371371 if ($ this ->verbose ) $ this ->logger ->info ('[DISCORD CHAT RELAY] ' );
372- if ($ discord = $ this ->discord ) {
373- foreach ($ this ->guild_channel_ids as $ guild_id => $ channel_id ) {
374- if (! $ guild = $ discord ->guilds ->get ('id ' , $ guild_id )) return false ;
375- if (! $ channel = $ guild ->channels ->get ('id ' , $ channel_id )) return false ;
376- if (! $ channel ->sendMessage ($ payload )) $ this ->logger ->warning ('[FAILED TO SEND MESSAGE TO TWITCH] ' );
377- }
372+ if (empty ($ this ->channels )) return false ;
373+ foreach ($ this ->channels [$ this ->lastchannel ] as $ guild_id => $ channel_id ) {
374+ if (! $ guild = $ this ->discord ->guilds ->get ('id ' , $ guild_id )) continue ;
375+ if (! $ channel = $ guild ->channels ->get ('id ' , $ channel_id )) continue ;
376+ if (! $ channel ->sendMessage ($ payload )) $ this ->logger ->warning ('[FAILED TO SEND MESSAGE TO TWITCH] ' );
378377 }
379378 return true ;
380379 }
0 commit comments