[C#/Tutorial] How to make a Discord Bot (Outdated?)

Craze

Alt + F4
Messages
234
Points
478
[[ Please move this thread to the Discord section, once we get one ]]

Requirements:
Visual Studio
Small knowledge of C#
Nuget Package Manager (Look below for how to get)

Ok, firstly make sure you have Visual Studio. Also, you need "Nuget Package Manager" for it.
If you don't have "Nuget Package Manager", Look up how to get it for visual studio.

Step 1 - Setting up the project
Firstly, make a new Visual C# Console application. And the install Discord.NET
To install Discord.NET, go to Tools and the hover over Nuget Package Manager.

Click "Manage NuGet Packages for Solution..."
After that, click Settings, in the bottom left (of the new tab). Make sure nuget.org is checked.

When it's checked, press OK.
In the bar at the left, click "nuget.org". There should be a search bar in the top right. Search "Discord".
Click Discord.Net (Should be at the top).

(Yours shouldn't be ticked). Click install. It should come up with an agreement, a few seconds after, accept it. Once it's downloaded, The API should be working!

Step 2 - Setting up Bot
Firstly, go here and create a new application.
You do not have permission to view link Log in or register now.

Name it whatever you want. After that, go to the bottom and click create application.
Once that's done, You should see "Create bot user" Click it and you've setup the discord side of your bot!
Now, it's time for some scripting! You can do whatever you want with your console application, we will just be loading the bot, but you can do colours, text, console title, etc.
In your main function, make this:
Code:
var bot = new Discord.DiscordClient();
This is used for controlling your Bot. It is a variable.
Now a few lines after that, add this:
Code:
bot.ExecuteAndWait(async () =>
            {
                await bot.Connect("YOUR TOKEN HERE");
            
            });
Go to your bots discord page. It should be here
You do not have permission to view link Log in or register now.

Click on your bot and scroll down to find token. It should say "Click to reveal" next to it. Click it. And then paste that token where it says YOUR TOKEN HERE in your script.
Your main function should look a bit like this (without the red lines)

Well done, your bot is all setup!

Step 3 - Commands
Above this:
Code:
bot.ExecuteAndWait(async () =>
            {
                await bot.Connect("YOUR TOKEN HERE");
            
            });
Add this:
Code:
bot.MessageReceived += bot_MessageReceived;
Once you've done that, add this function:
Code:
static void bot_MessageReceived(object sender, Discord.MessageEventArgs e)
        {


        }
Make sure it is outside of your main function!
Now, it's time to add commands.
Here are some examples:
Code:
if(e.Message.RawText.StartsWith("Your Command"))
            {
                e.Channel.SendMessage(e.User.Mention + "Your reply");
            }
            else if(e.Message.RawText.StartsWith("Your 2nd Command"))
            {
                e.User.SendMessage("Direct Messaging");
            }
Make sure they go in bot_MessageReceived.
and, if you type e and then a dot, you'll get a list of things you can do!

Step 4 - Adding the bot to a server.
Adding a bot to a server is pretty simple.
Go to where your bot application thing is located and find Client Id, it should be at the top.
Example:


You do not have permission to view link Log in or register now.

And then just do what is there and what it says. :grinning:
If you have any questions or suggestions, please tell me in the comments!
 
Last edited:
G

Gentle

Guest
[[ Please move this thread to the Discord section, once we get one ]]

Requirements:
Visual Studio
Small knowledge of C#
Nuget Package Manager (Look below for how to get)

Ok, firstly make sure you have Visual Studio. Also, you need "Nuget Package Manager" for it.
If you don't have "Nuget Package Manager", Look up how to get it for visual studio.

Step 1 - Setting up the project
Firstly, make a new Visual C# Console application. And the install Discord.NET
To install Discord.NET, go to Tools and the hover over Nuget Package Manager.

Click "Manage NuGet Packages for Solution..."
After that, click Settings, in the bottom left (of the new tab). Make sure nuget.org is checked.

When it's checked, press OK.
In the bar at the left, click "nuget.org". There should be a search bar in the top right. Search "Discord".
Click Discord.Net (Should be at the top).

(Yours shouldn't be ticked). Click install. It should come up with an agreement, a few seconds after, accept it. Once it's downloaded, The API should be working!

Step 2 - Setting up Bot
Firstly, go here and create a new application.
You do not have permission to view link Log in or register now.

Name it whatever you want. After that, go to the bottom and click create application.
Once that's done, You should see "Create bot user" Click it and you've setup the discord side of your bot!
Make sure you're using the Discord thing..
Code:
using Discord;
Now, it's time for some scripting! You can do whatever you want with your console application, we will just be loading the bot, but you can do colours, text, console title, etc.
In your main function, make this:
Code:
var bot = new Discord.DiscordClient();
This is used for controlling your Bot. It is a variable.
Now a few lines after that, add this:
Code:
bot.ExecuteAndWait(async () =>
            {
                await bot.Connect("YOUR TOKEN HERE", TokenType.Bot);
             
            });
Go to your bots discord page. It should be here
You do not have permission to view link Log in or register now.

Click on your bot and scroll down to find token. It should say "Click to reveal" next to it. Click it. And then paste that token where it says YOUR TOKEN HERE in your script.
Your main function should look a bit like this (without the red lines)
(OUTDATED IMAGE)

Well done, your bot is all setup!

Step 3 - Commands
Above this:
Code:
bot.ExecuteAndWait(async () =>
            {
                await bot.Connect("YOUR TOKEN HERE");
             
            });
Add this:
Code:
bot.MessageReceived += bot_MessageReceived;
Once you've done that, add this function:
Code:
static void bot_MessageReceived(object sender, Discord.MessageEventArgs e)
        {


        }
Make sure it is outside of your main function!
Now, it's time to add commands.
Here are some examples:
Code:
if(e.Message.RawText.StartsWith("Your Command"))
            {
                e.Channel.SendMessage(e.User.Mention + "Your reply");
            }
            else if(e.Message.RawText.StartsWith("Your 2nd Command"))
            {
                e.User.SendMessage("Direct Messaging");
            }
Make sure they go in bot_MessageReceived.
and, if you type e and the a dot, you'll get a list of things you can do!

Step 4 - Adding the bot to a server.
Adding a bot to a server is pretty simple.
Go to where your bot application thing is located and find Client Id, it should be at the top.
Example:


You do not have permission to view link Log in or register now.

And then just do what is there and what it says. :grinning:
If you have any questions or suggestions, please tell me in the comments!
Hot tut
 
Last edited by a moderator:

CabCon

Head Administrator
Staff member
Head Staff Team
Messages
4,462
Points
828
It is so much fun to work with a bot for discord! Good tutorial :y:
 

Zenshu

New Member
Messages
1
Points
1
How would you tell it to read a .txt file and output the contents to the discord chat? I've looked all over but can't find an answer for this one issue I have run into with getting my bot up and running and doing what we need it to do.
 

HanCobra

New Member
Messages
4
Points
1
This is discord bot code Error: No overload for method 'Connect' takes 1 arguments DiscordBot
 

Micalobia

New Member
Messages
2
Points
1
For some reason, Visual Studio can't find DiscordClient. Was the name of it changed, or am I doing something wrong?
 

Top