|
|
@@ -1,75 +1,75 @@ |
|
|
|
|stub| Events |
|
|
|
============= |
|
|
|
Events |
|
|
|
====== |
|
|
|
|
|
|
|
Usage |
|
|
|
----- |
|
|
|
To take advantage of Events in Discord.Net, you need to hook into them. |
|
|
|
|
|
|
|
There are two ways of hooking into events. See the example for examples on using these events. |
|
|
|
|
|
|
|
Usable Events |
|
|
|
------------- |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| Event Name | EventArgs | Description | |
|
|
|
+====================+====================+==========================================+ |
|
|
|
| UserBanned | BanEventArgs | Called when a user is banned. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| UserUnbanned | BanEventArgs | Called when a user is unbanned. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| ChannelCreated | ChannelEventArgs | Called when a channel is created. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| ChannelDestroyed | ChannelEventArgs | Called when a channel is destroyed. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| ChannelUpdated | ChannelEventArgs | Called when a channel is updated. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| MessageReceived | MessageEventArgs | Called when a message is received. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| MessageSent | MessageEventArgs | Called when a message is sent. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| MessageDeleted | MessageEventArgs | Called when a message is deleted. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| MessageUpdated | MessageEventArgs | Called when a message is updated\\edited.| |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| MessageReadRemotely| MessageEventArgs | Called when a message is read. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| RoleCreated | RoleEventArgs | Called when a role is created. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| RoleUpdated | RoleEventArgs | Called when a role is updated. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| RoleDeleted | RoleEventArgs | Called when a role is deleted. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| JoinedServer | ServerEventArgs | Called when a member joins a server. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| LeftServer | ServerEventArgs | Called when a member leaves a server. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| ServerUpdated | ServerEventArgs | Called when a server is updated. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| ServerUnavailable | ServerEventArgs | Called when a Discord server goes down. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| ServerAvailable | ServerEventArgs |Called when a Discord server goes back up.| |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| UserJoined | UserEventArgs | Called when a user joins a Channel. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| UserLeft | UserEventArgs | Called when a user leaves a Channel. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| UserUpdated | UserEventArgs | --- | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| UserPresenceUpdated| UserEventArgs | Called when a user's presence changes. | |
|
|
|
| | | (Here\\Away) | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| UserVoiceState | UserEventArgs | Called when a user's voice state changes.| |
|
|
|
| Updated | | (Muted\\Unmuted) | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
|UserIsTypingUpdated | UserEventArgs | Called when a user starts\\stops typing. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| UserIsSpeaking | UserEventArgs | Called when a user's voice state changes.| |
|
|
|
| Updated | | (Speaking\\Not Speaking) | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
| ProfileUpdated | N/A | Called when a user's profile changes. | |
|
|
|
+--------------------+--------------------+------------------------------------------+ |
|
|
|
Example |
|
|
|
------- |
|
|
|
|
|
|
|
.. literalinclude:: /samples/events.cs |
|
|
|
:language: csharp6 |
|
|
|
:tab-width: 2 |
|
|
|
Events in Discord.NET are raised using the Event system in c#. Most events are raised on the ``DiscordClient`` class. |
|
|
|
|
|
|
|
Most events in Discord.NET explain theirselves by their name. |
|
|
|
|
|
|
|
Messages |
|
|
|
-------- |
|
|
|
|
|
|
|
The Four Message Events (MessageReceived, Updated, Deleted, and Acknowledged) are raised when a message has been modified/created. |
|
|
|
|
|
|
|
Example of MessageReceived: |
|
|
|
|
|
|
|
.. code-block:: c# |
|
|
|
|
|
|
|
// (Preface: Echo Bots are discouraged, make sure your bot is not running in a public server if you use them) |
|
|
|
|
|
|
|
// Hook into the MessageReceived event using a Lambda |
|
|
|
_client.MessageReceived += (s, e) => { |
|
|
|
// Check to make sure that the bot is not the author |
|
|
|
if (!e.Message.IsAuthor) |
|
|
|
// Echo the message back to the channel |
|
|
|
e.Channel.SendMessage(e.Message); |
|
|
|
}; |
|
|
|
|
|
|
|
Users |
|
|
|
----- |
|
|
|
|
|
|
|
There are Six User Events: |
|
|
|
|
|
|
|
UserBanned: A user has been banned from a Server |
|
|
|
UserUnbanned: A user was unbanned |
|
|
|
UserJoined: A user joins a server |
|
|
|
UserLeft: A user left (or was kicked) from a Server |
|
|
|
UserIsTyping: A user in a channel starts typing |
|
|
|
UserUpdated: A user object was updated. (caused by a presence update, role/permission change, or a voice state update) |
|
|
|
|
|
|
|
.. note:: |
|
|
|
UserUpdated Events include a ``User`` object for Before and After the change. |
|
|
|
When accessing the User, you should only use ``e.Before`` if comparing changes, otherwise use ``e.After`` |
|
|
|
|
|
|
|
Examples: |
|
|
|
|
|
|
|
.. code-block:: c# |
|
|
|
|
|
|
|
// Register a Hook into the UserBanned event using a Lambda |
|
|
|
_client.UserBanned += (s, e) => { |
|
|
|
// Create a Channel object by searching for a channel named '#logs' on the server the ban occurred in. |
|
|
|
var logChannel = e.Server.FindChannels("logs").FirstOrDefault(); |
|
|
|
// Send a message to the server's log channel, stating that a user was banned. |
|
|
|
logChannel.SendMessage($"User Banned: {e.User.Name}") |
|
|
|
}; |
|
|
|
|
|
|
|
// Register a Hook into the UserUpdated event using a Lambda |
|
|
|
_client.UserUpdated += (s, e) => { |
|
|
|
// Check that the user is in a Voice channel |
|
|
|
if (e.After.VoiceChannel == null) return; |
|
|
|
|
|
|
|
// See if they changed Voice channels |
|
|
|
if (e.Before.VoiceChannel == e.After.VoiceChannel) return; |
|
|
|
|
|
|
|
// do something... |
|
|
|
}; |
|
|
|
|
|
|
|
Connection States |
|
|
|
----------------- |
|
|
|
|
|
|
|
Connection Events will be raised when the Connection State of your client changes. |
|
|
|
|
|
|
|
.. warning:: |
|
|
|
You should not use DiscordClient.Connected to run code when your client first connects to Discord. |
|
|
|
If you lose connection and automatically reconnect, this code will be ran again, which may lead to unexpected behavior. |