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.

WebhookClientHelper.cs 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Discord.API.Rest;
  7. using Discord.Rest;
  8. using ImageModel = Discord.API.Image;
  9. using WebhookModel = Discord.API.Webhook;
  10. namespace Discord.Webhook
  11. {
  12. internal static class WebhookClientHelper
  13. {
  14. public static async Task<RestInternalWebhook> GetWebhookAsync(DiscordWebhookClient client, ulong webhookId)
  15. {
  16. var model = await client.ApiClient.GetWebhookAsync(webhookId);
  17. if (model == null)
  18. throw new InvalidOperationException("Could not find a webhook for the supplied credentials.");
  19. return RestInternalWebhook.Create(client, model);
  20. }
  21. public static async Task<ulong> SendMessageAsync(DiscordWebhookClient client,
  22. string text, bool isTTS, IEnumerable<Embed> embeds, string username, string avatarUrl, RequestOptions options)
  23. {
  24. var args = new CreateWebhookMessageParams(text) { IsTTS = isTTS };
  25. if (embeds != null)
  26. args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
  27. if (username != null)
  28. args.Username = username;
  29. if (avatarUrl != null)
  30. args.AvatarUrl = avatarUrl;
  31. var model = await client.ApiClient.CreateWebhookMessageAsync(client.Webhook.Id, args, options: options).ConfigureAwait(false);
  32. return model.Id;
  33. }
  34. #if FILESYSTEM
  35. public static async Task<ulong> SendFileAsync(DiscordWebhookClient client, string filePath, string text, bool isTTS,
  36. IEnumerable<Embed> embeds, string username, string avatarUrl, RequestOptions options)
  37. {
  38. string filename = Path.GetFileName(filePath);
  39. using (var file = File.OpenRead(filePath))
  40. return await SendFileAsync(client, file, filename, text, isTTS, embeds, username, avatarUrl, options).ConfigureAwait(false);
  41. }
  42. #endif
  43. public static async Task<ulong> SendFileAsync(DiscordWebhookClient client, Stream stream, string filename, string text, bool isTTS,
  44. IEnumerable<Embed> embeds, string username, string avatarUrl, RequestOptions options)
  45. {
  46. var args = new UploadWebhookFileParams(stream) { Filename = filename, Content = text, IsTTS = isTTS };
  47. if (username != null)
  48. args.Username = username;
  49. if (avatarUrl != null)
  50. args.AvatarUrl = avatarUrl;
  51. if (embeds != null)
  52. args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
  53. var msg = await client.ApiClient.UploadWebhookFileAsync(client.Webhook.Id, args, options).ConfigureAwait(false);
  54. return msg.Id;
  55. }
  56. public static async Task<WebhookModel> ModifyAsync(DiscordWebhookClient client,
  57. Action<WebhookProperties> func, RequestOptions options)
  58. {
  59. var args = new WebhookProperties();
  60. func(args);
  61. var apiArgs = new ModifyWebhookParams
  62. {
  63. Avatar = args.Image.IsSpecified ? args.Image.Value?.ToModel() : Optional.Create<ImageModel?>(),
  64. Name = args.Name
  65. };
  66. if (!apiArgs.Avatar.IsSpecified && client.Webhook.AvatarId != null)
  67. apiArgs.Avatar = new ImageModel(client.Webhook.AvatarId);
  68. return await client.ApiClient.ModifyWebhookAsync(client.Webhook.Id, apiArgs, options).ConfigureAwait(false);
  69. }
  70. public static async Task DeleteAsync(DiscordWebhookClient client, RequestOptions options)
  71. {
  72. await client.ApiClient.DeleteWebhookAsync(client.Webhook.Id, options).ConfigureAwait(false);
  73. }
  74. }
  75. }