Create a Discord bot in C# and .NET - Part 1

Creating a Discord bot is a fun way to add new functionality to your Discord server. The Discord API is very comprehensive and beginners may find it too complex to know where to get started. In this series of articles, I'm going to show you have to create a Discord bot using C# and .NET from scratch. The articles will demonstrate fundamentals such as new project setup, starting the bot and adding useful bot commands like: simple responses, embeds and remote API calls.

🧑🏻‍💻 Source code: https://github.com/adamstirtan/DiscordBotTutorial

🧠 Part 2: https://blog.adamstirtan.net/2023/10/create-discord-bot-in-c-and-net-part-2.html

🧠 Part 3: https://blog.adamstirtan.net/2023/10/create-discord-bot-in-c-and-net-part-3.html

Project setup in Visual Studio

Open Visual Studio 2022 and create a new project.

Choose Console App as the project type.


Give your project a name and choose where to save the source code. For simplicity, check "Place solution and project in the same directory".


Choose your .NET version, I'm using the latest LTS which as of writing is .NET 6. I like the explicit syntax without top-level statements so I've checked that box. 👴🏻


At this point, you have a console application that can be started. It will display "Hello, World!" in your terminal. Make sure everything is working properly by launching your project by clicking the play button or pressing F5.


Create application in the Discord developer portal

We'll come back to Visual Studio in a moment. The next step is to tell Discord that we want to create a new application. All running instances of your bot will all share this application's description and permissions. 

  1. Navigate to https://discord.com/developers/applications
  2. Login to Discord
  3. Select New Application

Give your application a name check agree to the terms and conditions.


The next page will have optional properties for your application including an icon and description. Save your changes and navigate to URL Generator which is located under OAuth2 in the navigation bar.

For simplicity sake, I'm going to give my bot Administrator permissions. Customize this to match your requirements.


Scroll down to the bottom of this page and you'll see a Generated URL. Copy the URL and open it in your browser.

Login to Discord and select which server you would like to associate your bot with.


Check your Discord server and you should have seen the bot enter the server. Since we haven't coded anything, it's just going to show as an offline bot.


In the users sidebar, you'll see the offline bot.

Save the Discord token in user secrets

The final thing we need to do is copy the bot's token from the Discord developer portal in to a safe place. If you're planning on committing this code in to source control then the token should not be included. An easy way to do this is using User Secrets.

On the Discord developer portal, select Bot on the navigation bar.


Select Reset Token to generate a new, one-time visible token.


Confirm the token reset by selecting "Yes, do it!". Your bot's token will be displayed now, but only once. Copy the token to the clipboard.


Back over in Visual Studio 2022, we want to securely store our Discord token. To add a user secret, right click on our Project (not the Solution) and select Manage User Secrets.


Visual Studio will ask if you want to add support user secrets, select Yes and a package reference to Microsoft.Extensions.Configuration.UserSecrets will be added to your project file. 

The user secrets file is a JSON file and the structure is up to you. I recommend creating a simple string property named DiscordToken.


That's it for part 1. At this point we'll done all the initial setup and you can safely commit your bot's source code in. We've stored the bot's Discord token in user secrets, so it will not be saved in source control. This is because Visual Studio uses a GUID to reference a directory elsewhere on your file system which is not part of tracked files for source control.

Comments

Popular posts from this blog

Create a Discord bot in C# and .NET - Part 2

Create a Discord bot in C# and .NET - Part 3