Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

discord bot in c#

/*To accomplish it via C# & .Net , You may use an unofficial library 
like discord.net,D#,D#+ or any other
I'm using discord.net
Link: https://github.com/discord-net/Discord.Net
Starter code:*/

// See https://aka.ms/new-console-template for more information
//namespaces
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using GreetingsBot.Common;
using GreetingsBot.Init;
using GreetingsBot.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

var config = new ConfigurationBuilder()
    .AddJsonFile($"appsettings.json")
    .AddEnvironmentVariables()
    .Build();
var client = new DiscordShardedClient();


var commands = new CommandService(new CommandServiceConfig
{
    // Again, log level:
    LogLevel = LogSeverity.Info,
            
    // There's a few more properties you can set,
    // for example, case-insensitive commands.
    CaseSensitiveCommands = false,
});

// Setup your DI container.
Bootstrapper.Init();
Bootstrapper.RegisterInstance(client);
Bootstrapper.RegisterInstance(commands);
Bootstrapper.RegisterType<ICommandHandler, CommandHandler>();
Bootstrapper.RegisterInstance(config);

await MainAsync();

async Task MainAsync()
{
    await Bootstrapper.ServiceProvider.GetRequiredService<ICommandHandler>().InitializeAsync();
    
    client.ShardReady += async shard =>
    {
        await Logger.Log(LogSeverity.Info, "ShardReady", $"Shard Number {shard.ShardId} is connected and ready!");
    };
        
    // Login and connect.
    var token = config.GetRequiredSection("Settings")["DiscordBotToken"];
    if (string.IsNullOrWhiteSpace(token))
    {
        await Logger.Log(LogSeverity.Error, $"{nameof(Program)} | {nameof(MainAsync)}", "Token is null or empty.");
        return;
    }
        
    await client.LoginAsync(TokenType.Bot, token);
    await client.StartAsync();

    // Wait infinitely so your bot actually stays connected.
    await Task.Delay(Timeout.Infinite);
}

/*What is sharding?
In the discord context sharding basically means, splitting one
instance up to at most 1000 guilds (discord servers). Normally you
won’t need some special things to do when starting with your bot. 
But as I always say, forewarned is forearmed.
And if we are honest, we want our bot to be successful.*/
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# get getter set setter method 
Csharp :: create list with values c# 
Csharp :: how to clone somthing unity 
Csharp :: c# textbox numbers only 
Csharp :: c# timestamp now 
Csharp :: c# list subfolders 
Csharp :: c# unity detect any keyboard input but not mouse input 
Csharp :: what is public static void 
Csharp :: unity log error 
Csharp :: unity how to get a child from a gameobject 
Csharp :: c# list with 0 initialize 
Csharp :: good food 
Csharp :: c# string tob64 
Csharp :: c# distinct by property 
Csharp :: c# jobject to string 
Csharp :: convert string to list int c# 
Csharp :: c# convert string to url encoding 
Csharp :: list clone - C# 
Csharp :: unity vs unreal for beginners 
Csharp :: c# create dynamic json 
Csharp :: index of item in list C# 
Csharp :: Configure Automapper 
Csharp :: remove all non alphabetic characters from string c# 
Csharp :: dotnet new api 
Csharp :: calculate distance using latitude and longitude c# 
Csharp :: max value data annotation c# 
Csharp :: c# string interpolation 
Csharp :: C# loop through array of objet 
Csharp :: HCF of list of number 
Csharp :: if statement c# 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =