-
Notifications
You must be signed in to change notification settings - Fork 32
Getting Started with DiscordSharp
DiscordSharp is an unofficial API for interfacing with Discord. DiscordSharp was featured on the Discord Blog alongside all the libraries that are available in just about every language you could want. (Except C).
DiscordSharp will run in just about .Net language, not just C#. While all the code snippets will be shown in C# I have confirmed that DiscordSharp runs in Visual Basic and probably F# also (though I don't know F# to test).
Discord.Net is excellent and powerful. However, I created DiscordSharp for not only my own purposes but also for it to be a quick and dirty way to get into making Discord bots. I like to think of DiscordSharp as Visual Basic (a good starting point) and Discord.Net as C# (the step above, more powerful, but not always needed).
For quick and easy bots that do small things, DiscordSharp will be fine. If you want to get into more advanced features, such as voice, Discord.Net is the library for you.
Previously, you would need to clone the GitHub repo and compile DiscordSharp from source. Now however, it's super easy to get it running with NuGet.
To install DiscordSharp from NuGet, simply type the following command into the package manager console:
Install-Package DiscordSharp -pre
The -pre argument is necessary due to the pre-release nature of DiscordSharp. Rest assured though, many portions of DiscordSharp are stable (apparently just not websocket-sharp which DiscordSharp depends on).
From there, you're ready to rock!
DiscordSharp is what I like to an event-based API. Meaning that you get an instance of your DiscordClient and you hook up to its various events. The basic "hello world" of DiscordSharp would look a bit as follows.
public static void Main(string[] args)
{
DiscordClient client = new DiscordClient();
client.ClientPrivateInformation = new DiscordUserInformation();
client.ClientPrivateInformation.email = "botemail@bot.net";
client.ClientPrivateInformation.password = "cleverpassword123";
client.Connected += (sender, e) =>
{
Console.WriteLine($"Connected! User: {e.user.user.username}.");
};
client.SendLoginRequest();
Thread t = new Thread(client.Connect);
t.Start();
Console.ReadLine();
}This creates an instance of our DiscordClient, as client. Then, we create a new instance of DiscordUserInformation which contains vital information for our bot like its email and password. Then, we assign to it.
Things start to get a little saucy when you start using lambdas when hooking up to our client.Connected event. For more information on lambdas, read up here. All you need to know for now is that when the event, Connected is called, what's inside of that codeblock after the => will be executed. e in this case is a DiscordConnectEventArgs which simply houses a DiscordMember which is who we've logged in as. However, this is in the future.
Next, we actually do the logging in via the SendLoginRequest() function. In recent versions of DiscordSharp, despite the token being cached by the API you must still call this function to load the token from the token_cache file. In addition, if the token_cache file is deleted it will be recreated with a new login token.
Now, we create a Thread to handle the non-locking function Connect(). All you really need to know about that is that it's what makes DiscordSharp really tick. This handles websockets and actually initializing a connection to Discord. Once it's connected, the Connected event will fire off and you'll see that it prints the user that you logged in as! Great!
The basic way is just an infinite while loop. while(true);. Dead simple, it'll keep it running, but it'll hit your CPU just like this.
There's a few ways you can keep the console open and keep DiscordSharp running. The simplest way is just to simply add a reference to System.Windows.Forms and then calling System.Windows.Forms.Application.Run();. This basically just runs an empty System.Windows.Forms and keeps the console open without any CPU hit whatsoever. This is the best way if you're not accepting input from the console.
Finally, if you want to accept input from the console, you can run an InputCheck method after you start your websocket connection thread. In this thread, simply do a do while loop which is less CPU intensive than the standard while loop. The way I have mine setup is as follows:
string input;
do
{
input = Console.ReadLine();
//check input for commands
}(!string.IsNullOrEmpty(input));