Last active
September 16, 2016 10:16
-
-
Save GeertHauwaerts/059adbc38fbac317b78c5a3a886c5562 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/** | |
* team-reflex/discord-php example. | |
* | |
* This is a simple example use of team-reflex/discord-php :) | |
* | |
* @author Geert Hauwaerts <geert@hauwaerts.be> | |
* @copyright Copyright (c) Geert Hauwaerts | |
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License, version 3 | |
*/ | |
/** | |
* Load the required namespaces. | |
*/ | |
use Discord\Discord; | |
use Discord\Parts\User\Game; | |
use Discord\Parts\Channel\Channel; | |
/** | |
* Load the composer libraries. | |
*/ | |
require __DIR__ . '/../vendor/autoload.php'; | |
/** | |
* Discord configuration. | |
*/ | |
$discord_apikey = 'my-key'; | |
$discord_server = 'my-server'; | |
$discord_channel = 'my-channel'; | |
$discord_game = 'with kittens!'; | |
$discord_avatar = __DIR__ . '/' . 'my-avatar.png'; | |
/** | |
* Enable integration with Sentry. (https://getsentry.com/) | |
*/ | |
#$sentry = new Raven_Client('my-key', [ | |
# 'release' => 'my-version', | |
#]); | |
#$error_handler = new Raven_ErrorHandler($sentry); | |
#$error_handler->registerExceptionHandler(); | |
#$error_handler->registerErrorHandler(); | |
#$error_handler->registerShutdownFunction(); | |
/** | |
* Create a new Discord bot. | |
*/ | |
$discord = new Discord(['token' => $discord_apikey); | |
/** | |
* Event hander: connected. | |
*/ | |
$discord->on('ready', function ($discord) { | |
$server = false; | |
$channel = false; | |
/* | |
* Find our server. | |
*/ | |
foreach ($discord->guilds as $s) { | |
if ($s->name === $discord_server) { | |
$server = $s; | |
} | |
} | |
if (!$server) { | |
die("Unable to find to the server '" . $discord_server . "'"); | |
} | |
/* | |
* Find our text channel. | |
*/ | |
foreach ($server->channels as $c) { | |
if (($c->type === Channel::TYPE_TEXT) && ($c->name === $discord_channel)) { | |
$channel = $c; | |
} | |
} | |
if (!$channel) { | |
die("Unable to find to the channel '" . $discord_channel . "'"); | |
} | |
/* | |
* Configure our presence. | |
*/ | |
$game = $discord->factory(Game::class, [ | |
'name' => $discord_game, | |
]); | |
$discord->updatePresence($game, false); | |
$discord->setAvatar($discord_avatar); | |
/* | |
* Example of a a looped action. | |
*/ | |
$discord->loop->addPeriodicTimer(10, function () use ($channel) { | |
foreach (['one', 'two', 'three'] as $msg) { | |
$channel->sendMessage($msg); | |
} | |
}); | |
/* | |
* Example of an '@botname command'. | |
*/ | |
$discord->on(Event::MESSAGE_CREATE, function ($message, $discord) { | |
if (preg_match('/^<@' . $discord->id . '> hello/i', $message->content)) { | |
$message->reply("(╯°□°)╯ hi there!"); | |
return true; | |
} | |
}); | |
/* | |
* Example of an '!command'. | |
*/ | |
$discord->on(Event::MESSAGE_CREATE, function ($message, $discord) { | |
if ($message->content === '!hello') { | |
$message->reply("(╯°□°)╯ hi there!"); | |
return true; | |
} | |
}); | |
/* | |
* Example logging of all text. | |
*/ | |
$discord->on(Event::MESSAGE_CREATE, function ($message, $discord) { | |
echo '[' . date('Y-m-d H:i:s') . '] ' . $message->content . "\n"; | |
}); | |
}); | |
/** | |
* Handle all Discord event errors. | |
*/ | |
$discord->on('error', function ($error) { | |
die($error); | |
}); | |
/** | |
* Start the Discord bot. | |
*/ | |
$discord->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment