The Basics - koreanpanda345/Discord_Net_Example GitHub Wiki

The Basics

First thing first is what ide do you use? Me personally uses Visual Studio 2019, and that is what I will do this tutorial in. But you can use any ide, such as Visual Studio Code.

Next things you need to do is make the project. we will be using the Console App (.NET Core).

Ok now, lets add the discord.net dependencies to the project.

  1. Go to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution...
  2. Then go to the Browse Tab, then type in the search bar discord.net
  3. Click on the first result, and click the check box, then Install
  • If a pop-up appears called Preview Changes, just click ok
  1. A pop-up will appear called License Acceptance, click "I Accept", then wait.

Now we can start programming. First thing first is what we need. Go to the Program.cs file. and add to your file the following:

using System.Collections.Generic;
using System.Text;
using System.Threading.Task;
using Discord;
using Discord.Command;
using Discord.WebSocket;

Next, we are going to define a Variables.

private DiscordSocketClient client = new DiscordSocketClient(new DiscordSocketConfig{
  LogLevel = LogSeverity.Debug
});

DiscordSocketClient is the bot, aka the client. This will allows us to connect to the bot, and do things to it. DiscordSocketConfig is will allow us to customize things, usually its used for logs. LogLevel will set how descriptive we want it to be, we are going to set it to be Debug for now, so it will be really descriptive.

Next we are going to define the prefix and the token.

private string prefix = "<YOUR_PREFIX>";
private string token = "<YOUR_TOKEN>";

replace <YOUR_PREFIX> with the desired prefix you want to use. and replace <YOUR_TOKEN> with your bot token.

Next We are going to make a method. we will call it MainAsync, and this will return async Task(it acts like void, but allows us to run it asynchronous).

What is Asynchronous? Well the best answer is that it forces the program to wait for a Task to end before continuing. Most programs default to synchronous which cause it to run all of it at once in a list. doesn't wait for something to finish.

Next, we will link it to the Main Method. should look like this

static void Main(string[] args)
  => new Program().MainAsync().GetAwaiter().GetResult();

public async Task MainAsync(){

}

what does this do? well what we are doing is grabbing the results that the MainAsync method returns. in our case, it will run the bot when we run the program.

next we need to make a few events. inside the MainAsync method type in the following:

client.Log += Client_Log;
client.Ready += Client_Ready;
client.MessageReceived += Client_MessageReceived;

If you're using vs 2019, then you have neat trick. when you add the += you can press tab and it will generate the events for you. Neat right.

lets start from the bottom up. starting with the Client_Log event.

During this, make sure to delete the throw new NotImplementedException(); we do not need it to throw anything at us.

Client_Log Event fires when the client needs to log, or should log something. since we set the LogLevel to be Debug, it will log everything that happens in discord.(If you don't like this you can change it be Verbose which makes it send only what is needed, not everything.) Lets add the following to the event:

Console.WriteLine($"{DateTime.Now} at {arg.Source}] - {arg.Message}");
return Task.CompletedTask;

So what C# is nice to use and provides us with an easier way to put objects into a string. by putting $ in front of the "" we can then use {} to insert an object inside the string. Cool right. Note that when your writing this in, you will see a red line under the Event Name, this is cause Task is a return type, and since we are not using await keyword, we can just use the return Task.CompletedTask;, this should remove the red line.

Next lets do the Client_Ready event.

The Client_Ready event fires when the client is logged in and is ready to be used. Before we type in stuff we need, we should change this to be async, as we will change the bot's activity. to do this, just put async in between private and Task, like so private async Task Client_Ready(){}. Now we can start writing in the Event. lets add this:

Console.WriteLine($"{client.CurrentUser.Username} is ready");
await client.SetGameAsync($"{prefix}help", null, ActivityType.Watching);

you maybe thinking why await? we don't need this. well await actually is needed for discord.net. it makes things to run asynchronously.

next lets do the fun part of Client_MessageReceived Event. Client_MessageReceived Event fires when a message is received, this is what we need to make a bot obivously. so lets make it as useful as we need it to be. but first like the Client_Ready Event we need it to be asynchronous, so lets do that, then while we are at it, lets change arg to be MessageParam to make senses. now lets start:

var Message = MessageParam as SocketUserMessage;
var Context = new CommandContext(client, Message);

if(Context.Message == null || Context.Message == "") return;
if(Context.User.IsBot) return;

Hey while we are at it lets add a hi command. just under the if(Context.User.IsBot) return; lets add:

if(Context.Message.Content == $"{prefix}hello")
   await Context.Channel.SendMessageAsync("Hello");

Thats it. now we are done with the events, lets go back to the MainAsync method and make the bot login. add this:

await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();
await Task.Delay(-1);

Make sure to add the await Task.Delay(-1); If you do not, then your program will run, then leave right away.

Hey,your all done, now lets test it to make sure it works, in vs 2019 you will see a green arrow at the top next to your Project name, just click that, and let it start up. It might take a minute or two at first run, as it needs to load all the symbols it needs. then go to your server that the bot is in, and just run the command.

⚠️ **GitHub.com Fallback** ⚠️