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.

DiscordWebhookClient.cs 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using Discord.Logging;
  6. using Discord.Rest;
  7. namespace Discord.Webhook
  8. {
  9. public class DiscordWebhookClient : IDisposable
  10. {
  11. public event Func<LogMessage, Task> Log { add { _logEvent.Add(value); } remove { _logEvent.Remove(value); } }
  12. internal readonly AsyncEvent<Func<LogMessage, Task>> _logEvent = new AsyncEvent<Func<LogMessage, Task>>();
  13. private readonly ulong _webhookId;
  14. internal IWebhook Webhook;
  15. internal readonly Logger _restLogger;
  16. internal API.DiscordRestApiClient ApiClient { get; }
  17. internal LogManager LogManager { get; }
  18. /// <summary> Creates a new Webhook discord client. </summary>
  19. public DiscordWebhookClient(IWebhook webhook)
  20. : this(webhook.Id, webhook.Token, new DiscordRestConfig()) { }
  21. /// <summary> Creates a new Webhook discord client. </summary>
  22. public DiscordWebhookClient(ulong webhookId, string webhookToken)
  23. : this(webhookId, webhookToken, new DiscordRestConfig()) { }
  24. /// <summary> Creates a new Webhook discord client. </summary>
  25. public DiscordWebhookClient(ulong webhookId, string webhookToken, DiscordRestConfig config)
  26. : this(config)
  27. {
  28. _webhookId = webhookId;
  29. ApiClient.LoginAsync(TokenType.Webhook, webhookToken).GetAwaiter().GetResult();
  30. Webhook = WebhookClientHelper.GetWebhookAsync(this, webhookId).GetAwaiter().GetResult();
  31. }
  32. /// <summary> Creates a new Webhook discord client. </summary>
  33. public DiscordWebhookClient(IWebhook webhook, DiscordRestConfig config)
  34. : this(config)
  35. {
  36. Webhook = webhook;
  37. _webhookId = Webhook.Id;
  38. }
  39. private DiscordWebhookClient(DiscordRestConfig config)
  40. {
  41. ApiClient = CreateApiClient(config);
  42. LogManager = new LogManager(config.LogLevel);
  43. LogManager.Message += async msg => await _logEvent.InvokeAsync(msg).ConfigureAwait(false);
  44. _restLogger = LogManager.CreateLogger("Rest");
  45. ApiClient.RequestQueue.RateLimitTriggered += async (id, info) =>
  46. {
  47. if (info == null)
  48. await _restLogger.VerboseAsync($"Preemptive Rate limit triggered: {id ?? "null"}").ConfigureAwait(false);
  49. else
  50. await _restLogger.WarningAsync($"Rate limit triggered: {id ?? "null"}").ConfigureAwait(false);
  51. };
  52. ApiClient.SentRequest += async (method, endpoint, millis) => await _restLogger.VerboseAsync($"{method} {endpoint}: {millis} ms").ConfigureAwait(false);
  53. }
  54. private static API.DiscordRestApiClient CreateApiClient(DiscordRestConfig config)
  55. => new API.DiscordRestApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent);
  56. /// <summary> Sends a message using to the channel for this webhook. Returns the ID of the created message. </summary>
  57. public Task<ulong> SendMessageAsync(string text, bool isTTS = false, IEnumerable<Embed> embeds = null,
  58. string username = null, string avatarUrl = null, RequestOptions options = null)
  59. => WebhookClientHelper.SendMessageAsync(this, text, isTTS, embeds, username, avatarUrl, options);
  60. /// <summary> Send a message to the channel for this webhook with an attachment. Returns the ID of the created message. </summary>
  61. public Task<ulong> SendFileAsync(string filePath, string text, bool isTTS = false,
  62. IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null, RequestOptions options = null)
  63. => WebhookClientHelper.SendFileAsync(this, filePath, text, isTTS, embeds, username, avatarUrl, options);
  64. /// <summary> Send a message to the channel for this webhook with an attachment. Returns the ID of the created message. </summary>
  65. public Task<ulong> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false,
  66. IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null, RequestOptions options = null)
  67. => WebhookClientHelper.SendFileAsync(this, stream, filename, text, isTTS, embeds, username, avatarUrl, options);
  68. /// <summary> Modifies the properties of this webhook. </summary>
  69. public Task ModifyWebhookAsync(Action<WebhookProperties> func, RequestOptions options = null)
  70. => Webhook.ModifyAsync(func, options);
  71. /// <summary> Deletes this webhook from Discord and disposes the client. </summary>
  72. public async Task DeleteWebhookAsync(RequestOptions options = null)
  73. {
  74. await Webhook.DeleteAsync(options).ConfigureAwait(false);
  75. Dispose();
  76. }
  77. public void Dispose()
  78. {
  79. ApiClient?.Dispose();
  80. }
  81. }
  82. }