Skip to content

Commit 32b2209

Browse files
committed
Support for multiple channels
1 parent 57f70cd commit 32b2209

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

Twitch/Commands.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ public function __construct(Twitch $twitch, bool $verbose)
2222
$this->twitch = $twitch;
2323
$this->verbose = $verbose;
2424
}
25-
public function handle(string $command): ?string
25+
public function handle(string $command, ?array $args = []): ?string
2626
{
2727
if ($this->verbose) $this->twitch->emit("[HANDLE COMMAND] `$command`");
28+
echo '[ARGS] '; var_dump($args); echo PHP_EOL;
2829
if ($command == 'help')
29-
{
30+
{
3031
$commandsymbol = $this->twitch->getCommandSymbol();
3132
$responses = $this->twitch->getResponses();
3233
$functions = $this->twitch->getFunctions();
@@ -87,6 +88,12 @@ public function handle(string $command): ?string
8788
$this->twitch->close();
8889
}
8990

91+
if ($command == 'join')
92+
{
93+
if ($this->verbose) $this->twitch->emit('[JOIN]');
94+
$this->twitch->joinChannel($args[1]);
95+
}
96+
9097
return $response;
9198
}
9299
}

Twitch/twitch.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ function __construct(array $options = [])
6262
$this->loop = $options['loop'];
6363
$this->secret = $options['secret'];
6464
$this->nick = $options['nick'];
65-
$this->channel = strtolower($options['channel']) ?? strtolower($options['nick']);
65+
foreach($options['channel'] as $channel)
66+
$this->channel[] = strtolower($channel);
67+
if(is_null($this->channel)) $this->channel = $options['nick'];
6668
$this->commandsymbol = $options['commandsymbol'] ?? array('!');
6769

6870
foreach ($options['whitelist'] as $whitelist){
@@ -112,13 +114,13 @@ public function close(bool $closeLoop = true): void
112114

113115
public function sendMessage(string $data, string $channel = null): void
114116
{
115-
$this->connection->write("PRIVMSG #" . ($channel ?? $this->channel) . " :" . $data . "\n");
116-
$this->emit("[REPLY] $data");
117+
$this->connection->write("PRIVMSG #" . ($channel ?? $this->reallastchannel) . " :" . $data . "\n");
118+
$this->emit('[REPLY] #' . ($channel ?? $this->reallastchannel) . ' - ' . $data);
117119
}
118120

119121
public function joinChannel(string $string): void
120122
{
121-
$connection->write("JOIN #" . strtolower($string) . "\n");
123+
$this->connection->write("JOIN #" . strtolower($string) . "\n");
122124
if ($this->verbose) $this->emit('[VERBOSE] [JOINCHANNEL] `' . strtolower($string) . '`');
123125
}
124126

@@ -166,7 +168,8 @@ protected function initIRC(ConnectionInterface $connection): void
166168
$connection->write("PASS " . $this->secret . "\n");
167169
$connection->write("NICK " . $this->nick . "\n");
168170
$connection->write("CAP REQ :twitch.tv/membership\n");
169-
$connection->write("JOIN #" . $this->channel . "\n");
171+
foreach ($this->channel as $channel)
172+
$connection->write("JOIN #" . $channel . "\n");
170173
if ($this->verbose) $this->emit('[INIT IRC]');
171174
}
172175

@@ -189,7 +192,7 @@ protected function process(string $data, ConnectionInterface $connection): void
189192
if ($response) {
190193
$payload = '@' . $this->lastuser . ', ' . $response . "\n";
191194
$this->sendMessage($payload);
192-
$this->discordRelay('[REPLY] ' . $payload);
195+
$this->discordRelay('[REPLY] #' . $this->reallastchannel . ' - ' . $payload);
193196
}
194197
}
195198
}
@@ -204,7 +207,7 @@ protected function parseMessage(string $data): ?string
204207
$this->lastmessage = $messageContents;
205208
$this->reallastuser = $this->parseUser($data);
206209
$this->reallastchannel = $this->parseChannel($data);
207-
$this->discordRelay('[MSG] ' . $this->reallastuser . ': ' . $messageContents);
210+
$this->discordRelay('[MSG] #' . $this->reallastchannel . ' - ' . $this->reallastuser . ': ' . $messageContents);
208211

209212
$commandsymbol = '';
210213
foreach($this->commandsymbol as $symbol) {
@@ -224,22 +227,22 @@ protected function parseMessage(string $data): ?string
224227
//Public commands
225228
if (in_array($command, $this->functions)) {
226229
if ($this->verbose) $this->emit('[FUNCTION]');
227-
$response = $this->commands->handle($command);
230+
$response = $this->commands->handle($command, $dataArr);
228231
}
229232

230233
//Whitelisted commands
231234
if ( in_array($this->lastuser, $this->whitelist) || ($this->lastuser == $this->nick) ) {
232235
if (in_array($command, $this->restricted_functions)) {
233236
if ($this->verbose) $this->emit('[RESTRICTED FUNCTION]');
234-
$response = $this->commands->handle($command);
237+
$response = $this->commands->handle($command, $dataArr);
235238
}
236239
}
237240

238241
//Bot owner commands (shares the same username)
239242
if ($this->lastuser == $this->nick) {
240243
if (in_array($command, $this->private_functions)) {
241244
if ($this->verbose) $this->emit('[PRIVATE FUNCTION]');
242-
$response = $this->commands->handle($command);
245+
$response = $this->commands->handle($command, $dataArr);
243246
}
244247
}
245248

run.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
//Required
1515
'secret' => $secret, // Client secret
1616
'nick' => 'ValZarGaming', // Twitch username
17-
'channel' => 'daathren', // Channel to join
17+
'channel' => [
18+
'daathren', // Channel to join
19+
'valzargaming', // (Optional) Additional channels
20+
],
1821

1922
//Optional
2023
//'discord' => $discord, // Pass your own instance of DiscordPHP (https://github.com/discord-php/DiscordPHP)

0 commit comments

Comments
 (0)