using Discord; using Discord.Commands; using Discord.Net; using Discord.WebSocket; using Microsoft.Extensions.DependencyInjection; using System; using System.Reflection; using System.Threading.Tasks; using Discord_Bot; using Newtonsoft.Json; using Newtonsoft.Json.Linq; public class Program { private DiscordSocketClient _client; private readonly CommandService _commands; private readonly IServiceProvider _services; public static Task Main(string[] args) => new Program().MainAsync(); public async Task MainAsync() { using var services = ConfigureServices(); Console.WriteLine("Ready for takeoff..."); var client = services.GetRequiredService(); client.Log += Log; services.GetRequiredService().Log += Log; // Get the bot token from the Config.json file. JObject config = Functions.GetConfig(); string token = config["token"].Value(); // Log in to Discord and start the bot. await client.LoginAsync(TokenType.Bot, token); await client.StartAsync(); await services.GetRequiredService().InitializeAsync(); // Run the bot forever. await Task.Delay(-1); } public ServiceProvider ConfigureServices() { return new ServiceCollection() .AddSingleton(new DiscordSocketClient(new DiscordSocketConfig { MessageCacheSize = 500, LogLevel = LogSeverity.Info, GatewayIntents = GatewayIntents.All, })) .AddSingleton(new CommandService(new CommandServiceConfig { LogLevel = LogSeverity.Info, DefaultRunMode = RunMode.Async, CaseSensitiveCommands = false })) .AddSingleton() .BuildServiceProvider(); } private Task Log(LogMessage message) { switch (message.Severity) { case LogSeverity.Critical: case LogSeverity.Error: Console.ForegroundColor = ConsoleColor.Red; break; case LogSeverity.Warning: Console.ForegroundColor = ConsoleColor.Yellow; break; case LogSeverity.Info: Console.ForegroundColor = ConsoleColor.White; break; case LogSeverity.Verbose: case LogSeverity.Debug: Console.ForegroundColor = ConsoleColor.DarkGray; break; } Console.WriteLine($"{DateTime.Now,-19} [{message.Severity,8}] {message.Source}: {message.Message} {message.Exception}"); Console.ResetColor(); return Task.CompletedTask; } }