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.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. };
  31. // You should dispose a service provider created using ASP.NET
  32. // when you are finished using it, at the end of your app's lifetime.
  33. // If you use another dependency injection framework, you should inspect
  34. // its documentation for the best way to do this.
  35. using (var services = ConfigureServices(config))
  36. {
  37. var client = services.GetRequiredService<DiscordShardedClient>();
  38. // The Sharded Client does not have a Ready event.
  39. // The ShardReady event is used instead, allowing for individual
  40. // control per shard.
  41. client.ShardReady += ReadyAsync;
  42. client.Log += LogAsync;
  43. await services.GetRequiredService<InteractionHandlingService>().InitializeAsync();
  44. await services.GetRequiredService<CommandHandlingService>().InitializeAsync();
  45. // Tokens should be considered secret data, and never hard-coded.
  46. await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token"));
  47. await client.StartAsync();
  48. await Task.Delay(Timeout.Infinite);
  49. }
  50. }
  51. private ServiceProvider ConfigureServices(DiscordSocketConfig config)
  52. => new ServiceCollection()
  53. .AddSingleton(new DiscordShardedClient(config))
  54. .AddSingleton<CommandService>()
  55. .AddSingleton(x => new InteractionService(x.GetRequiredService<DiscordShardedClient>()))
  56. .AddSingleton<CommandHandlingService>()
  57. .AddSingleton<InteractionHandlingService>()
  58. .BuildServiceProvider();
  59. private Task ReadyAsync(DiscordSocketClient shard)
  60. {
  61. Console.WriteLine($"Shard Number {shard.ShardId} is connected and ready!");
  62. return Task.CompletedTask;
  63. }
  64. private Task LogAsync(LogMessage log)
  65. {
  66. Console.WriteLine(log.ToString());
  67. return Task.CompletedTask;
  68. }
  69. }
  70. }