Skip to content

Commit

Permalink
Support for multiple channels
Browse files Browse the repository at this point in the history
  • Loading branch information
valzargaming committed Mar 8, 2021
1 parent 57f70cd commit 32b2209
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
11 changes: 9 additions & 2 deletions Twitch/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ public function __construct(Twitch $twitch, bool $verbose)
$this->twitch = $twitch;
$this->verbose = $verbose;
}
public function handle(string $command): ?string
public function handle(string $command, ?array $args = []): ?string
{
if ($this->verbose) $this->twitch->emit("[HANDLE COMMAND] `$command`");
echo '[ARGS] '; var_dump($args); echo PHP_EOL;
if ($command == 'help')
{
{
$commandsymbol = $this->twitch->getCommandSymbol();
$responses = $this->twitch->getResponses();
$functions = $this->twitch->getFunctions();
Expand Down Expand Up @@ -87,6 +88,12 @@ public function handle(string $command): ?string
$this->twitch->close();
}

if ($command == 'join')
{
if ($this->verbose) $this->twitch->emit('[JOIN]');
$this->twitch->joinChannel($args[1]);
}

return $response;
}
}
Expand Down
23 changes: 13 additions & 10 deletions Twitch/twitch.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ function __construct(array $options = [])
$this->loop = $options['loop'];
$this->secret = $options['secret'];
$this->nick = $options['nick'];
$this->channel = strtolower($options['channel']) ?? strtolower($options['nick']);
foreach($options['channel'] as $channel)
$this->channel[] = strtolower($channel);
if(is_null($this->channel)) $this->channel = $options['nick'];
$this->commandsymbol = $options['commandsymbol'] ?? array('!');

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

public function sendMessage(string $data, string $channel = null): void
{
$this->connection->write("PRIVMSG #" . ($channel ?? $this->channel) . " :" . $data . "\n");
$this->emit("[REPLY] $data");
$this->connection->write("PRIVMSG #" . ($channel ?? $this->reallastchannel) . " :" . $data . "\n");
$this->emit('[REPLY] #' . ($channel ?? $this->reallastchannel) . ' - ' . $data);
}

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

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

Expand All @@ -189,7 +192,7 @@ protected function process(string $data, ConnectionInterface $connection): void
if ($response) {
$payload = '@' . $this->lastuser . ', ' . $response . "\n";
$this->sendMessage($payload);
$this->discordRelay('[REPLY] ' . $payload);
$this->discordRelay('[REPLY] #' . $this->reallastchannel . ' - ' . $payload);
}
}
}
Expand All @@ -204,7 +207,7 @@ protected function parseMessage(string $data): ?string
$this->lastmessage = $messageContents;
$this->reallastuser = $this->parseUser($data);
$this->reallastchannel = $this->parseChannel($data);
$this->discordRelay('[MSG] ' . $this->reallastuser . ': ' . $messageContents);
$this->discordRelay('[MSG] #' . $this->reallastchannel . ' - ' . $this->reallastuser . ': ' . $messageContents);

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

//Whitelisted commands
if ( in_array($this->lastuser, $this->whitelist) || ($this->lastuser == $this->nick) ) {
if (in_array($command, $this->restricted_functions)) {
if ($this->verbose) $this->emit('[RESTRICTED FUNCTION]');
$response = $this->commands->handle($command);
$response = $this->commands->handle($command, $dataArr);
}
}

//Bot owner commands (shares the same username)
if ($this->lastuser == $this->nick) {
if (in_array($command, $this->private_functions)) {
if ($this->verbose) $this->emit('[PRIVATE FUNCTION]');
$response = $this->commands->handle($command);
$response = $this->commands->handle($command, $dataArr);
}
}

Expand Down
5 changes: 4 additions & 1 deletion run.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
//Required
'secret' => $secret, // Client secret
'nick' => 'ValZarGaming', // Twitch username
'channel' => 'daathren', // Channel to join
'channel' => [
'daathren', // Channel to join
'valzargaming', // (Optional) Additional channels
],

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

0 comments on commit 32b2209

Please sign in to comment.