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.

events.cs 1.1 KiB

123456789101112131415161718192021222324252627282930313233343536
  1. using Discord;
  2. using Discord.WebSocket;
  3. public class Program
  4. {
  5. private DiscordSocketClient _client;
  6. static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult();
  7. public async Task MainAsync()
  8. {
  9. // When working with events that have Cacheable<IMessage, ulong> parameters,
  10. // you must enable the message cache in your config settings if you plan to
  11. // use the cached message entity.
  12. var _config = new DiscordSocketConfig { MessageCacheSize = 100 };
  13. _client = new DiscordSocketClient(_config);
  14. await _client.LoginAsync(TokenType.Bot, "bot token");
  15. await _client.StartAsync();
  16. _client.MessageUpdated += MessageUpdated;
  17. _client.Ready += () =>
  18. {
  19. Console.WriteLine("Bot is connected!");
  20. return Task.CompletedTask;
  21. }
  22. await Task.Delay(-1);
  23. }
  24. private async Task MessageUpdated(Cacheable<IMessage, ulong> before, SocketMessage after, ISocketMessageChannel channel)
  25. {
  26. // If the message was not in the cache, downloading it will result in getting a copy of `after`.
  27. var message = await before.GetOrDownloadAsync();
  28. Console.WriteLine($"{message} -> {after}");
  29. }
  30. }