starting to add commands to new bot setup

This commit is contained in:
Ben Harris 2017-04-09 20:21:55 -04:00
parent 9f9a653c70
commit 04f63e4641
14 changed files with 174 additions and 53 deletions

84
bot
View File

@ -3,57 +3,63 @@
pid=$(ps -ef | grep bot.php | grep -v grep | awk '{print $2}')
case "$1" in
nohup)
if [ "" == "$pid" ]; then
echo "starting bot in background (nohup)"
nohup php bot.php > bot.out 2> bot.err < /dev/null &
else
echo "bot already running"
fi
;;
start)
if [ "" != "$pid" ]; then
kill $pid
fi
php bot.php
;;
nohup)
if [ "" == "$pid" ]; then
echo "starting bot in background (nohup)"
nohup php bot.php > bot.out 2> bot.err < /dev/null &
else
echo "bot already running"
fi
;;
stop)
if [ "" != "$pid" ]; then
echo "killing bot ($pid)"
kill $pid
fi
;;
start)
if [ "" != "$pid" ]; then
kill $pid
fi
php bot.php
;;
status)
if [ "" != "$pid" ]; then
echo "bot running with pid $pid"
else
echo "bot not running"
fi
;;
stop)
if [ "" != "$pid" ]; then
echo "killing bot ($pid)"
kill $pid
fi
;;
status)
if [ "" != "$pid" ]; then
echo "bot running with pid $pid"
else
echo "bot not running"
fi
;;
log)
if [ "" != "$pid" ]; then
less bot.out
else
else
echo "bot not running"
fi
;;
restart)
if [ "" != "$pid" ]; then
echo "killing bot ($pid)"
kill $pid
fi
echo "starting bot in background (nohup)"
nohup php bot.php > bot.out 2> bot.err < /dev/null &
;;
check)
find ./src -name \*.php -exec php -l "{}" \;
php -l *.php
;;
restart)
if [ "" != "$pid" ]; then
echo "killing bot ($pid)"
kill $pid
fi
echo "starting bot in background (nohup)"
nohup php bot.php > bot.out 2> bot.err < /dev/null &
;;
*)
echo $"Usage: $0 {start|nohup|stop|restart|log|status}"
exit 1
*)
echo $"Usage: $0 {start|nohup|stop|restart|log|status}"
exit 1
esac

View File

@ -3,7 +3,7 @@
///////////////////////////////////////////////////////////
// config
///////////////////////////////////////////////////////////
error_reporting(E_ALL);
error_reporting(-1);
include __DIR__.'/vendor/autoload.php';

View File

@ -1,7 +1,7 @@
<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
error_reporting(-1);
if (!cli_set_process_title("BenBot")) {

View File

@ -1,5 +1,6 @@
<?php
namespace BenBot;
error_reporting(-1);
use Discord\Discord;
use Discord\Parts\User\Game;
@ -84,6 +85,7 @@ class BenBot extends Discord {
Utils::sendFile($msg, "{$this->dir}/uploaded_images/{$this->imgs[$cmd]}", $this->imgs[$cmd], $cmd);
}
// look up command
if (array_key_exists($cmd, $this->cmds)) {
$command = $this->cmds[$cmd];
@ -156,6 +158,7 @@ class BenBot extends Discord {
$this->registerAllCommands();
Utils::ping("bot started successfully");
echo PHP_EOL, "BOT STARTED SUCCESSFULLY", PHP_EOL;
});

View File

@ -1,5 +1,6 @@
<?php
namespace BenBot;
error_reporting(-1);
use BenBot\BenBot;
use Discord\Parts\Channel\Message;

18
src/Commands/Cities.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace BenBot\Commands;
error_reporting(-1);
class Cities {
private static $bot;
public static function register(&$that)
{
self::$bot = $that;
}
public static function saveCity($msg, $args)
{
}
}

View File

@ -1,5 +1,6 @@
<?php
namespace BenBot\Commands;
error_reporting(-1);
use Carbon\Carbon;

View File

@ -1,5 +1,6 @@
<?php
namespace BenBot\Commands;
error_reporting(-1);
class Fun {

View File

@ -1,5 +1,9 @@
<?php
namespace BenBot\Commands;
error_reporting(-1);
use BenBot\Utils;
class Jokes {
@ -8,27 +12,36 @@ class Jokes {
public static function register(&$that)
{
self::$bot = $that;
$joke = self::$bot->registerCommand('joke', [__CLASS__, 'joke'], [
'description' => 'tells a random joke',
'usage' => '',
'registerHelp' => true,
]);
$joke->registerSubCommand('chucknorris', [__CLASS__, 'chucknorris'], [
'description' => 'get a fact about chuck norris',
'aliases' => [
'chuck',
'norris',
],
]);
$joke->registerSubCommand('chucknorris', [__CLASS__, 'chucknorris'], [
'description' => 'get a fact about chuck norris',
'aliases' => [
'chuck',
'norris',
],
]);
$joke->registerSubCommand('yomama', [__CLASS__, 'yomama'], [
'description' => 'yo mama joke',
]);
$joke->registerSubCommand('yomama', [__CLASS__, 'yomama'], [
'description' => 'yo mama joke',
]);
$joke->registerSubCommand('dad', [__CLASS__, 'dad'], [
'description' => 'get a dad joke',
]);
echo __CLASS__ . " registered", PHP_EOL;
}
public static function joke($msg, $args)
{
return self::$bot->jokes[array_rand(self::$bot->jokes)];
@ -36,6 +49,7 @@ class Jokes {
public static function chucknorris($msg, $args)
{
echo "looking up chucknorris joke", PHP_EOL;
$url = "http://api.icndb.com/jokes/random1";
self::$bot->http->get($url, null, [], false)->then(function ($result) use ($msg) {
Utils::send($msg, $result->value->joke);
@ -49,4 +63,15 @@ class Jokes {
return self::$bot->yomamajokes[array_rand(self::$bot->yomamajokes)];
}
public static function dad($msg, $args)
{
echo "looking up dad joke", PHP_EOL;
$url = "https://icanhazdadjoke.com";
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());
});
}
}

View File

@ -1,5 +1,8 @@
<?php
namespace BenBot\Commands;
error_reporting(-1);
use BenBot\Utils;
class Misc {
@ -8,14 +11,51 @@ class Misc {
public static function register(&$that)
{
self::$bot = $that;
self::$bot->registerCommand('hi', [__CLASS__, 'hi'], [
'description' => 'just some greetings'
'description' => 'just wanted to say hi',
]);
self::$bot->registerCommand('text_benh', [__CLASS__, 'textBenh'], [
'description' => 'sends an SMS to benh',
'usage' => '<message>',
'aliases' => [
'textben',
],
'registerHelp' => true,
]);
echo __CLASS__ . " registered", PHP_EOL;
}
public static function hi($msg, $args)
{
return "hello {$msg->author}, how are you today?";
$greetings = [
"hello {$msg->author}, how are you today?",
"soup",
"hi!",
"henlo",
"hallo!",
"hola",
"wassup",
];
return $greetings[array_rand($greetings)];
}
public static function textBenh($msg, $args)
{
if (count($args) === 0) {
return "can't send a blank message, silly";
}
$server = $msg->channel->guild->name;
$user = Utils::isDM($msg) ? $msg->author->username : $msg->author->user->username;
$from = "From: {$server} Discord <{$server}@bot.benharris.ch>";
$msg_body = "$user:\n\n" . implode(" ", $args);
if (mail(getenv('PHONE_NUMBER')."@vtext.com", "", $msg_body, $from)) {
return "message sent to benh";
}
}
}

23
src/Commands/Weather.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace BenBot\Commands;
error_reporting(-1);
class Weather {
private static $bot;
public static function register(&$that)
{
self::$bot = $that;
$weather = self::$bot->registerCommand('weather', [__CLASS__, 'weather'], [
'description' => 'just some greetings'
]);
echo __CLASS__ . " registered", PHP_EOL;
}
public static function weather($msg, $args)
{
}
}

View File

@ -1,5 +1,6 @@
<?php
namespace BenBot;
error_reporting(-1);
use BenBot\Utils;
use function Stringy\create as s;

View File

@ -1,5 +1,6 @@
<?php
namespace BenBot;
error_reporting(-1);
use MessagePack\Packer;
use MessagePack\Unpacker;

View File

@ -1,5 +1,6 @@
<?php
namespace BenBot;
error_reporting(-1);
use Carbon\Carbon;
use Discord\Parts\Embed\Embed;