NEW 📢 Save up to 30% on your first month, limited time offer!

How to host a C# (.NET) Discord bot

Discord bot hostingUpdated Oct 6, 20263 min read

This guide is for bots written in C# or another .NET language, for example with Discord.Net or DSharpPlus.

Choose the type

  1. Open your server in the panel, stay on Overview and click Edit Server Type.
  2. Choose .NET Bot. Changing the type reinstalls the server and by default deletes its files, so do this on a new server. See How to switch your bot between Node.js, Python and Java.
  3. In Settings, Game Settings you can pick the .NET version (8 is the default; 6 and 9 are available).

A small example project

MyBot.csproj

``xml <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net8.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Discord.Net" Version="3.*" /> </ItemGroup> </Project> ``

Program.cs

```csharp using Discord; using Discord.WebSocket;

var client = new DiscordSocketClient(new DiscordSocketConfig { GatewayIntents = GatewayIntents.Guilds }); client.Log += msg => { Console.WriteLine(msg); return Task.CompletedTask; };

await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("DISCORD_TOKEN")); await client.StartAsync(); await Task.Delay(Timeout.Infinite); ```

Two ways to deploy

Upload the project. In Files, upload your .csproj and source files (not the bin and obj folders). On every start the server builds your project with dotnet publish and then runs it. The first build downloads NuGet packages, so it takes a minute. Leave Project or dll empty to use the only .csproj in the main folder, or type the file name if there are several.

Upload published files. Run dotnet publish -c Release on your computer and upload the contents of the output folder. Set Project or dll to the .dll name, for example MyBot.dll. Starts faster and needs less memory.

Using git instead? See How to deploy and update your Discord bot from GitHub.

Token and start

Paste your token into Discord bot token. Your code reads it as the environment variable DISCORD_TOKEN. Open Console and start. When the console shows the bot connected, it is online.

Good to know

  • Building uses memory. A project with many packages may need a plan with 1 GB or more while it builds; a published .dll needs far less. See How much RAM does a Discord bot need.
  • A build error stops the server with the compiler messages in Console. Fix them and press Start.
  • Match TargetFramework to the .NET version you chose (net8.0 for .NET 8).

CREATE YOUR GAME SERVER
READY TO GET STARTED?

Save up to 30% on your new game server.
Premium performance, instant activation, and unbeatable reliability all at a better price.

OUR LATEST BLOGS