You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Program.cs 3.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Discord;
  2. using Discord.Commands;
  3. using Discord.Interactions;
  4. using Discord.WebSocket;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using ShardedClient.Services;
  7. using System;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace ShardedClient
  11. {
  12. // This is a minimal example of using Discord.Net's Sharded Client
  13. // The provided DiscordShardedClient class simplifies having multiple
  14. // DiscordSocketClient instances (or shards) to serve a large number of guilds.
  15. class Program
  16. {
  17. static void Main(string[] args)
  18. => new Program()
  19. .MainAsync()
  20. .GetAwaiter()
  21. .GetResult();
  22. public async Task MainAsync()
  23. {
  24. // You specify the amount of shards you'd like to have with the
  25. // DiscordSocketConfig. Generally, it's recommended to
  26. // have 1 shard per 1500-2000 guilds your bot is in.
  27. var config = new DiscordSocketConfig
  28. {
  29. TotalShards = 2,
  30. GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent
  31. };
  32. // You should dispose a service provider created using ASP.NET
  33. // when you are finished using it, at the end of your app's lifetime.
  34. // If you use another dependency injection framework, you should inspect
  35. // its documentation for the best way to do this.
  36. using (var services = ConfigureServices(config))
  37. {
  38. var client = services.GetRequiredService<DiscordShardedClient>();
  39. // The Sharded Client does not have a Ready event.
  40. // The ShardReady event is used instead, allowing for individual
  41. // control per shard.
  42. client.ShardReady += ReadyAsync;
  43. client.Log += LogAsync;
  44. await services.GetRequiredService<InteractionHandlingService>()
  45. .InitializeAsync();
  46. await services.GetRequiredService<CommandHandlingService>()
  47. .InitializeAsync();
  48. // Tokens should be considered secret data, and never hard-coded.
  49. await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token"));
  50. await client.StartAsync();
  51. await Task.Delay(Timeout.Infinite);
  52. }
  53. }
  54. private ServiceProvider ConfigureServices(DiscordSocketConfig config)
  55. => new ServiceCollection()
  56. .AddSingleton(new DiscordShardedClient(config))
  57. .AddSingleton<CommandService>()
  58. .AddSingleton(x => new InteractionService(x.GetRequiredService<DiscordShardedClient>()))
  59. .AddSingleton<CommandHandlingService>()
  60. .AddSingleton<InteractionHandlingService>()
  61. .BuildServiceProvider();
  62. private Task ReadyAsync(DiscordSocketClient shard)
  63. {
  64. Console.WriteLine($"Shard Number {shard.ShardId} is connected and ready!");
  65. return Task.CompletedTask;
  66. }
  67. private Task LogAsync(LogMessage log)
  68. {
  69. Console.WriteLine(log.ToString());
  70. return Task.CompletedTask;
  71. }
  72. }
  73. }