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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Net.Http;
  3. using System.Threading.Tasks;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Discord;
  6. using Discord.WebSocket;
  7. using Discord.Commands;
  8. using _02_commands_framework.Services;
  9. namespace _02_commands_framework
  10. {
  11. // This is a minimal example of using Discord.Net's command
  12. // framework - by no means does it show everything the framework
  13. // is capable of.
  14. //
  15. // You can find samples of using the command framework:
  16. // - Here, under the 02_commands_framework sample
  17. // - https://github.com/foxbot/DiscordBotBase - a barebones bot template
  18. // - https://github.com/foxbot/patek - a more feature-filled bot, utilizing more aspects of the library
  19. class Program
  20. {
  21. static void Main(string[] args)
  22. => new Program().MainAsync().GetAwaiter().GetResult();
  23. public async Task MainAsync()
  24. {
  25. var services = ConfigureServices();
  26. var client = services.GetRequiredService<DiscordSocketClient>();
  27. client.Log += LogAsync;
  28. services.GetRequiredService<CommandService>().Log += LogAsync;
  29. await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token"));
  30. await client.StartAsync();
  31. await services.GetRequiredService<CommandHandlingService>().InitializeAsync();
  32. await Task.Delay(-1);
  33. }
  34. private Task LogAsync(LogMessage log)
  35. {
  36. Console.WriteLine(log.ToString());
  37. return Task.CompletedTask;
  38. }
  39. private IServiceProvider ConfigureServices()
  40. {
  41. return new ServiceCollection()
  42. .AddSingleton<DiscordSocketClient>()
  43. .AddSingleton<CommandService>()
  44. .AddSingleton<CommandHandlingService>()
  45. .AddSingleton<HttpClient>()
  46. .AddSingleton<PictureService>()
  47. .BuildServiceProvider();
  48. }
  49. }
  50. }