C# (C-Sharp) is a general-purpose programming language that runs on the .NET Framework.

Today we’re going to cover the exact steps you’ll need to take to start making your very own Discord bot in C#.

Personally, I’ve always been a fan of C# and the .NET framework for quickly getting desktop projects up and running.

With an IDE like Visual Studio you can easily set up a project, create a GUI (Graphical User Interface that includes all of your application’s buttons, textboxes, etc.), assign events to your GUI, and start creating a fully functional application.

Let’s go over how you set up your IDE for C# bot development, and then cover the best Discord API libraries/wrappers available.

Choosing an IDE for C# Discord Bot Development

The first step we need to take is to choose a solid, reputable IDE for our C# development environment. Visual Studio and Visual Studio Code (there is a difference!) are both great for writing C# code. We’ll cover how to get both of these set up and configured for bot development.

Visual Studio: The most popular C# IDE (recommended)

Visual Studio Logo

Visual Studio, developed and maintained by Microsoft, is the go-to IDE when it comes to .NET programming.

There are both paid and free versions of VS available depending on use, but you can get the Community edition to start (just be sure to read their license limitations in detail).

If you’re just getting started with C# and find yourself using Windows as your primary operating system, I would highly recommend getting Visual Studio set up and ready to go. It’s the easiest, most “out of the box” IDE for C#.

If you use Mac or Linux as your go-to OS, read on to Visual Studio Code.

You can download Visual Studio from this link here.

Visual Studio Code: A great cross-platform C# editor

Visual Studio Code Logo

If you’re a Mac or Linux user, then, unfortunately, you won’t be able to use Visual Studio without an emulator or virtual machine (which might be more hassle than it’s worth to get configured.)

The easiest solution here is to use a cross-platform editor for C# development: Visual Studio Code. It works great out of the box on Mac OS, Linux, and Windows. 

You can Download Visual Studio Code for free and start coding.

Important: Both of the popular discord libraries/wrappers we’re using in this guide (Discord .NET and DSharpPlus) have issues with Mono (another cross-platform.NET framework). Do not use Mono for developing your Discord bot if you’re going to be using one of these libraries! Your bot will crash! As an alternative download .NET Core, which is also available cross-platform on both Linux and Mac OS

Once you have your IDE of choice downloaded and installed, it’s time to decide on a Discord API library/wrapper for your bot.

C# Discord API Libraries for Bot Development

Out of all the libraries/wrappers available for C# bot programming, we narrowed it down to the two that are most actively maintained: Discord .NET and DSharpPlus.

Both libraries are 100% open-source and viewable in their entirety on GitHub. Here’s the repository for Discord .NET and here’s the repository for DSharpPlus.

But which wrapper/library should I pick for my bot?

This is always a tough decision, so we’ll give you a few stats for comparison.

Discord .NET has very stable releases (lower frequency of releases) and a larger development community that keeps the library updated and maintained.

DSharpPlus, although it has a smaller development community, iterates fast and pushes out fixes and suggestions incredibly quick.

In my eyes, this is a choice between a library/wrapper with solid support and backing (Discord .NET) or a library/wrapper with quick development and bug fixes (DSharpPlus).

As of this guide’s publication, both development communities are very active, and both projects continue to be maintained.

Let’s walk through setting each of these up in your project.

Setting up Discord .NET and DSharpPlus for Bot Development

Discord .NET and DSharpPlus are both distributed through the NuGet Package Manager — this is great news!

If you’re not familiar with NuGet, it’s an easy way to add libraries into your project without going through the work of downloading them separately, copy/pasting them into your project folder, including them, etc.

Instead of manually adding dependencies to your project, NuGet lets you do it automatically through a simple add-on.

Visual Studio has NuGet built-in.
Visual Studio Code has an add-on available: NuGet Package Manager.

Discord .NET Setup

Discord .NET Logo

To install Discord .NET through the NuGet Package Manager, take a look at this guide on the Discord.NET Wiki.

There are installation instructions for Visual Studio and Visual Studio Code available on that wiki page:

Discord Bot Installing with NuGet

Once you have your IDE and Library/Wrapper configured, you can move on to the next section — actually coding and creating your own Discord bot in C#!

DSharpPlus Setup

D Sharp Plus (+)

To setup and configure DSharpPlus in Visual Studio or Visual Studio Code, follow along with this guide on the D#+ documentation page.

The guide above includes step by step instructions and screenshots for getting DSharpPlus added to your project through the NuGet Package Manager, and it even includes some basic code you can use to get a basic bot up and running quickly (to make sure your setup is correct).

Discord Bot Adding NuGet Package

Once DSharpPlus is configured, you’re ready to start coding!

Programming Your C# Discord Bot

At this point you should have your IDE installed and a library/wrapper ready to go and added to your project.

If you haven’t done that yet, you can do it now: Create a new project in Visual Studio / Visual Studio Code and add your library of choice to the project through the NuGet package manager.

If you’re using Discord .NET:

Follow along with this guide for creating your first, basic Discord bot.

Your code for a very basic bot will look something like this:

public class Program
{
	private DiscordSocketClient _client;
	
	public static void Main(string[] args)
		=> new Program().MainAsync().GetAwaiter().GetResult();

	public async Task MainAsync()
	{
		_client = new DiscordSocketClient();
		_client.Log += Log;
		await _client.LoginAsync(TokenType.Bot, 
			Environment.GetEnvironmentVariable("DiscordToken"));
		await _client.StartAsync();
		
		// Block this task until the program is closed.
		await Task.Delay(-1);
	}
	private Task Log(LogMessage msg)
	{
		Console.WriteLine(msg.ToString());
		return Task.CompletedTask;
	}
}

If you’re using DSharpPlus:

You can code along with this guide to get your bot up and running.

A very basic Discord bot will look like this:

class Program
    {
        static DiscordClient discord;

        static void Main(string[] args)
        {
            MainAsync(args).ConfigureAwait(false).GetAwaiter().GetResult();
        }

        static async Task MainAsync(string[] args)
        {
            discord = new DiscordClient(new DiscordConfiguration
            {
                Token = "<your token here>",
                TokenType = TokenType.Bot
            });

            discord.MessageCreated += async e =>
            {
                if (e.Message.Content.ToLower().StartsWith("ping"))
                    await e.Message.RespondAsync("pong!");
            };

            await discord.ConnectAsync();
            await Task.Delay(-1);
        }
    }

If you followed along with either of those guides linked above, you have successfully created your very first Discord Bot in C#. Congratulations!

And if you’re wondering what you should do now, we’ll cover the steps you should take next in the upcoming section.

Next Steps in C# Discord Bot Development

If you’ve gotten this far you know all of the steps required to make your very own Discord bot in the C# programming language. That’s a great accomplishment!

But what should you do next? How do you figure out the best path forward?

Well, before anything, we would strongly recommend that you read the “Discord Bot Ideas” section of this guide to get a good sense of the type of bot you’d like to create. Write down every bot idea that comes into your head.

It doesn’t even matter if it sounds like a bad idea right now, you want to get everything down and filter it out later.

After you’ve done that, I’d personally hop on an open source platform like GitHub, filter search results by programming language (C#), and start looking at all of the open source bots out there.

Looking at other people’s code is a great way to get ideas and see how others are structuring their code.

Your next steps should be to filter down your idea list to the one idea you want to pursue right now, and then get coding! Keep practicing, keep learning, and keep on coding — this is how you succeed with bot programming.

If you want to learn more about Discord bot development and making your own Discord bots on demand, then join the WriteBots community today.

Leave a Reply