Skip to content
This repository was archived by the owner on Apr 2, 2022. It is now read-only.

Commit d419787

Browse files
Move some code to externals, tidy up error messages
1 parent 7218c25 commit d419787

File tree

4 files changed

+89
-76
lines changed

4 files changed

+89
-76
lines changed

globals.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
$global_last_message = null;
4+
5+
/* Updates immediately, doesnt matter if it spams botnix, it can take it ;-) */
6+
$last_update_status = time() - 600;
7+
8+
/* First update an hour from now, prevents spamming the API if the bot is in a short crashloop */
9+
$last_botsite_webapi_update = time() + 3600;
10+
11+
/* Current IRC nickname reported from botnix core, used to address bot via telnet session */
12+
$ircnick = "";
13+
14+
/* Live config should be in the directory above this one. It's like this to make sure you never accidentally commit it to a repository. */
15+
$config = parse_ini_file("../botnix-discord.ini");
16+
17+
$randoms = [];
18+
19+
$conn = mysqli_connect($config['dbhost'], $config['dbuser'], $config['dbpass']);
20+
mysqli_select_db($conn, $config['db']);

help.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function GetHelp($section, $botusername, $botid, $author = "") {
88
$content = str_replace(":author:", $author, $content);
99
return json_decode($content);
1010
} else {
11-
return ["color"=>0xff0000, "description"=>"Missing help file '$section.json'!"];
11+
return ["color"=>0xff0000, "description"=>"No such help section '$section'!"];
1212
}
1313
}
1414

infobot.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/* Connect to the bot via its telnet port over localhost for remote control */
3+
function infobot($user, $content, $randomnick = "")
4+
{
5+
global $config;
6+
global $ircnick;
7+
/* NO, you can't configure any host other than localhost, as this connection is plaintext! */
8+
$fp = fsockopen('localhost', $config['telnetport'], $errno, $errstr, 1);
9+
if (!$fp) {
10+
print $errno . ": " . $errstr . "\n";
11+
return "*NOTHING*";
12+
} else {
13+
/* Note: This uses the .DR command specific to the infobot.pm module. We only want infobot available on discord,
14+
* all the IRC admin/moderation stuff is useless on discord and may introduce problems.
15+
*/
16+
$userprompt = fgets($fp);
17+
fwrite($fp, $config['telnetuser']."\n");
18+
$passprompt = fgets($fp);
19+
fwrite($fp, $config['telnetpass']."\n");
20+
$welcomeprompt = fgets($fp);
21+
if ($randomnick != "") {
22+
fwrite($fp, ".RN $randomnick\n");
23+
$response = fgets($fp);
24+
}
25+
$user = preg_replace("/\s+/", "_", $user);
26+
fwrite($fp, ".DR $user $content\n");
27+
$response = fgets($fp);
28+
$netdata = fgets($fp);
29+
if (preg_match("/nick => '(.+?)'/", $netdata, $match)) {
30+
if ($match[1] != "") {
31+
$ircnick = $match[1];
32+
}
33+
}
34+
fclose($fp);
35+
list($found, $response) = explode(' ', $response, 2);
36+
return [$found, str_replace("_", " ", $response)];
37+
}
38+
}

run.php

+30-75
Original file line numberDiff line numberDiff line change
@@ -15,72 +15,15 @@
1515
*/
1616

1717
include __DIR__.'/vendor/autoload.php';
18+
include __DIR__.'/globals.php';
1819
include __DIR__.'/apiupdate.php';
1920
include __DIR__.'/help.php';
2021
include __DIR__.'/settings.php';
22+
include __DIR__.'/infobot.php';
2123

2224
use Discord\Parts\User\Game;
2325
use Discord\Parts\User\User;
2426

25-
/* Updates immediately, doesnt matter if it spams botnix, it can take it ;-) */
26-
$last_update_status = time() - 600;
27-
28-
/* First update an hour from now, prevents spamming the API if the bot is in a short crashloop */
29-
$last_botsite_webapi_update = time() + 3600;
30-
31-
/* Current IRC nickname reported from botnix core, used to address bot via telnet session */
32-
$ircnick = "";
33-
34-
/* Live config should be in the directory above this one. It's like this to make sure you never accidentally commit it to a repository. */
35-
$config = parse_ini_file("../botnix-discord.ini");
36-
37-
$randoms = [];
38-
39-
$global_found = 1;
40-
41-
$conn = mysqli_connect($config['dbhost'], $config['dbuser'], $config['dbpass']);
42-
mysqli_select_db($conn, $config['db']);
43-
44-
/* Connect to the bot via its telnet port over localhost for remote control */
45-
function sporks($user, $content, $randomnick = "")
46-
{
47-
global $config;
48-
global $ircnick;
49-
global $global_found;
50-
/* NO, you can't configure any host other than localhost, as this connection is plaintext! */
51-
$fp = fsockopen('localhost', $config['telnetport'], $errno, $errstr, 1);
52-
if (!$fp) {
53-
print $errno . ": " . $errstr . "\n";
54-
return "*NOTHING*";
55-
} else {
56-
/* Note: This uses the .DR command specific to the infobot.pm module. We only want infobot available on discord,
57-
* all the IRC admin/moderation stuff is useless on discord and may introduce problems.
58-
*/
59-
$userprompt = fgets($fp);
60-
fwrite($fp, $config['telnetuser']."\n");
61-
$passprompt = fgets($fp);
62-
fwrite($fp, $config['telnetpass']."\n");
63-
$welcomeprompt = fgets($fp);
64-
if ($randomnick != "") {
65-
fwrite($fp, ".RN $randomnick\n");
66-
$response = fgets($fp);
67-
}
68-
$user = preg_replace("/\s+/", "_", $user);
69-
fwrite($fp, ".DR $user $content\n");
70-
$response = fgets($fp);
71-
$netdata = fgets($fp);
72-
if (preg_match("/nick => '(.+?)'/", $netdata, $match)) {
73-
if ($match[1] != "") {
74-
$ircnick = $match[1];
75-
}
76-
}
77-
fclose($fp);
78-
list($found, $response) = explode(' ', $response, 2);
79-
$global_found = $found;
80-
return str_replace("_", " ", $response);
81-
}
82-
}
83-
8427
/* Connect to discord */
8528
$discord = new \Discord\Discord([
8629
'token' => $config['token'],
@@ -90,7 +33,7 @@ function sporks($user, $content, $randomnick = "")
9033
]);
9134

9235
/* Discord API connection ready to serve! */
93-
$discord->on('ready', function ($discord) {
36+
$discord->on('ready', function ($discord) use ($global_last_message) {
9437
echo "Bot '$discord->username#$discord->discriminator' ($discord->id) is ready.", PHP_EOL;
9538
$countdetails = count_guilds($discord);
9639
echo "Present on " . $countdetails['guild_count'] . " servers with a total of " . $countdetails['member_count'] . " members\n";
@@ -103,7 +46,6 @@ function sporks($user, $content, $randomnick = "")
10346
global $ircnick;
10447
global $config;
10548
global $randoms;
106-
global $global_found;
10749

10850
// Grab from global, because discordphp strips it out!
10951
$author = $global_last_message->d->author;
@@ -133,7 +75,7 @@ function sporks($user, $content, $randomnick = "")
13375
if (time() - $last_update_status > 120) {
13476
$last_update_status = time();
13577
echo "Running presence update\n";
136-
$info = sporks("Self", $ircnick . " status");
78+
list($found,$info) = infobot("Self", $ircnick . " status");
13779
$countdetails = count_guilds($discord);
13880
preg_match('/^Since (.+?), there have been (\d+) modifications and (\d+) questions. I have been alive for (.+?), I currently know (\d+)/', $info, $matches);
13981
$game = $discord->factory(Game::class, [
@@ -214,7 +156,7 @@ function sporks($user, $content, $randomnick = "")
214156
}
215157
}
216158
if (!$access) {
217-
$message->channel->sendMessage("Sorry, <@" . $author->id . ">, you require the \"manage messages\" permission to alter configuration for <#".$global_last_message->d->channel_id.">");
159+
$message->channel->sendMessage("Sorry, <@" . $author->id . ">, you must be in a role with the \"manage messages\" permission to alter configuration for <#".$global_last_message->d->channel_id."> (or be server owner or an administrator)");
218160
return;
219161
}
220162
switch ($params[2]) {
@@ -255,15 +197,15 @@ function sporks($user, $content, $randomnick = "")
255197
}
256198
break;
257199
case 'ignore':
258-
$add_or_del = $params[3];
259-
$userid = (strtolower($add_or_del) == 'add' || strtolower($add_or_del) == 'del') ? $params[4] : '';
200+
$add_or_del = strtolower($params[3]);
201+
$userid = ($add_or_del == 'add' || $add_or_del == 'del') ? $params[4] : '';
260202
if (preg_match('/<@(\d+)>/', $userid, $usermatch) || strtolower($add_or_del) == 'list') {
261203
$userid = sizeof($usermatch) > 1 ? $usermatch[1] : '';
262204
$user_array = [];
263205
if (isset($chanconfig['ignores']) && is_array($chanconfig['ignores'])) {
264206
$user_array = $chanconfig['ignores'];
265207
}
266-
if (strtolower($add_or_del) == 'add') {
208+
if ($add_or_del == 'add') {
267209
if ($userid == $discord->id) {
268210
$message->channel->sendMessage("", false, ["color"=>0xffda00,"description"=>"Confucious say, only an idiot would ignore their inner voice..."]);
269211
return;
@@ -275,25 +217,36 @@ function sporks($user, $content, $randomnick = "")
275217
} else {
276218
$message->channel->sendMessage("", false, ["color"=>0xffda00,"description"=>"You can't add an ignore on yourself on <#" . $global_last_message->d->channel_id .">!"]);
277219
}
278-
} else if (strtolower($add_or_del) == 'del') {
220+
} else if ($add_or_del == 'del') {
279221
if (($key = array_search($userid, $user_array)) !== false) {
280222
unset($user_array[$key]);
281223
setSettings($global_last_message->d->channel_id, "ignores", $user_array);
282224
$message->channel->sendMessage("", false, ["color"=>0xffda00,"description"=>"User <@".$userid."> removed from ignore list on <#" . $global_last_message->d->channel_id .">"]);
283225
} else {
284226
$message->channel->sendMessage("", false, ["color"=>0xffda00,"description"=>"User <@".$userid."> does not exist on ignore list on <#" . $global_last_message->d->channel_id .">!"]);
285227
}
286-
} else if (strtolower($add_or_del) == 'list') {
287-
$descr = "**Ignore list for <#" . $global_last_message->d->channel_id .">**\r\n\r\n";
288-
foreach ($user_array as $user_id) {
289-
$descr .= "<@" . $user_id . "> ($user_id)\r\n";
228+
} else if ($add_or_del == 'list') {
229+
if (count($user_array)) {
230+
$descr = "**Ignore list for <#" . $global_last_message->d->channel_id .">**\r\n\r\n";
231+
foreach ($user_array as $user_id) {
232+
$descr .= "<@" . $user_id . "> ($user_id)\r\n";
233+
}
234+
} else {
235+
$descr = "**Ignore list for <#" . $global_last_message->d->channel_id .">** is **empty**!";
290236
}
291237
$message->channel->sendMessage("", false, ["color"=>0xffda00,"description"=>$descr]);
292238
}
293239
} else {
294-
$message->channel->sendMessage("", false, ["color"=>0xffda00,"description"=>"User to add or delete on <#" . $global_last_message->d->channel_id ."> must be referred to as a metion"]);
240+
if ($add_or_del != 'add' && $add_or_del != 'del' && $add_or_del != 'list') {
241+
$message->channel->sendMessage("", false, ["color"=>0xff0000, "description"=>"Invalid config ignore command '**$params[3]**', should be '**add**', '**del**' or '**list**'"]);
242+
} else {
243+
$message->channel->sendMessage("", false, ["color"=>0xffda00,"description"=>"User to add or delete on <#" . $global_last_message->d->channel_id ."> must be referred to as a metion"]);
244+
}
295245
}
296246
break;
247+
default:
248+
$message->channel->sendMessage("", false, ["color"=>0xff0000, "description"=>"Invalid config command '**$params[2]**', should be '**set**', '**show**' or '**ignore**'"]);
249+
break;
297250
}
298251
return;
299252
}
@@ -327,9 +280,11 @@ function sporks($user, $content, $randomnick = "")
327280
}
328281
}
329282

283+
$found = 0;
284+
330285
/* When learning is not disabled, bot is always learning */
331286
if (!$learningdisabled) {
332-
$reply = sporks($author->username, $content, $randomnick);
287+
list($found,$reply) = infobot($author->username, $content, $randomnick);
333288
$reply = trim(preg_replace('/\r|\n/', '', $reply));
334289
}
335290

@@ -338,7 +293,7 @@ function sporks($user, $content, $randomnick = "")
338293

339294
/* When learning is disabled, bot must be directly addressed to learn a new fact */
340295
if ($learningdisabled) {
341-
$reply = sporks($author->username, $content, $randomnick);
296+
list($found,$reply) = infobot($author->username, $content, $randomnick);
342297
$reply = trim(preg_replace('/\r|\n/', '', $reply));
343298
}
344299

@@ -376,7 +331,7 @@ function sporks($user, $content, $randomnick = "")
376331
$reply = str_replace($matches[0][$i], "<".$matches[0][$i].">", $reply);
377332
}
378333
}
379-
if ($talkative && !$global_found) {
334+
if ($talkative && !$found) {
380335
return;
381336
}
382337
$message->channel->sendMessage($reply);

0 commit comments

Comments
 (0)