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.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. var services = ConfigureServices();
  28. _client = new DiscordShardedClient(config);
  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 _client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token"));
  35. await _client.StartAsync();
  36. await Task.Delay(-1);
  37. }
  38. private IServiceProvider ConfigureServices()
  39. {
  40. return new ServiceCollection()
  41. .AddSingleton(_client)
  42. .AddSingleton<CommandService>()
  43. .AddSingleton<CommandHandlingService>()
  44. .BuildServiceProvider();
  45. }
  46. private Task ReadyAsync(DiscordSocketClient shard)
  47. {
  48. Console.WriteLine($"Shard Number {shard.ShardId} is connected and ready!");
  49. return Task.CompletedTask;
  50. }
  51. private Task LogAsync(LogMessage log)
  52. {
  53. Console.WriteLine(log.ToString());
  54. return Task.CompletedTask;
  55. }
  56. }
  57. }