generic error handler

This commit is contained in:
Ben Harris 2017-05-03 21:08:28 -04:00
parent ec1fcff534
commit 702d4dc4b8
6 changed files with 49 additions and 37 deletions

View File

@ -165,8 +165,8 @@ final class BenBot extends Discord
if (!Utils::isDM($msg) && $msg->channel->guild->id === "233603102047993856") {
if ($str->contains('dib', false)) {
$msg->react(":dib:284335774823088129")->otherwise(function ($e) {
echo $e->getMessage(), PHP_EOL;
$msg->react(":dib:284335774823088129")->otherwise(function ($e) use ($msg) {
Utils::logError($e, $msg);
});
}
}

View File

@ -275,9 +275,8 @@ final class Debug
self::$bot->loop->addTimer(2, function ($timer) use ($res) {
$res->channel->messages->delete($res);
});
}, function ($e) {
echo $e->getMessage(), PHP_EOL;
echo $e->getTraceAsString(), PHP_EOL;
}, function ($e) use ($msg) {
Utils::logError($e, $msg);
});
});
});

View File

@ -75,9 +75,8 @@ final class Hangman
'gameid' => $gameid,
'origmsg' => $msg,
];
$msg->author->user->sendMessage("enter the secret word")->otherwise(function ($e) {
echo $e->getMessage(), PHP_EOL;
echo $e->getTraceAsString(), PHP_EOL;
$msg->author->user->sendMessage("enter the secret word")->otherwise(function ($e) use ($msg) {
Utils::logError($e, $msg);
});
return "waiting for {$msg->author} to enter a word";
}

View File

@ -53,7 +53,7 @@ final class Jokes
self::$bot->http->get($url, null, [], false)->then(function ($result) use ($msg) {
Utils::send($msg, $result->value->joke);
}, function ($e) use ($msg) {
Utils::send($msg, $e->getMessage());
Utils::logError($e, $msg);
});
}
@ -69,7 +69,7 @@ final class Jokes
self::$bot->http->get($url, null, ['Accept' => 'application/json'], false)->then(function ($result) use ($msg) {
Utils::send($msg, $result->joke);
}, function ($e) use ($msg) {
Utils::send($msg, $e->getMessage());
Utils::logError($e, $msg);
});
}

View File

@ -81,21 +81,17 @@ final class Music
self::$voiceclients[$msg->channel->guild->id] = $vc;
$vc->playFile(self::$bot->dir . "/music/$file")->then(function () use ($vc) {
$vc->close();
}, function ($e) {
echo $e->getMessage(), PHP_EOL;
echo $e->getTraceAsString(), PHP_EOL;
}, function ($e) use ($msg) {
Utils::logError($e, $msg);
});
}, function ($e) {
echo $e->getMessage(), PHP_EOL;
echo $e->getTraceAsString(), PHP_EOL;
}, function ($e) use ($msg) {
Utils::logError($e, $msg);
});
}, function ($e) {
echo $e->getMessage(), PHP_EOL;
echo $e->getTraceAsString(), PHP_EOL;
}, function ($e) use ($msg) {
Utils::logError($e, $msg);
});
}, function ($e) {
echo $e->getMessage(), PHP_EOL;
echo $e->getTraceAsString(), PHP_EOL;
}, function ($e) use ($msg) {
Utils::logError($e, $msg);
});
});
}
@ -143,7 +139,7 @@ final class Music
if (strlen($args[0]) === 11) {
$cmd .= "https://www.youtube.com/watch?v={$args[0]}";
} elseif (strpos($args[0], "youtube.com") !== false) {
$cmd = $args[0];
$cmd .= $args[0];
} else {
$query = implode(" ", $args);
$cmd .= "'ytsearch:$query'";
@ -180,6 +176,11 @@ final class Music
$url = escapeshellarg($json->webpage_url);
$filename = $json->id . '-' . md5($json->title) . '-' . $json->duration;
if ($json->duration > 60 * 60) {
$deferred->reject("video too long, sorry");
return $deferred->promise();
}
foreach (scandir(self::$bot->dir . '/music') as $file) {
// check if we've already downloaded the file!
if (pathinfo($file, PATHINFO_FILENAME) === $filename) {

View File

@ -20,25 +20,38 @@ final class Utils
public static function send($msg, $txt, $embed = null)
{
return $msg->channel->sendMessage($txt, false, $embed)
->otherwise(function($e) use ($msg) {
echo $e->getMessage(), PHP_EOL;
echo $e->getTraceAsString(), PHP_EOL;
$msg->reply("sry, an error occurred. check with <@193011352275648514>.\n```{$e->getMessage()}```");
self::ping($e->getMessage());
});
return $msg->channel->sendMessage(
$txt,
false,
$embed
)->otherwise(function($e) use ($msg) {
self::logError($e, $msg);
});
}
public static function sendFile($msg, $filepath, $filename, $txt)
{
return $msg->channel->sendFile($filepath, $filename, $txt)
->otherwise(function($e) use ($msg) {
echo $e->getMessage(), PHP_EOL;
echo $e->getTraceAsString(), PHP_EOL;
$msg->reply("sry, an error occurred. check with <@193011352275648514>.\n```{$e->getMessage()}```");
self::ping($e->getMessage());
});
return $msg->channel->sendFile(
$filepath,
$filename,
$txt
)->otherwise(function($e) use ($msg) {
self::logError($e, $msg);
});
}
public static function logError($e, $msg = null)
{
echo $e->getMessage(), PHP_EOL;
echo $e->getTraceAsString(), PHP_EOL;
if ($msg != null) {
self::send($msg,
"sorry, an error occurred. check with <@193011352275648514>.\n```{$e->getMessage()}```"
);
self::ping("```{$e->getMessage()}```\nin {$msg->channel->name} in {$msg->channel->guild->name}.\ncheck the logs.");
}
}