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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Discord;
  5. using Discord.WebSocket;
  6. namespace BasicBot
  7. {
  8. // This is a minimal, bare-bones example of using Discord.Net.
  9. //
  10. // If writing a bot with commands/interactions, we recommend using the Discord.Net.Commands/Discord.Net.Interactions
  11. // framework, rather than handling them yourself, like we do in this sample.
  12. //
  13. // You can find samples of using the command framework:
  14. // - Here, under the TextCommandFramework sample
  15. // - At the guides: https://discordnet.dev/guides/text_commands/intro.html
  16. //
  17. // You can find samples of using the interaction framework:
  18. // - Here, under the InteractionFramework sample
  19. // - At the guides: https://discordnet.dev/guides/int_framework/intro.html
  20. class Program
  21. {
  22. // Non-static readonly fields can only be assigned in a constructor.
  23. // If you want to assign it elsewhere, consider removing the readonly keyword.
  24. private readonly DiscordSocketClient _client;
  25. // Discord.Net heavily utilizes TAP for async, so we create
  26. // an asynchronous context from the beginning.
  27. static void Main(string[] args)
  28. => new Program()
  29. .MainAsync()
  30. .GetAwaiter()
  31. .GetResult();
  32. public Program()
  33. {
  34. // It is recommended to Dispose of a client when you are finished
  35. // using it, at the end of your app's lifetime.
  36. _client = new DiscordSocketClient();
  37. // Subscribing to client events, so that we may receive them whenever they're invoked.
  38. _client.Log += LogAsync;
  39. _client.Ready += ReadyAsync;
  40. _client.MessageReceived += MessageReceivedAsync;
  41. _client.InteractionCreated += InteractionCreatedAsync;
  42. }
  43. public async Task MainAsync()
  44. {
  45. // Tokens should be considered secret data, and never hard-coded.
  46. await _client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token"));
  47. // Different approaches to making your token a secret is by putting them in local .json, .yaml, .xml or .txt files, then reading them on startup.
  48. await _client.StartAsync();
  49. // Block the program until it is closed.
  50. await Task.Delay(Timeout.Infinite);
  51. }
  52. private Task LogAsync(LogMessage log)
  53. {
  54. Console.WriteLine(log.ToString());
  55. return Task.CompletedTask;
  56. }
  57. // The Ready event indicates that the client has opened a
  58. // connection and it is now safe to access the cache.
  59. private Task ReadyAsync()
  60. {
  61. Console.WriteLine($"{_client.CurrentUser} is connected!");
  62. return Task.CompletedTask;
  63. }
  64. // This is not the recommended way to write a bot - consider
  65. // reading over the Commands Framework sample.
  66. private async Task MessageReceivedAsync(SocketMessage message)
  67. {
  68. // The bot should never respond to itself.
  69. if (message.Author.Id == _client.CurrentUser.Id)
  70. return;
  71. if (message.Content == "!ping")
  72. {
  73. // Create a new componentbuilder, in which dropdowns & buttons can be created.
  74. var cb = new ComponentBuilder()
  75. .WithButton("Click me!", "unique-id", ButtonStyle.Primary);
  76. // Send a message with content 'pong', including a button.
  77. // This button needs to be build by calling .Build() before being passed into the call.
  78. await message.Channel.SendMessageAsync("pong!", components: cb.Build());
  79. }
  80. }
  81. // For better functionality & a more developer-friendly approach to handling any kind of interaction, refer to:
  82. // https://discordnet.dev/guides/int_framework/intro.html
  83. private async Task InteractionCreatedAsync(SocketInteraction interaction)
  84. {
  85. // safety-casting is the best way to prevent something being cast from being null.
  86. // If this check does not pass, it could not be cast to said type.
  87. if (interaction is SocketMessageComponent component)
  88. {
  89. // Check for the ID created in the button mentioned above.
  90. if (component.Data.CustomId == "unique-id")
  91. await interaction.RespondAsync("Thank you for clicking my button!");
  92. else Console.WriteLine("An ID has been received that has no handler!");
  93. }
  94. }
  95. }
  96. }