/*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.*/