Skip to content

Commit 14e4863

Browse files
committed
Nullsafe tweaks; Some doc blocks; Emit on connection closed
1 parent bd263a6 commit 14e4863

1 file changed

Lines changed: 46 additions & 23 deletions

File tree

Twitch/Twitch.php

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -115,29 +115,39 @@ public function close(bool $closeLoop = true): void
115115
public function sendMessage(string $data, ?string $channel = null): void
116116
{
117117
$this->connection->write("PRIVMSG #" . ($channel ?? $this->reallastchannel ?? current($this->channels)) . " :" . $data . "\n");
118-
$this->emit('[REPLY] #' . ($channel ?? $this->reallastchannel) . ' - ' . $data);
119-
if ($channel) $this->reallastchannel = $channel;
118+
$this->emit('[REPLY] #' . ($channel ?? $this->reallastchannel ?? current($this->channels)) . ' - ' . $data);
119+
if ($channel) $this->reallastchannel = $channel ?? $this->reallastchannel ?? current($this->channels);
120120
}
121121

122-
public function joinChannel(string $string): void
122+
public function joinChannel(?string $string = ""): void
123123
{
124-
$string = strtolower($string) ?? $this->reallastchannel;
125124
if ($this->verbose) $this->emit('[VERBOSE] [JOIN CHANNEL] `' . $string . '`');
126-
$this->connection->write("JOIN #" . $string . "\n");
127-
if (!in_array($string, $this->channels)) $this->channels[] = $string;
125+
if($string){
126+
$string = strtolower($string);
127+
$this->connection->write("JOIN #" . $string . "\n");
128+
if (!in_array($string, $this->channels)) $this->channels[] = $string;
129+
}
128130
}
129131

130-
public function leaveChannel(?string $string = null): void // Commands.php should not send a string or it is possible for users to tell the bot to leave someone else's channel!
132+
/*
133+
* 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
134+
* This command is exposed so other ReactPHP applications can call it, but those applications should always attempt to pass a valid string
135+
* getChannels has also been exposed for the purpose of checking if the string exists before attempting to call this function
136+
*/
137+
public function leaveChannel(?string $string = ""): void
131138
{
132-
$string = strtolower($string ?? $this->reallastchannel);
133139
if ($this->verbose) $this->emit('[VERBOSE] [LEAVE CHANNEL] `' . $string . '`');
140+
$string = strtolower($string ?? $this->reallastchannel);
134141
$this->connection->write("PART #" . ($string ?? $this->reallastchannel) . "\n");
135142
foreach ($this->channels as &$channel){
136143
if ($channel == $string) $channel = null;
137144
unset ($channel);
138145
}
139146
}
140147

148+
/*
149+
* Attempt to catch errors with the user-provided $options early
150+
*/
141151
protected function resolveOptions(array $options = []): array
142152
{
143153
if (!$options['secret']) {
@@ -155,27 +165,37 @@ protected function resolveOptions(array $options = []): array
155165
return $options;
156166
}
157167

168+
/*
169+
* Connect the bot to Twitch
170+
* This command should not be run while the bot is still connected to Twitch
171+
* Additional handling may be needed in the case of disconnect via $connection->on('close' (See: Issue #1 on GitHub)
172+
*/
158173
protected function connect(): void
159174
{
160175
$url = 'irc.chat.twitch.tv';
161176
$port = '6667';
162177
if ($this->verbose) $this->emit("[CONNECT] $url:$port");
163178

164-
$twitch = $this;
165-
$this->connector->connect("$url:$port")->then(
166-
function (ConnectionInterface $connection) use ($twitch) {
167-
$twitch->connection = $connection;
168-
$twitch->initIRC($twitch->connection);
169-
170-
$connection->on('data', function($data) use ($connection, $twitch) {
171-
$twitch->process($data, $twitch->connection);
172-
});
173-
$twitch->emit('[CONNECTED]');
174-
},
175-
function (Exception $exception) {
176-
$twitch->emit('[ERROR] ' . $exception->getMessage());
177-
}
178-
);
179+
if(!$twitch->connection){
180+
$twitch = $this;
181+
$this->connector->connect("$url:$port")->then(
182+
function (ConnectionInterface $connection) use ($twitch) {
183+
$twitch->connection = $connection;
184+
$twitch->initIRC($twitch->connection);
185+
186+
$connection->on('data', function($data) use ($connection, $twitch) {
187+
$twitch->process($data, $twitch->connection);
188+
});
189+
$connection->on('close', function () use ($twitch) {
190+
$twitch->emit('[CLOSE]');
191+
});
192+
$twitch->emit('[CONNECTED]');
193+
},
194+
function (Exception $exception) {
195+
$twitch->emit('[ERROR] ' . $exception->getMessage());
196+
}
197+
);
198+
} else $twitch->emit('[SYMANTICS ERROR] A connection already exists!');
179199
}
180200
protected function initIRC(ConnectionInterface $connection): void
181201
{
@@ -288,6 +308,9 @@ protected function parseChannel(string $data): ?string
288308
return $channel;
289309
}
290310

311+
/*
312+
* This function can double as an event listener
313+
*/
291314
public function emit(string $string): void
292315
{
293316
echo "[EMIT] $string" . PHP_EOL;

0 commit comments

Comments
 (0)