Skip to content

This is a Discord Bot application which coded using C# with .NET Core .

Notifications You must be signed in to change notification settings

Frozia00/CSharp-Creating-Discord-Bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 

Repository files navigation

CSharp-Creating-Discord-Bot

This is a Discord Bot application which coded using C# with .NET Core .

Contents

1) Create Discord Developer App

2) C# Application

1) Create Discord Developer App

  • 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.

Screenshot_1

  • You can set your bot's icon, description and permissions in the "General Information" Tab after the creating.

Screenshot_2

  • Click the "Bot" tab on your page's left column.

Screenshot_3

  • 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".

Screenshot_4

  • 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.

2) C# Application

  • 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

Screenshot_5

  • For add "DSharpPlus" package to NuGet, type CTRL+SHIFT+P and open the commands palette.
  • Click the "NuGet Package Manager: Add Package"

Screenshot_6

  • Type "DSharpPlus" to search.
  • Then click it.
  • Select to version of "3.2.3" from the opened list.

Screenshot_7 Screenshot_8 Screenshot_9

  • Add this library for your program:
using DSharpPlus;

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:

Screenshot_10

  • 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);
        }
    }
}

Your bot application is ready for using to Discord. You only need start with debugging the program in console.

About

This is a Discord Bot application which coded using C# with .NET Core .

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages