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.
- Navigate to https://discord.com/developers/applications
- Login to Discord
- Select New Application
Login to Discord and select which server you would like to associate your bot with.
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.
Comments
Post a Comment