Advertisement
Terror_nisse

Untitled

Jul 14th, 2023
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1.     public class Program
  2.     {
  3.         private readonly IConfiguration _configuration;
  4.         private readonly IServiceProvider _services;
  5.  
  6.         private readonly DiscordSocketConfig _socketConfig = new()
  7.         {
  8.             GatewayIntents = (GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent | GatewayIntents.GuildMembers) ^ GatewayIntents.GuildBans,
  9.             LogGatewayIntentWarnings = false,
  10.             AlwaysDownloadUsers = true,
  11.         };
  12.  
  13.         public Program()
  14.         {
  15.  
  16.             _configuration = new ConfigurationBuilder()
  17.             .AddEnvironmentVariables(prefix: "DC_")
  18.             .AddJsonFile("config.json", optional: true)
  19.             .Build();
  20.  
  21.             _services = new ServiceCollection()
  22.             .AddSingleton(_configuration)
  23.             .AddSingleton(_socketConfig)
  24.             .AddSingleton<DiscordSocketClient>()
  25.             .AddSingleton(x => new InteractionService(x.GetRequiredService<DiscordSocketClient>()))
  26.             .AddSingleton<InteractionHandler>()
  27.             .AddSingleton<IUserSettingHandler, UserSettingHandler>()
  28.             .AddSingleton<IPostwmClass, PostwmClass>()
  29.             .AddSingleton<IUserTranslationsHandler, UserTranslationsHandler>()
  30.             .AddSingleton<UserSettingHandler>()
  31.             .AddSingleton<PostwmClass>()
  32.             .AddSingleton<UserTranslationsHandler>()
  33.             .BuildServiceProvider();
  34.         }
  35.  
  36.         static void Main(string[] args)
  37.         => new Program().RunAsync()
  38.             .GetAwaiter()
  39.             .GetResult();
  40.  
  41.         public async Task RunAsync()
  42.         {
  43.             var client = _services.GetRequiredService<DiscordSocketClient>();
  44.  
  45.             client.Log += LogAsync;
  46.  
  47.             // Here we can initialize the service that will register and execute our commands
  48.             await _services.GetRequiredService<InteractionHandler>()
  49.                 .InitializeAsync();
  50.  
  51.             // Bot token can be provided from the Configuration object we set up earlier
  52.             await client.LoginAsync(TokenType.Bot, _configuration["botToken"]);
  53.             await client.StartAsync();
  54.  
  55.             // Never quit the program until manually forced to.
  56.             await Task.Delay(Timeout.Infinite);
  57.         }
  58.  
  59. #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
  60.         private async Task LogAsync(LogMessage message)
  61. #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
  62.         => Console.WriteLine(message.ToString());
  63.  
  64.         public static bool IsDebug()
  65.         {
  66. #if DEBUG
  67.             return true;
  68. #else
  69.             return false;
  70. #endif
  71.         }
  72.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement