basics.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ======
  2. Basics
  3. ======
  4. First step is to include the Composer autoload file and `import <https://www.php.net/manual/en/language.namespaces.importing.php>`_ any required classes.
  5. .. code:: php
  6. <?php
  7. use Discord\Discord;
  8. use Discord\WebSockets\Intents;
  9. use Discord\WebSockets\Event;
  10. include __DIR__.'/vendor/autoload.php';
  11. The Discord instance can be set up with an array of options. All are optional except for token:
  12. .. code:: php
  13. $discord = new Discord([
  14. 'token' => 'Your-Token-Here',
  15. 'intents' => [
  16. Intents::GUILDS, Intents::GUILD_BANS, // ...
  17. ],
  18. // or
  19. 'intents' => 12345,
  20. // or
  21. 'intents' => Intents::getDefaultIntents() | Intents::GUILD_MEMBERS, // default intents as well as guild members
  22. 'loadAllMembers' => false,
  23. 'storeMessages' => false,
  24. 'retrieveBans' => false,
  25. 'pmChannels' => false,
  26. 'disabledEvents' => [
  27. Event::MESSAGE_CREATE, Event::MESSAGE_DELETE, // ...
  28. ],
  29. 'loop' => \React\EventLoop\Factory::create(),
  30. 'logger' => new \Monolog\Logger('New logger'),
  31. 'dnsConfig' => '1.1.1.1',
  32. 'shardId' => 0,
  33. 'shardCount' => 5,
  34. ]);
  35. ``token`` is your Discord token. **Required**.
  36. ``intents`` can be an array of valid intents *or* an integer representing the intents. Default is all intents minus any privileged intents. At the moment this means all intents minus ``GUILD_MEMBERS`` and ``GUILD_PRESENCES``. To enable these intents you must first enable them in your Discord developer portal.
  37. ``loadAllMembers`` is a boolean whether all members should be fetched and stored on bot start. Loading members takes a while to retrieve from Discord and store, so default is false. This requires the ``GUILD_MEMBERS`` intent to be enabled in DiscordPHP. See above for more details.
  38. ``storeMessages`` is a boolean whether messages received and sent should be stored. Default is false.
  39. ``retrieveBans`` is a boolean whether bans should be retrieved on bot load. Default is false.
  40. ``pmChannels`` is a boolean whether PM channels should be stored on bot load. Default is false.
  41. ``disabledEvents`` is an array of events that will be disabled. By default all events are enabled.
  42. ``loop`` is an instance of a ReactPHP event loop that can be provided to the client rather than creating a new loop. Useful if you want to use other React components. By default, a new loop is created.
  43. ``logger`` is an instance of a logger that implements ``LoggerInterface``. By default, a new Monolog logger with log level DEBUG is created to print to stdout.
  44. ``dnsConfig`` is an instace of ``Config`` or a string of name server address. By default system setting is used and fall back to 8.8.8.8 when system configuration is not found. Currently only used for VoiceClient.
  45. ----
  46. The following options should only be used by large bots that require sharding. If you plan to use sharding, `read up <https://discord.com/developers/docs/topics/gateway#sharding>`_ on how Discord implements it.
  47. ``shardId`` is the ID of the bot shard.
  48. ``shardCount`` is the number of shards that you are using.
  49. ----
  50. Gateway events should be registered inside the ``ready`` event, which is emitted once when the bot first starts and has connected to the gateway.
  51. To register an event we use the ``$discord->on(...)`` function, which registers a handler. A list of events is available `here <https://github.com/discord-php/DiscordPHP/blob/master/src/Discord/WebSockets/Event.php#L30-L75>`_. They are described in more detail in further sections of the documentation. All events take a callback which is called when the event is triggered, and the callback is called with an object representing the content of the event and an instance of the ``Discord`` client.
  52. .. code:: php
  53. $discord->on('ready', function (Discord $discord) {
  54. $discord->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) {
  55. // ... handle message sent
  56. });
  57. });
  58. Finally, the event loop needs to be started. Treat this as an infinite loop.
  59. .. code:: php
  60. $discord->run();
  61. If you want to stop the bot you can run:
  62. .. code:: php
  63. $discord->close();
  64. If you want to stop the bot without stopping the event loop, the close function takes a boolean:
  65. .. code:: php
  66. $discord->close(false);