Add missing REST Webhook implemenation (#843)
* Add Webhook API models, REST implementation, and Socket bridges. * Remove token overrides from REST. Leaving that as a Webhook package only feature. * Add Webhook API models, REST implementation, and Socket bridges. * Remove token overrides from REST. Leaving that as a Webhook package only feature. * Webhook core implementation. * Webhook REST implementation. * Webhook client implementation. * Add channel bucket id.
This commit is contained in:
committed by
Christopher F
parent
a19ff188e9
commit
7b2ddd027c
81
src/Discord.Net.Webhook/WebhookClientHelper.cs
Normal file
81
src/Discord.Net.Webhook/WebhookClientHelper.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.API.Rest;
|
||||
using Discord.Rest;
|
||||
using ImageModel = Discord.API.Image;
|
||||
using WebhookModel = Discord.API.Webhook;
|
||||
|
||||
namespace Discord.Webhook
|
||||
{
|
||||
internal static class WebhookClientHelper
|
||||
{
|
||||
public static async Task<RestInternalWebhook> GetWebhookAsync(DiscordWebhookClient client, ulong webhookId)
|
||||
{
|
||||
var model = await client.ApiClient.GetWebhookAsync(webhookId);
|
||||
if (model == null)
|
||||
throw new InvalidOperationException("Could not find a webhook for the supplied credentials.");
|
||||
return RestInternalWebhook.Create(client, model);
|
||||
}
|
||||
public static async Task<ulong> SendMessageAsync(DiscordWebhookClient client,
|
||||
string text, bool isTTS, IEnumerable<Embed> embeds, string username, string avatarUrl, RequestOptions options)
|
||||
{
|
||||
var args = new CreateWebhookMessageParams(text) { IsTTS = isTTS };
|
||||
if (embeds != null)
|
||||
args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
|
||||
if (username != null)
|
||||
args.Username = username;
|
||||
if (avatarUrl != null)
|
||||
args.AvatarUrl = avatarUrl;
|
||||
|
||||
var model = await client.ApiClient.CreateWebhookMessageAsync(client.Webhook.Id, args, options: options).ConfigureAwait(false);
|
||||
return model.Id;
|
||||
}
|
||||
#if FILESYSTEM
|
||||
public static async Task<ulong> SendFileAsync(DiscordWebhookClient client, string filePath, string text, bool isTTS,
|
||||
IEnumerable<Embed> embeds, string username, string avatarUrl, RequestOptions options)
|
||||
{
|
||||
string filename = Path.GetFileName(filePath);
|
||||
using (var file = File.OpenRead(filePath))
|
||||
return await SendFileAsync(client, file, filename, text, isTTS, embeds, username, avatarUrl, options).ConfigureAwait(false);
|
||||
}
|
||||
#endif
|
||||
public static async Task<ulong> SendFileAsync(DiscordWebhookClient client, Stream stream, string filename, string text, bool isTTS,
|
||||
IEnumerable<Embed> embeds, string username, string avatarUrl, RequestOptions options)
|
||||
{
|
||||
var args = new UploadWebhookFileParams(stream) { Filename = filename, Content = text, IsTTS = isTTS };
|
||||
if (username != null)
|
||||
args.Username = username;
|
||||
if (avatarUrl != null)
|
||||
args.AvatarUrl = username;
|
||||
if (embeds != null)
|
||||
args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
|
||||
var msg = await client.ApiClient.UploadWebhookFileAsync(client.Webhook.Id, args, options).ConfigureAwait(false);
|
||||
return msg.Id;
|
||||
}
|
||||
|
||||
public static async Task<WebhookModel> ModifyAsync(DiscordWebhookClient client,
|
||||
Action<WebhookProperties> func, RequestOptions options)
|
||||
{
|
||||
var args = new WebhookProperties();
|
||||
func(args);
|
||||
var apiArgs = new ModifyWebhookParams
|
||||
{
|
||||
Avatar = args.Image.IsSpecified ? args.Image.Value?.ToModel() : Optional.Create<ImageModel?>(),
|
||||
Name = args.Name
|
||||
};
|
||||
|
||||
if (!apiArgs.Avatar.IsSpecified && client.Webhook.AvatarId != null)
|
||||
apiArgs.Avatar = new ImageModel(client.Webhook.AvatarId);
|
||||
|
||||
return await client.ApiClient.ModifyWebhookAsync(client.Webhook.Id, apiArgs, options).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public static async Task DeleteAsync(DiscordWebhookClient client, RequestOptions options)
|
||||
{
|
||||
await client.ApiClient.DeleteWebhookAsync(client.Webhook.Id, options).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user