This is a Discord Bot application which coded using C# with .NET Core .
- Go to Discord Develpor Portal >>> https://discordapp.com/developers/applications/
- On "Applications" Tab, click the "New Application" button on top-right of page.
- Set the name of your bot in pop-up window. Then click the "Create" button.
- You can set your bot's icon, description and permissions in the "General Information" Tab after the creating.
- Click the "Bot" tab on your page's left column.
-
Click "Yes do it" button when the message box is opened and add your bot.
-
After that you'll see that page. And "click to reveal token".
-
The token is you will use it in C# application, so save it.
-
Also the Client ID (in OAuth2 or General Information Tab) is you need to authorize for your Discord server to bot. Save it, too.
- You should create a Console Application in Visual Studio.
Note: I haven't used Visual Stuido, my IDE is just "Visual Studio Code". So, may there be differences according Visual Studio.
-
Install .NET Core SDK 3.0 from Microsoft.
-
Install "NuGet Package Manaer", for VS Code open the Extensions and search the Nuget Package Manager
- For add "DSharpPlus" package to NuGet, type CTRL+SHIFT+P and open the commands palette.
- Click the "NuGet Package Manager: Add Package"
- Type "DSharpPlus" to search.
- Then click it.
- Select to version of "3.2.3" from the opened list.
- Add this library for your program:
using DSharpPlus;
- Create your OAuth2 link with Client ID. Like this: https://discordapp.com/oauth2/authorize?&client_id=655421891418390528&scope=bot&permissions=0
The "655421891418390528" is your Client ID, paste it after "client_id" tag in link.
- Enter the created link with client id and authorize your bot in your discord server. Like this:
- Create your app codes, like this template (All details are commented):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DSharpPlus;
//OAuth2 URL's of bot is like this >>> https://discordapp.com/oauth2/authorize?&client_id=655421891418390528scope=bot&permissions=0
//This URL is only sample, you should create your own URL with your "client id".
namespace DiscordBot
{
class Program
{
static DiscordClient discord;
static void Main(string[] args)
{
MainAsync(args).ConfigureAwait(false).GetAwaiter().GetResult();
}
static async Task MainAsync(string[] args)
{
discord = new DiscordClient(new DiscordConfiguration
{
Token = "H1wBUFC6DV9JohuaZslSFavU7ZC2aBDI", // YOUR CLIENT'S TOKEN WHICH YOU SAVED IN BOT SETTINGS.
TokenType = TokenType.Bot
});
discord.MessageCreated += async e =>
{
if (e.Message.Content.ToLower().StartsWith("Hi")) // The user's message
await e.Message.RespondAsync("Hi dude!"); // Bot's response
if (e.Message.Content.ToLower() == "Hello Bot, how're you?") // The alternative code for sending message (w/o "StartsWith")
await e.Message.RespondAsync("https://imgur.com/gallery/0000.jpeg"); //Sample for responsing with Image link
if (e.Message.Content.ToLower()=="!soldier") // You can create your own commands start with "!" or another thing.
await e.Message.RespondAsync("Yes sir!");
if (e.Message.Content.ToLower()=="!random") // This is sample for random responsing from bot if you tpye "!random".
{
Random rand = new Random();
int r = rand.Next(0,2);
Console.WriteLine(r);
if(r == 0)
{
await e.Message.RespondAsync("https://imgur.com/gallery/111111.jpeg");
}
else
{
await e.Message.RespondAsync("https://imgur.com/gallery/222222.jpeg");
}
}
};
await discord.ConnectAsync();
await Task.Delay(-1);
}
}
}