Messages from the Discord server are exposed via events on the DiscordClient class and follow the standard EventHandler<EventArgs> C# pattern.
.. warning::
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.
Using the async-await pattern to let the thread continue immediately is recommended and is demonstrated in the examples below.
Connection State
----------------
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.
Messages
--------
- 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.
- MessageAcknowledged is only triggered in client mode, and occurs when a message is read on another device logged-in with your account.
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 += async (s, e) => {
// Check to make sure that the bot is not the author
if (!e.Message.IsAuthor)
// Echo the message back to the channel
await e.Channel.SendMessage(e.Message);
};
Users
-----
There are several 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 (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 += async (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.
Discord.Net will log all of its events/exceptions using a built-in LogManager.
This LogManager can be accessed through DiscordClient.Log
Usage
-----
To handle Log Messages through Discord.Net's Logger, you must hook into the Log.Message<LogMessageEventArgs> Event.
The LogManager does not provide a string-based result for the message, you must put your own message format together using the data provided through LogMessageEventArgs
See the Example for a snippet of logging.
Logging Your Own Data
---------------------
The LogManager included in Discord.Net can also be used to log your own messages.
You can use DiscordClient.Log.Log(LogSeverity, Source, Message, Exception), or one of the shortcut helpers, to log data.
Example:
.. code-block:: c#
_client.MessageReceived += async (s, e) {
// Log a new Message with Severity Info, Sourced from 'MessageReceived', with the Message Contents.
Using this library requires you to state the intention of the program using it.
By default, the library assumes your application is a bot or otherwise automated program, and locks access to certain client-only features.
As we approach the official API, Discord will be creating a divide between bots and clients, so it's important to use the mode appropriate for your program to minimize breaking changes!
.. warning::
This is not a complete list, new features will be added in the future.
There are two types of permissions: *Channel Permissions* and *Server Permissions*.
Channel Permissions
-------------------
Channel Permissions have a set of bools behind them:
Channel Permissions are controlled using a set of flags:
======================= ======= ==============
Flag Type Description
@@ -49,7 +49,7 @@ Otherwise, you can use a single DualChannelPermissions.
Server Permissions
------------------
Server permisisons are read-only, you cannot change them. You may still access them, however, using User.GetServerPermissions();
Server Permissions can be accessed by ``Server.GetPermissions(User)``, and updated with ``Server.UpdatePermissions(User, ServerPermissions)``
A user's server permissions also contain the default values for it's channel permissions, so the channel permissions listed above are also valid flags for Server Permissions. There are also a few extra Server Permissions:
@@ -57,7 +57,7 @@ A user's server permissions also contain the default values for it's channel per
Flag Type Description
======================= ======= ==============
BanMembers Server Ban users from the server.
KickMembers Server Kick users from the server. They can stil rejoin.
KickMembers Server Kick users from the server. They can still rejoin.
ManageRoles Server Manage roles on the server, and their permissions.
ManageChannels Server Manage channels that exist on the server (add, remove them)
ManageServer Server Manage the server settings.
@@ -69,7 +69,7 @@ Managing permissions for roles is much easier than for users in channels. For ro
Discord.Net currently requires logging in with a claimed account - anonymous logins are not supported. You can `register for a Discord account here`.
Discord.Net currently requires logging in with a claimed account - anonymous logins are not supported. You can `register for a Discord account here`_.
New accounts are also useless when not connected to a server, so you should create an invite code for whatever server you intend to test on using the official Discord client.
@@ -17,12 +17,16 @@ You can get Discord.Net from NuGet:
* `Discord.Net`_
* `Discord.Net.Commands`_
* `Discord.Net.Modules`_
If you have trouble installing from NuGet, try installing dependencies manually.
You can also pull the latest source from `GitHub`_
Discord.Net is an unofficial C# wrapper around the `Discord chat service`.
Discord.Net is an unofficial C# wrapper around the `Discord Chat Service`.
It offers several methods to create automated operations, bots, or even custom clients.
Feel free to join us in the `Discord API chat`_.
@@ -9,28 +9,29 @@ Feel free to join us in the `Discord API chat`_.
.. _Discord chat service: https://discordapp.com
.. _Discord API chat: https://discord.gg/0SBTUU1wZTVjAMPx
Warning
-------
.. warn::
This is an alpha!
This is a beta!
This library has been built thanks to a community effort reverse engineering the Discord client.
As Discord is still in alpha, it may change at any time without notice, breaking this library as well.
Discord.Net itself is also in early development and you will often encounter breaking changes until the official Discord API is released.
This library has been built thanks to a community effort reverse engineering the Discord client.
As the API is still unofficial, it may change at any time without notice, breaking this library as well.
Discord.Net itself is still in development (and is currently undergoing a rewrite) and you may encounter breaking changes throughout development until the official Discord API is released.
It is highly recommended that you always use the latest version and please report any bugs you find to our `Discord chat`_.
/// <summary> Max time in milliseconds to wait for DiscordAudioClient to connect and initialize. </summary>
public int ConnectionTimeout { get { return _connectionTimeout; } set { SetValue(ref _connectionTimeout, value); } }
private int _connectionTimeout = 30000;
@@ -33,8 +35,8 @@ namespace Discord.Audio
public int BufferLength { get { return _bufferLength; } set { SetValue(ref _bufferLength, value); } }
private int _bufferLength = 1000;
/// <summary> Gets or sets the bitrate used (in kbit/s, between 1 and 512 inclusively) for outgoing voice packets. A null value will use default Opus settings. </summary>
public int? Bitrate { get { return _bitrate; } set { SetValue(ref _bitrate, value); } }
/// <summary> Gets or sets the bitrate used (in kbit/s, between 1 and MaxBitrate inclusively) for outgoing voice packets. A null value will use default Opus settings. </summary>
public int? Bitrate { get { return _bitrate; } set { SetValue(ref _bitrate, value); } }
private int? _bitrate = null;
/// <summary> Gets or sets the number of channels (1 or 2) used in both input provided to IAudioClient and output send to Discord. Defaults to 2 (stereo). </summary>
public int Channels { get { return _channels; } set { SetValue(ref _channels, value); } }
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.