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

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