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

Craze

Alt + F4
Messages
229
Reaction score
163
Points
903
[[ 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.
5e3614ee5a8c4b238cfd68c5351f1352.png

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

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).
b9ff66e754cd46dcba106500f1ff87e6.png

(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.
Please, Log in or Register to view URLs content!

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
Please, Log in or Register to view URLs content!

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)
f60faa9b06f2467badbc0448577de618.png

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:
8a6ce9e75a9346d48bfbb0c111ce1ef7.png


Please, Log in or Register to view URLs content!

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.
5e3614ee5a8c4b238cfd68c5351f1352.png

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

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).
b9ff66e754cd46dcba106500f1ff87e6.png

(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.
Please, Log in or Register to view URLs content!

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
Please, Log in or Register to view URLs content!

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)
f60faa9b06f2467badbc0448577de618.png

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:
8a6ce9e75a9346d48bfbb0c111ce1ef7.png


Please, Log in or Register to view URLs content!

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
5,093
Reaction score
2,881
Points
1,103
It is so much fun to work with a bot for discord! Good tutorial :y:
 

Zenshu

New Member
Messages
1
Reaction score
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
Reaction score
0
Points
1
This is discord bot code Error: No overload for method 'Connect' takes 1 arguments DiscordBot
 

Micalobia

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