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 3.1 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. Events
  2. ======
  3. Usage
  4. -----
  5. Messages from the Discord server are exposed via events on the DiscordClient class and follow the standard EventHandler<EventArgs> C# pattern.
  6. .. warning::
  7. Note that all synchronous code in an event handler will run on the gateway socket's thread and should be handled as quickly as possible.
  8. Using the async-await pattern to let the thread continue immediately is recommended and is demonstrated in the examples below.
  9. Ready
  10. -----
  11. The Ready Event is raised only once, when your client finishes processing the READY packet from Discord.
  12. This has replaced the previous "Connected" event, and indicates that it is safe to begin retrieving users, channels, or servers from the cache.
  13. Messages
  14. --------
  15. - MessageReceived, MessageUpdated and MessageDeleted are raised when a new message arrives, an existing one has been updated (by the user, or by Discord itself), or deleted.
  16. - MessageAcknowledged is only triggered in client mode, and occurs when a message is read on another device logged-in with your account.
  17. Example of MessageReceived:
  18. .. code-block:: csharp6
  19. // (Preface: Echo Bots are discouraged, make sure your bot is not running in a public server if you use them)
  20. // Hook into the MessageReceived event using a Lambda
  21. _client.MessageReceived += async (s, e) => {
  22. // Check to make sure that the bot is not the author
  23. if (!e.Message.IsAuthor)
  24. // Echo the message back to the channel
  25. await e.Channel.SendMessage(e.Message);
  26. };
  27. Users
  28. -----
  29. There are several user events:
  30. - UserBanned: A user has been banned from a server.
  31. - UserUnbanned: A user was unbanned.
  32. - UserJoined: A user joins a server.
  33. - UserLeft: A user left (or was kicked from) a server.
  34. - UserIsTyping: A user in a channel starts typing.
  35. - UserUpdated: A user object was updated (presence update, role/permission change, or a voice state update).
  36. .. note::
  37. UserUpdated Events include a ``User`` object for Before and After the change.
  38. When accessing the User, you should only use ``e.Before`` if comparing changes, otherwise use ``e.After``
  39. Examples:
  40. .. code-block:: csharp6
  41. // Register a Hook into the UserBanned event using a Lambda
  42. _client.UserBanned += async (s, e) => {
  43. // Create a Channel object by searching for a channel named '#logs' on the server the ban occurred in.
  44. var logChannel = e.Server.FindChannels("logs").FirstOrDefault();
  45. // Send a message to the server's log channel, stating that a user was banned.
  46. await logChannel.SendMessage($"User Banned: {e.User.Name}");
  47. };
  48. // Register a Hook into the UserUpdated event using a Lambda
  49. _client.UserUpdated += async (s, e) => {
  50. // Check that the user is in a Voice channel
  51. if (e.After.VoiceChannel == null) return;
  52. // See if they changed Voice channels
  53. if (e.Before.VoiceChannel == e.After.VoiceChannel) return;
  54. await logChannel.SendMessage($"User {e.After.Name} changed voice channels!");
  55. };