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 2.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Threading.Tasks;
  3. using _03_sharded_client.Services;
  4. using Discord;
  5. using Discord.Commands;
  6. using Discord.WebSocket;
  7. using Microsoft.Extensions.DependencyInjection;
  8. namespace _03_sharded_client
  9. {
  10. // This is a minimal example of using Discord.Net's Sharded Client
  11. // The provided DiscordShardedClient class simplifies having multiple
  12. // DiscordSocketClient instances (or shards) to serve a large number of guilds.
  13. class Program
  14. {
  15. private DiscordShardedClient _client;
  16. static void Main(string[] args)
  17. => new Program().MainAsync().GetAwaiter().GetResult();
  18. public async Task MainAsync()
  19. {
  20. // You specify the amount of shards you'd like to have with the
  21. // DiscordSocketConfig. Generally, it's recommended to
  22. // have 1 shard per 1500-2000 guilds your bot is in.
  23. var config = new DiscordSocketConfig
  24. {
  25. TotalShards = 2
  26. };
  27. _client = new DiscordShardedClient(config);
  28. var services = ConfigureServices();
  29. // The Sharded Client does not have a Ready event.
  30. // The ShardReady event is used instead, allowing for individual
  31. // control per shard.
  32. _client.ShardReady += ReadyAsync;
  33. _client.Log += LogAsync;
  34. await services.GetRequiredService<CommandHandlingService>().InitializeAsync();
  35. await _client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token"));
  36. await _client.StartAsync();
  37. await Task.Delay(-1);
  38. }
  39. private IServiceProvider ConfigureServices()
  40. {
  41. return new ServiceCollection()
  42. .AddSingleton(_client)
  43. .AddSingleton<CommandService>()
  44. .AddSingleton<CommandHandlingService>()
  45. .BuildServiceProvider();
  46. }
  47. private Task ReadyAsync(DiscordSocketClient shard)
  48. {
  49. Console.WriteLine($"Shard Number {shard.ShardId} is connected and ready!");
  50. return Task.CompletedTask;
  51. }
  52. private Task LogAsync(LogMessage log)
  53. {
  54. Console.WriteLine(log.ToString());
  55. return Task.CompletedTask;
  56. }
  57. }
  58. }