Getting Started¶

Requirements¶

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.

Installation¶

You can get Discord.Net from NuGet:

If you have trouble installing from NuGet, try installing dependencies manually.

You can also pull the latest source from GitHub

Async¶

Discord.Net uses C# tasks extensively - nearly all operations return one. It is highly recommended that these tasks be awaited whenever possible. To do so requires the calling method be marked as async, which can be problematic in a console application. An example of how to get around this is provided below.

For more information, go to MSDN’s Await-Async section.

Example¶

class Program
{
  static void Main(string[] args)
  {
    var client = new DiscordClient();

    //Display all log messages in the console
    client.LogMessage += (s, e) => Console.WriteLine($"[{e.Severity}] {e.Source}: {e.Message}");

    //Echo back any message received, provided it didn't come from the bot itself
    client.MessageReceived += async (s, e) =>
    {
      if (!e.Message.IsAuthor)
        await e.Channel.SendMessage(e.Message.Text);
    };

    //Convert our sync method to an async one and block the Main function until the bot disconnects
    client.ExecuteAndWait(async () =>
    {
      //Connect to the Discord server using our email and password
      await client.Connect("discordtest@email.com", "Password123");

      //If we are not a member of any server, use our invite code (made beforehand in the official Discord Client)
      if (!client.Servers.Any())
        await client.AcceptInvite(client.GetInvite("aaabbbcccdddeee"));
    });
  }
}