You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

events.rst 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. Events
  2. ======
  3. Usage
  4. -----
  5. Events in Discord.NET are raised using the Event system in c#. Most events are raised on the ``DiscordClient`` class.
  6. Most events in Discord.NET explain theirselves by their name.
  7. Messages
  8. --------
  9. The Four Message Events (MessageReceived, Updated, Deleted, and Acknowledged) are raised when a message has been modified/created.
  10. Example of MessageReceived:
  11. .. code-block:: c#
  12. // (Preface: Echo Bots are discouraged, make sure your bot is not running in a public server if you use them)
  13. // Hook into the MessageReceived event using a Lambda
  14. _client.MessageReceived += (s, e) => {
  15. // Check to make sure that the bot is not the author
  16. if (!e.Message.IsAuthor)
  17. // Echo the message back to the channel
  18. e.Channel.SendMessage(e.Message);
  19. };
  20. Users
  21. -----
  22. There are Six User Events:
  23. UserBanned: A user has been banned from a Server
  24. UserUnbanned: A user was unbanned
  25. UserJoined: A user joins a server
  26. UserLeft: A user left (or was kicked) from a Server
  27. UserIsTyping: A user in a channel starts typing
  28. UserUpdated: A user object was updated. (caused by a presence update, role/permission change, or a voice state update)
  29. .. note::
  30. UserUpdated Events include a ``User`` object for Before and After the change.
  31. When accessing the User, you should only use ``e.Before`` if comparing changes, otherwise use ``e.After``
  32. Examples:
  33. .. code-block:: c#
  34. // Register a Hook into the UserBanned event using a Lambda
  35. _client.UserBanned += (s, e) => {
  36. // Create a Channel object by searching for a channel named '#logs' on the server the ban occurred in.
  37. var logChannel = e.Server.FindChannels("logs").FirstOrDefault();
  38. // Send a message to the server's log channel, stating that a user was banned.
  39. logChannel.SendMessage($"User Banned: {e.User.Name}")
  40. };
  41. // Register a Hook into the UserUpdated event using a Lambda
  42. _client.UserUpdated += (s, e) => {
  43. // Check that the user is in a Voice channel
  44. if (e.After.VoiceChannel == null) return;
  45. // See if they changed Voice channels
  46. if (e.Before.VoiceChannel == e.After.VoiceChannel) return;
  47. // do something...
  48. };
  49. Connection States
  50. -----------------
  51. Connection Events will be raised when the Connection State of your client changes.
  52. .. warning::
  53. You should not use DiscordClient.Connected to run code when your client first connects to Discord.
  54. If you lose connection and automatically reconnect, this code will be ran again, which may lead to unexpected behavior.