ping.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * Example Bot with Discord-PHP
  4. *
  5. * When an User says "ping", the Bot will reply "pong"
  6. *
  7. * Run this example bot from main directory using command:
  8. * php examples/ping.php
  9. */
  10. include __DIR__.'/../vendor/autoload.php';
  11. // Import classes, install a LSP such as Intelephense to auto complete imports
  12. use Discord\Discord;
  13. use Discord\Parts\Channel\Message;
  14. // Create a $discord BOT
  15. $discord = new Discord([
  16. 'token' => '', // Put your Bot token here from https://discord.com/developers/applications/
  17. ]);
  18. // When the Bot is ready
  19. $discord->on('ready', function (Discord $discord) {
  20. // Listen for messages
  21. $discord->on('message', function (Message $message, Discord $discord) {
  22. // If message is from a bot
  23. if ($message->author->bot) {
  24. // Do nothing
  25. return;
  26. }
  27. // If message is "ping"
  28. if ($message->content == 'ping') {
  29. // Reply with "pong"
  30. $message->reply('pong');
  31. }
  32. });
  33. });
  34. // Start the Bot (must be at the bottom)
  35. $discord->run();