* 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.pull/915/head
@@ -1,5 +1,6 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.IO; | |||
using System.Threading.Tasks; | |||
namespace Discord | |||
@@ -19,5 +20,12 @@ namespace Discord | |||
/// <summary> Modifies this text channel. </summary> | |||
Task ModifyAsync(Action<TextChannelProperties> func, RequestOptions options = null); | |||
/// <summary> Creates a webhook in this text channel. </summary> | |||
Task<IWebhook> CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null); | |||
/// <summary> Gets the webhook in this text channel with the provided id, or null if not found. </summary> | |||
Task<IWebhook> GetWebhookAsync(ulong id, RequestOptions options = null); | |||
/// <summary> Gets the webhooks for this text channel. </summary> | |||
Task<IReadOnlyCollection<IWebhook>> GetWebhooksAsync(RequestOptions options = null); | |||
} | |||
} |
@@ -118,6 +118,11 @@ namespace Discord | |||
/// <summary> Removes all users from this guild if they have not logged on in a provided number of days or, if simulate is true, returns the number of users that would be removed. </summary> | |||
Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null); | |||
/// <summary> Gets the webhook in this guild with the provided id, or null if not found. </summary> | |||
Task<IWebhook> GetWebhookAsync(ulong id, RequestOptions options = null); | |||
/// <summary> Gets a collection of all webhooks for this guild. </summary> | |||
Task<IReadOnlyCollection<IWebhook>> GetWebhooksAsync(RequestOptions options = null); | |||
/// <summary> Gets a specific emote from this guild. </summary> | |||
Task<GuildEmote> GetEmoteAsync(ulong id, RequestOptions options = null); | |||
/// <summary> Creates a new emote in this guild. </summary> | |||
@@ -1,6 +1,5 @@ | |||
namespace Discord | |||
{ | |||
//TODO: Add webhook endpoints | |||
public interface IWebhookUser : IGuildUser | |||
{ | |||
ulong WebhookId { get; } | |||
@@ -0,0 +1,34 @@ | |||
using System; | |||
using System.Threading.Tasks; | |||
namespace Discord | |||
{ | |||
public interface IWebhook : IDeletable, ISnowflakeEntity | |||
{ | |||
/// <summary> Gets the token of this webhook. </summary> | |||
string Token { get; } | |||
/// <summary> Gets the default name of this webhook. </summary> | |||
string Name { get; } | |||
/// <summary> Gets the id of this webhook's default avatar. </summary> | |||
string AvatarId { get; } | |||
/// <summary> Gets the url to this webhook's default avatar. </summary> | |||
string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128); | |||
/// <summary> Gets the channel for this webhook. </summary> | |||
ITextChannel Channel { get; } | |||
/// <summary> Gets the id of the channel for this webhook. </summary> | |||
ulong ChannelId { get; } | |||
/// <summary> Gets the guild owning this webhook. </summary> | |||
IGuild Guild { get; } | |||
/// <summary> Gets the id of the guild owning this webhook. </summary> | |||
ulong? GuildId { get; } | |||
/// <summary> Gets the user that created this webhook. </summary> | |||
IUser Creator { get; } | |||
/// <summary> Modifies this webhook. </summary> | |||
Task ModifyAsync(Action<WebhookProperties> func, RequestOptions options = null); | |||
} | |||
} |
@@ -0,0 +1,41 @@ | |||
namespace Discord | |||
{ | |||
/// <summary> | |||
/// Modify an <see cref="IWebhook"/> with the specified parameters. | |||
/// </summary> | |||
/// <example> | |||
/// <code language="c#"> | |||
/// await webhook.ModifyAsync(x => | |||
/// { | |||
/// x.Name = "Bob"; | |||
/// x.Avatar = new Image("avatar.jpg"); | |||
/// }); | |||
/// </code> | |||
/// </example> | |||
/// <seealso cref="IWebhook"/> | |||
public class WebhookProperties | |||
{ | |||
/// <summary> | |||
/// The default name of the webhook. | |||
/// </summary> | |||
public Optional<string> Name { get; set; } | |||
/// <summary> | |||
/// The default avatar of the webhook. | |||
/// </summary> | |||
public Optional<Image?> Image { get; set; } | |||
/// <summary> | |||
/// The channel for this webhook. | |||
/// </summary> | |||
/// <remarks> | |||
/// This field is not used when authenticated with <see cref="TokenType.Webhook"/>. | |||
/// </remarks> | |||
public Optional<ITextChannel> Channel { get; set; } | |||
/// <summary> | |||
/// The channel id for this webhook. | |||
/// </summary> | |||
/// <remarks> | |||
/// This field is not used when authenticated with <see cref="TokenType.Webhook"/>. | |||
/// </remarks> | |||
public Optional<ulong> ChannelId { get; set; } | |||
} | |||
} |
@@ -34,5 +34,7 @@ namespace Discord | |||
Task<IReadOnlyCollection<IVoiceRegion>> GetVoiceRegionsAsync(RequestOptions options = null); | |||
Task<IVoiceRegion> GetVoiceRegionAsync(string id, RequestOptions options = null); | |||
Task<IWebhook> GetWebhookAsync(ulong id, RequestOptions options = null); | |||
} | |||
} |
@@ -0,0 +1,25 @@ | |||
#pragma warning disable CS1591 | |||
using Newtonsoft.Json; | |||
namespace Discord.API | |||
{ | |||
internal class Webhook | |||
{ | |||
[JsonProperty("id")] | |||
public ulong Id { get; set; } | |||
[JsonProperty("channel_id")] | |||
public ulong ChannelId { get; set; } | |||
[JsonProperty("token")] | |||
public string Token { get; set; } | |||
[JsonProperty("name")] | |||
public Optional<string> Name { get; set; } | |||
[JsonProperty("avatar")] | |||
public Optional<string> Avatar { get; set; } | |||
[JsonProperty("guild_id")] | |||
public Optional<ulong> GuildId { get; set; } | |||
[JsonProperty("user")] | |||
public Optional<User> Creator { get; set; } | |||
} | |||
} |
@@ -0,0 +1,14 @@ | |||
#pragma warning disable CS1591 | |||
using Newtonsoft.Json; | |||
namespace Discord.API.Rest | |||
{ | |||
[JsonObject(MemberSerialization = MemberSerialization.OptIn)] | |||
internal class CreateWebhookParams | |||
{ | |||
[JsonProperty("name")] | |||
public string Name { get; set; } | |||
[JsonProperty("avatar")] | |||
public Optional<Image?> Avatar { get; set; } | |||
} | |||
} |
@@ -0,0 +1,16 @@ | |||
#pragma warning disable CS1591 | |||
using Newtonsoft.Json; | |||
namespace Discord.API.Rest | |||
{ | |||
[JsonObject(MemberSerialization = MemberSerialization.OptIn)] | |||
internal class ModifyWebhookParams | |||
{ | |||
[JsonProperty("name")] | |||
public Optional<string> Name { get; set; } | |||
[JsonProperty("avatar")] | |||
public Optional<Image?> Avatar { get; set; } | |||
[JsonProperty("channel_id")] | |||
public Optional<ulong> ChannelId { get; set; } | |||
} | |||
} |
@@ -1,7 +1,7 @@ | |||
#pragma warning disable CS1591 | |||
using Discord.Net.Rest; | |||
using System.Collections.Generic; | |||
using System.IO; | |||
using Discord.Net.Rest; | |||
namespace Discord.API.Rest | |||
{ | |||
@@ -15,6 +15,7 @@ namespace Discord.API.Rest | |||
public Optional<bool> IsTTS { get; set; } | |||
public Optional<string> Username { get; set; } | |||
public Optional<string> AvatarUrl { get; set; } | |||
public Optional<Embed[]> Embeds { get; set; } | |||
public UploadWebhookFileParams(Stream file) | |||
{ | |||
@@ -25,6 +26,7 @@ namespace Discord.API.Rest | |||
{ | |||
var d = new Dictionary<string, object>(); | |||
d["file"] = new MultipartFile(File, Filename.GetValueOrDefault("unknown.dat")); | |||
if (Content.IsSpecified) | |||
d["content"] = Content.Value; | |||
if (IsTTS.IsSpecified) | |||
@@ -35,6 +37,8 @@ namespace Discord.API.Rest | |||
d["username"] = Username.Value; | |||
if (AvatarUrl.IsSpecified) | |||
d["avatar_url"] = AvatarUrl.Value; | |||
if (Embeds.IsSpecified) | |||
d["embeds"] = Embeds.Value; | |||
return d; | |||
} | |||
} | |||
@@ -164,6 +164,9 @@ namespace Discord.Rest | |||
Task<IVoiceRegion> IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options) | |||
=> Task.FromResult<IVoiceRegion>(null); | |||
Task<IWebhook> IDiscordClient.GetWebhookAsync(ulong id, RequestOptions options) | |||
=> Task.FromResult<IWebhook>(null); | |||
Task IDiscordClient.StartAsync() | |||
=> Task.Delay(0); | |||
Task IDiscordClient.StopAsync() | |||
@@ -144,6 +144,14 @@ namespace Discord.Rest | |||
return null; | |||
} | |||
public static async Task<RestWebhook> GetWebhookAsync(BaseDiscordClient client, ulong id, RequestOptions options) | |||
{ | |||
var model = await client.ApiClient.GetWebhookAsync(id); | |||
if (model != null) | |||
return RestWebhook.Create(client, (IGuild)null, model); | |||
return null; | |||
} | |||
public static async Task<IReadOnlyCollection<RestVoiceRegion>> GetVoiceRegionsAsync(BaseDiscordClient client, RequestOptions options) | |||
{ | |||
var models = await client.ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false); | |||
@@ -473,7 +473,7 @@ namespace Discord.API | |||
var ids = new BucketIds(channelId: channelId); | |||
return await SendJsonAsync<Message>("POST", () => $"channels/{channelId}/messages", args, ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); | |||
} | |||
public async Task CreateWebhookMessageAsync(ulong webhookId, CreateWebhookMessageParams args, RequestOptions options = null) | |||
public async Task<Message> CreateWebhookMessageAsync(ulong webhookId, CreateWebhookMessageParams args, RequestOptions options = null) | |||
{ | |||
if (AuthTokenType != TokenType.Webhook) | |||
throw new InvalidOperationException($"This operation may only be called with a {nameof(TokenType.Webhook)} token."); | |||
@@ -486,8 +486,8 @@ namespace Discord.API | |||
if (args.Content.Length > DiscordConfig.MaxMessageSize) | |||
throw new ArgumentException($"Message content is too long, length must be less or equal to {DiscordConfig.MaxMessageSize}.", nameof(args.Content)); | |||
options = RequestOptions.CreateOrClone(options); | |||
await SendJsonAsync("POST", () => $"webhooks/{webhookId}/{AuthToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); | |||
return await SendJsonAsync<Message>("POST", () => $"webhooks/{webhookId}/{AuthToken}?wait=true", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); | |||
} | |||
public async Task<Message> UploadFileAsync(ulong channelId, UploadFileParams args, RequestOptions options = null) | |||
{ | |||
@@ -503,7 +503,7 @@ namespace Discord.API | |||
var ids = new BucketIds(channelId: channelId); | |||
return await SendMultipartAsync<Message>("POST", () => $"channels/{channelId}/messages", args.ToDictionary(), ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); | |||
} | |||
public async Task UploadWebhookFileAsync(ulong webhookId, UploadWebhookFileParams args, RequestOptions options = null) | |||
public async Task<Message> UploadWebhookFileAsync(ulong webhookId, UploadWebhookFileParams args, RequestOptions options = null) | |||
{ | |||
if (AuthTokenType != TokenType.Webhook) | |||
throw new InvalidOperationException($"This operation may only be called with a {nameof(TokenType.Webhook)} token."); | |||
@@ -522,7 +522,7 @@ namespace Discord.API | |||
throw new ArgumentOutOfRangeException($"Message content is too long, length must be less or equal to {DiscordConfig.MaxMessageSize}.", nameof(args.Content)); | |||
} | |||
await SendMultipartAsync("POST", () => $"webhooks/{webhookId}/{AuthToken}", args.ToDictionary(), new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); | |||
return await SendMultipartAsync<Message>("POST", () => $"webhooks/{webhookId}/{AuthToken}?wait=true", args.ToDictionary(), new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); | |||
} | |||
public async Task DeleteMessageAsync(ulong channelId, ulong messageId, RequestOptions options = null) | |||
{ | |||
@@ -1198,6 +1198,70 @@ namespace Discord.API | |||
return await SendAsync<IReadOnlyCollection<VoiceRegion>>("GET", () => $"guilds/{guildId}/regions", ids, options: options).ConfigureAwait(false); | |||
} | |||
//Webhooks | |||
public async Task<Webhook> CreateWebhookAsync(ulong channelId, CreateWebhookParams args, RequestOptions options = null) | |||
{ | |||
Preconditions.NotEqual(channelId, 0, nameof(channelId)); | |||
Preconditions.NotNull(args, nameof(args)); | |||
Preconditions.NotNull(args.Name, nameof(args.Name)); | |||
options = RequestOptions.CreateOrClone(options); | |||
var ids = new BucketIds(channelId: channelId); | |||
return await SendJsonAsync<Webhook>("POST", () => $"channels/{channelId}/webhooks", args, ids, options: options); | |||
} | |||
public async Task<Webhook> GetWebhookAsync(ulong webhookId, RequestOptions options = null) | |||
{ | |||
Preconditions.NotEqual(webhookId, 0, nameof(webhookId)); | |||
options = RequestOptions.CreateOrClone(options); | |||
try | |||
{ | |||
if (AuthTokenType == TokenType.Webhook) | |||
return await SendAsync<Webhook>("GET", () => $"webhooks/{webhookId}/{AuthToken}", new BucketIds(), options: options).ConfigureAwait(false); | |||
else | |||
return await SendAsync<Webhook>("GET", () => $"webhooks/{webhookId}", new BucketIds(), options: options).ConfigureAwait(false); | |||
} | |||
catch (HttpException ex) when (ex.HttpCode == HttpStatusCode.NotFound) { return null; } | |||
} | |||
public async Task<Webhook> ModifyWebhookAsync(ulong webhookId, ModifyWebhookParams args, RequestOptions options = null) | |||
{ | |||
Preconditions.NotEqual(webhookId, 0, nameof(webhookId)); | |||
Preconditions.NotNull(args, nameof(args)); | |||
Preconditions.NotNullOrEmpty(args.Name, nameof(args.Name)); | |||
options = RequestOptions.CreateOrClone(options); | |||
if (AuthTokenType == TokenType.Webhook) | |||
return await SendJsonAsync<Webhook>("PATCH", () => $"webhooks/{webhookId}/{AuthToken}", args, new BucketIds(), options: options).ConfigureAwait(false); | |||
else | |||
return await SendJsonAsync<Webhook>("PATCH", () => $"webhooks/{webhookId}", args, new BucketIds(), options: options).ConfigureAwait(false); | |||
} | |||
public async Task DeleteWebhookAsync(ulong webhookId, RequestOptions options = null) | |||
{ | |||
Preconditions.NotEqual(webhookId, 0, nameof(webhookId)); | |||
options = RequestOptions.CreateOrClone(options); | |||
if (AuthTokenType == TokenType.Webhook) | |||
await SendAsync("DELETE", () => $"webhooks/{webhookId}/{AuthToken}", new BucketIds(), options: options).ConfigureAwait(false); | |||
else | |||
await SendAsync("DELETE", () => $"webhooks/{webhookId}", new BucketIds(), options: options).ConfigureAwait(false); | |||
} | |||
public async Task<IReadOnlyCollection<Webhook>> GetGuildWebhooksAsync(ulong guildId, RequestOptions options = null) | |||
{ | |||
Preconditions.NotEqual(guildId, 0, nameof(guildId)); | |||
options = RequestOptions.CreateOrClone(options); | |||
var ids = new BucketIds(guildId: guildId); | |||
return await SendAsync<IReadOnlyCollection<Webhook>>("GET", () => $"guilds/{guildId}/webhooks", ids, options: options).ConfigureAwait(false); | |||
} | |||
public async Task<IReadOnlyCollection<Webhook>> GetChannelWebhooksAsync(ulong channelId, RequestOptions options = null) | |||
{ | |||
Preconditions.NotEqual(channelId, 0, nameof(channelId)); | |||
options = RequestOptions.CreateOrClone(options); | |||
var ids = new BucketIds(channelId: channelId); | |||
return await SendAsync<IReadOnlyCollection<Webhook>>("GET", () => $"channels/{channelId}/webhooks", ids, options: options).ConfigureAwait(false); | |||
} | |||
//Helpers | |||
protected void CheckState() | |||
{ | |||
@@ -91,6 +91,9 @@ namespace Discord.Rest | |||
/// <inheritdoc /> | |||
public Task<RestVoiceRegion> GetVoiceRegionAsync(string id, RequestOptions options = null) | |||
=> ClientHelper.GetVoiceRegionAsync(this, id, options); | |||
/// <inheritdoc /> | |||
public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null) | |||
=> ClientHelper.GetWebhookAsync(this, id, options); | |||
//IDiscordClient | |||
async Task<IApplication> IDiscordClient.GetApplicationInfoAsync(RequestOptions options) | |||
@@ -160,5 +163,8 @@ namespace Discord.Rest | |||
=> await GetVoiceRegionsAsync(options).ConfigureAwait(false); | |||
async Task<IVoiceRegion> IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options) | |||
=> await GetVoiceRegionAsync(id, options).ConfigureAwait(false); | |||
async Task<IWebhook> IDiscordClient.GetWebhookAsync(ulong id, RequestOptions options) | |||
=> await GetWebhookAsync(id, options); | |||
} | |||
} |
@@ -7,6 +7,7 @@ using System.Linq; | |||
using System.Threading.Tasks; | |||
using Model = Discord.API.Channel; | |||
using UserModel = Discord.API.User; | |||
using WebhookModel = Discord.API.Webhook; | |||
namespace Discord.Rest | |||
{ | |||
@@ -280,6 +281,30 @@ namespace Discord.Rest | |||
RequestOptions options) | |||
=> new TypingNotifier(client, channel, options); | |||
//Webhooks | |||
public static async Task<RestWebhook> CreateWebhookAsync(ITextChannel channel, BaseDiscordClient client, string name, Stream avatar, RequestOptions options) | |||
{ | |||
var args = new CreateWebhookParams { Name = name }; | |||
if (avatar != null) | |||
args.Avatar = new API.Image(avatar); | |||
var model = await client.ApiClient.CreateWebhookAsync(channel.Id, args, options).ConfigureAwait(false); | |||
return RestWebhook.Create(client, channel, model); | |||
} | |||
public static async Task<RestWebhook> GetWebhookAsync(ITextChannel channel, BaseDiscordClient client, ulong id, RequestOptions options) | |||
{ | |||
var model = await client.ApiClient.GetWebhookAsync(id, options: options).ConfigureAwait(false); | |||
if (model == null) | |||
return null; | |||
return RestWebhook.Create(client, channel, model); | |||
} | |||
public static async Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(ITextChannel channel, BaseDiscordClient client, RequestOptions options) | |||
{ | |||
var models = await client.ApiClient.GetChannelWebhooksAsync(channel.Id, options).ConfigureAwait(false); | |||
return models.Select(x => RestWebhook.Create(client, channel, x)) | |||
.ToImmutableArray(); | |||
} | |||
//Helpers | |||
private static IUser GetAuthor(BaseDiscordClient client, IGuild guild, UserModel model, ulong? webhookId) | |||
{ | |||
@@ -77,8 +77,23 @@ namespace Discord.Rest | |||
public IDisposable EnterTypingState(RequestOptions options = null) | |||
=> ChannelHelper.EnterTypingState(this, Discord, options); | |||
public Task<RestWebhook> CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null) | |||
=> ChannelHelper.CreateWebhookAsync(this, Discord, name, avatar, options); | |||
public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null) | |||
=> ChannelHelper.GetWebhookAsync(this, Discord, id, options); | |||
public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null) | |||
=> ChannelHelper.GetWebhooksAsync(this, Discord, options); | |||
private string DebuggerDisplay => $"{Name} ({Id}, Text)"; | |||
//ITextChannel | |||
async Task<IWebhook> ITextChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options) | |||
=> await CreateWebhookAsync(name, avatar, options); | |||
async Task<IWebhook> ITextChannel.GetWebhookAsync(ulong id, RequestOptions options) | |||
=> await GetWebhookAsync(id, options); | |||
async Task<IReadOnlyCollection<IWebhook>> ITextChannel.GetWebhooksAsync(RequestOptions options) | |||
=> await GetWebhooksAsync(options); | |||
//IMessageChannel | |||
async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options) | |||
{ | |||
@@ -254,6 +254,20 @@ namespace Discord.Rest | |||
return model.Pruned; | |||
} | |||
//Webhooks | |||
public static async Task<RestWebhook> GetWebhookAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options) | |||
{ | |||
var model = await client.ApiClient.GetWebhookAsync(id, options: options).ConfigureAwait(false); | |||
if (model == null) | |||
return null; | |||
return RestWebhook.Create(client, guild, model); | |||
} | |||
public static async Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(IGuild guild, BaseDiscordClient client, RequestOptions options) | |||
{ | |||
var models = await client.ApiClient.GetGuildWebhooksAsync(guild.Id, options).ConfigureAwait(false); | |||
return models.Select(x => RestWebhook.Create(client, guild, x)).ToImmutableArray(); | |||
} | |||
//Emotes | |||
public static async Task<GuildEmote> GetEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options) | |||
{ | |||
@@ -257,6 +257,12 @@ namespace Discord.Rest | |||
public Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null) | |||
=> GuildHelper.PruneUsersAsync(this, Discord, days, simulate, options); | |||
//Webhooks | |||
public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null) | |||
=> GuildHelper.GetWebhookAsync(this, Discord, id, options); | |||
public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null) | |||
=> GuildHelper.GetWebhooksAsync(this, Discord, options); | |||
public override string ToString() => Name; | |||
private string DebuggerDisplay => $"{Name} ({Id})"; | |||
@@ -396,5 +402,10 @@ namespace Discord.Rest | |||
return ImmutableArray.Create<IGuildUser>(); | |||
} | |||
Task IGuild.DownloadUsersAsync() { throw new NotSupportedException(); } | |||
async Task<IWebhook> IGuild.GetWebhookAsync(ulong id, RequestOptions options) | |||
=> await GetWebhookAsync(id, options); | |||
async Task<IReadOnlyCollection<IWebhook>> IGuild.GetWebhooksAsync(RequestOptions options) | |||
=> await GetWebhooksAsync(options); | |||
} | |||
} |
@@ -0,0 +1,91 @@ | |||
using System; | |||
using System.Diagnostics; | |||
using System.Threading.Tasks; | |||
using Model = Discord.API.Webhook; | |||
namespace Discord.Rest | |||
{ | |||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")] | |||
public class RestWebhook : RestEntity<ulong>, IWebhook, IUpdateable | |||
{ | |||
internal IGuild Guild { get; private set; } | |||
internal ITextChannel Channel { get; private set; } | |||
public ulong ChannelId { get; } | |||
public string Token { get; } | |||
public string Name { get; private set; } | |||
public string AvatarId { get; private set; } | |||
public ulong? GuildId { get; private set; } | |||
public IUser Creator { get; private set; } | |||
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id); | |||
internal RestWebhook(BaseDiscordClient discord, IGuild guild, ulong id, string token, ulong channelId) | |||
: base(discord, id) | |||
{ | |||
Guild = guild; | |||
Token = token; | |||
ChannelId = channelId; | |||
} | |||
internal RestWebhook(BaseDiscordClient discord, ITextChannel channel, ulong id, string token, ulong channelId) | |||
: this(discord, channel.Guild, id, token, channelId) | |||
{ | |||
Channel = channel; | |||
} | |||
internal static RestWebhook Create(BaseDiscordClient discord, IGuild guild, Model model) | |||
{ | |||
var entity = new RestWebhook(discord, guild, model.Id, model.Token, model.ChannelId); | |||
entity.Update(model); | |||
return entity; | |||
} | |||
internal static RestWebhook Create(BaseDiscordClient discord, ITextChannel channel, Model model) | |||
{ | |||
var entity = new RestWebhook(discord, channel, model.Id, model.Token, model.ChannelId); | |||
entity.Update(model); | |||
return entity; | |||
} | |||
internal void Update(Model model) | |||
{ | |||
if (model.Avatar.IsSpecified) | |||
AvatarId = model.Avatar.Value; | |||
if (model.Creator.IsSpecified) | |||
Creator = RestUser.Create(Discord, model.Creator.Value); | |||
if (model.GuildId.IsSpecified) | |||
GuildId = model.GuildId.Value; | |||
if (model.Name.IsSpecified) | |||
Name = model.Name.Value; | |||
} | |||
public async Task UpdateAsync(RequestOptions options = null) | |||
{ | |||
var model = await Discord.ApiClient.GetWebhookAsync(Id, options).ConfigureAwait(false); | |||
Update(model); | |||
} | |||
public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) | |||
=> CDN.GetUserAvatarUrl(Id, AvatarId, size, format); | |||
public async Task ModifyAsync(Action<WebhookProperties> func, RequestOptions options = null) | |||
{ | |||
var model = await WebhookHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false); | |||
Update(model); | |||
} | |||
public Task DeleteAsync(RequestOptions options = null) | |||
=> WebhookHelper.DeleteAsync(this, Discord, options); | |||
public override string ToString() => $"Webhook: {Name}:{Id}"; | |||
private string DebuggerDisplay => $"Webhook: {Name} ({Id})"; | |||
//IWebhook | |||
IGuild IWebhook.Guild | |||
=> Guild ?? throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object."); | |||
ITextChannel IWebhook.Channel | |||
=> Channel ?? throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object."); | |||
Task IWebhook.ModifyAsync(Action<WebhookProperties> func, RequestOptions options) | |||
=> ModifyAsync(func, options); | |||
} | |||
} |
@@ -0,0 +1,38 @@ | |||
using System; | |||
using System.Threading.Tasks; | |||
using Discord.API.Rest; | |||
using ImageModel = Discord.API.Image; | |||
using Model = Discord.API.Webhook; | |||
namespace Discord.Rest | |||
{ | |||
internal static class WebhookHelper | |||
{ | |||
public static async Task<Model> ModifyAsync(IWebhook webhook, BaseDiscordClient 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 && webhook.AvatarId != null) | |||
apiArgs.Avatar = new ImageModel(webhook.AvatarId); | |||
if (args.Channel.IsSpecified) | |||
apiArgs.ChannelId = args.Channel.Value.Id; | |||
else if (args.ChannelId.IsSpecified) | |||
apiArgs.ChannelId = args.ChannelId.Value; | |||
return await client.ApiClient.ModifyWebhookAsync(webhook.Id, apiArgs, options).ConfigureAwait(false); | |||
} | |||
public static async Task DeleteAsync(IWebhook webhook, BaseDiscordClient client, RequestOptions options) | |||
{ | |||
await client.ApiClient.DeleteWebhookAsync(webhook.Id, options).ConfigureAwait(false); | |||
} | |||
} | |||
} |
@@ -68,11 +68,25 @@ namespace Discord.Rpc | |||
=> ChannelHelper.TriggerTypingAsync(this, Discord, options); | |||
public IDisposable EnterTypingState(RequestOptions options = null) | |||
=> ChannelHelper.EnterTypingState(this, Discord, options); | |||
//Webhooks | |||
public Task<RestWebhook> CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null) | |||
=> ChannelHelper.CreateWebhookAsync(this, Discord, name, avatar, options); | |||
public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null) | |||
=> ChannelHelper.GetWebhookAsync(this, Discord, id, options); | |||
public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null) | |||
=> ChannelHelper.GetWebhooksAsync(this, Discord, options); | |||
private string DebuggerDisplay => $"{Name} ({Id}, Text)"; | |||
//ITextChannel | |||
string ITextChannel.Topic { get { throw new NotSupportedException(); } } | |||
async Task<IWebhook> ITextChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options) | |||
=> await CreateWebhookAsync(name, avatar, options); | |||
async Task<IWebhook> ITextChannel.GetWebhookAsync(ulong id, RequestOptions options) | |||
=> await GetWebhookAsync(id, options); | |||
async Task<IReadOnlyCollection<IWebhook>> ITextChannel.GetWebhooksAsync(RequestOptions options) | |||
=> await GetWebhooksAsync(options); | |||
//IMessageChannel | |||
async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options) | |||
@@ -0,0 +1,13 @@ | |||
#pragma warning disable CS1591 | |||
using Newtonsoft.Json; | |||
namespace Discord.API.Gateway | |||
{ | |||
internal class WebhookUpdateEvent | |||
{ | |||
[JsonProperty("guild_id")] | |||
public ulong GuildId { get; set; } | |||
[JsonProperty("channel_id")] | |||
public ulong ChannelId { get; set; } | |||
} | |||
} |
@@ -112,10 +112,26 @@ namespace Discord.WebSocket | |||
} | |||
return null; | |||
} | |||
//Webhooks | |||
public Task<RestWebhook> CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null) | |||
=> ChannelHelper.CreateWebhookAsync(this, Discord, name, avatar, options); | |||
public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null) | |||
=> ChannelHelper.GetWebhookAsync(this, Discord, id, options); | |||
public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null) | |||
=> ChannelHelper.GetWebhooksAsync(this, Discord, options); | |||
private string DebuggerDisplay => $"{Name} ({Id}, Text)"; | |||
internal new SocketTextChannel Clone() => MemberwiseClone() as SocketTextChannel; | |||
//ITextChannel | |||
async Task<IWebhook> ITextChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options) | |||
=> await CreateWebhookAsync(name, avatar, options); | |||
async Task<IWebhook> ITextChannel.GetWebhookAsync(ulong id, RequestOptions options) | |||
=> await GetWebhookAsync(id, options); | |||
async Task<IReadOnlyCollection<IWebhook>> ITextChannel.GetWebhooksAsync(RequestOptions options) | |||
=> await GetWebhooksAsync(options); | |||
//IGuildChannel | |||
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | |||
=> Task.FromResult<IGuildUser>(GetUser(id)); | |||
@@ -433,6 +433,12 @@ namespace Discord.WebSocket | |||
_downloaderPromise.TrySetResultAsync(true); | |||
} | |||
//Webhooks | |||
public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null) | |||
=> GuildHelper.GetWebhookAsync(this, Discord, id, options); | |||
public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null) | |||
=> GuildHelper.GetWebhooksAsync(this, Discord, options); | |||
//Emotes | |||
public Task<GuildEmote> GetEmoteAsync(ulong id, RequestOptions options = null) | |||
=> GuildHelper.GetEmoteAsync(this, Discord, id, options); | |||
@@ -682,5 +688,10 @@ namespace Discord.WebSocket | |||
Task<IGuildUser> IGuild.GetOwnerAsync(CacheMode mode, RequestOptions options) | |||
=> Task.FromResult<IGuildUser>(Owner); | |||
Task IGuild.DownloadUsersAsync() { throw new NotSupportedException(); } | |||
async Task<IWebhook> IGuild.GetWebhookAsync(ulong id, RequestOptions options) | |||
=> await GetWebhookAsync(id, options); | |||
async Task<IReadOnlyCollection<IWebhook>> IGuild.GetWebhooksAsync(RequestOptions options) | |||
=> await GetWebhooksAsync(options); | |||
} | |||
} |
@@ -1,32 +1,49 @@ | |||
using Discord.API.Rest; | |||
using Discord.Rest; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.IO; | |||
using System.Threading.Tasks; | |||
using System.Linq; | |||
using Discord.Logging; | |||
using Discord.Rest; | |||
namespace Discord.Webhook | |||
{ | |||
public partial class DiscordWebhookClient | |||
public class DiscordWebhookClient : IDisposable | |||
{ | |||
public event Func<LogMessage, Task> Log { add { _logEvent.Add(value); } remove { _logEvent.Remove(value); } } | |||
internal readonly AsyncEvent<Func<LogMessage, Task>> _logEvent = new AsyncEvent<Func<LogMessage, Task>>(); | |||
private readonly ulong _webhookId; | |||
internal IWebhook Webhook; | |||
internal readonly Logger _restLogger; | |||
internal API.DiscordRestApiClient ApiClient { get; } | |||
internal LogManager LogManager { get; } | |||
/// <summary> Creates a new Webhook discord client. </summary> | |||
public DiscordWebhookClient(IWebhook webhook) | |||
: this(webhook.Id, webhook.Token, new DiscordRestConfig()) { } | |||
/// <summary> Creates a new Webhook discord client. </summary> | |||
public DiscordWebhookClient(ulong webhookId, string webhookToken) | |||
: this(webhookId, webhookToken, new DiscordRestConfig()) { } | |||
/// <summary> Creates a new Webhook discord client. </summary> | |||
public DiscordWebhookClient(ulong webhookId, string webhookToken, DiscordRestConfig config) | |||
: this(config) | |||
{ | |||
_webhookId = webhookId; | |||
ApiClient.LoginAsync(TokenType.Webhook, webhookToken).GetAwaiter().GetResult(); | |||
Webhook = WebhookClientHelper.GetWebhookAsync(this, webhookId).GetAwaiter().GetResult(); | |||
} | |||
/// <summary> Creates a new Webhook discord client. </summary> | |||
public DiscordWebhookClient(IWebhook webhook, DiscordRestConfig config) | |||
: this(config) | |||
{ | |||
Webhook = webhook; | |||
_webhookId = Webhook.Id; | |||
} | |||
private DiscordWebhookClient(DiscordRestConfig config) | |||
{ | |||
ApiClient = CreateApiClient(config); | |||
LogManager = new LogManager(config.LogLevel); | |||
LogManager.Message += async msg => await _logEvent.InvokeAsync(msg).ConfigureAwait(false); | |||
@@ -41,42 +58,40 @@ namespace Discord.Webhook | |||
await _restLogger.WarningAsync($"Rate limit triggered: {id ?? "null"}").ConfigureAwait(false); | |||
}; | |||
ApiClient.SentRequest += async (method, endpoint, millis) => await _restLogger.VerboseAsync($"{method} {endpoint}: {millis} ms").ConfigureAwait(false); | |||
ApiClient.LoginAsync(TokenType.Webhook, webhookToken).GetAwaiter().GetResult(); | |||
} | |||
private static API.DiscordRestApiClient CreateApiClient(DiscordRestConfig config) | |||
=> new API.DiscordRestApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent); | |||
public async Task SendMessageAsync(string text, bool isTTS = false, Embed[] embeds = null, | |||
/// <summary> Sends a message using to the channel for this webhook. Returns the ID of the created message. </summary> | |||
public Task<ulong> SendMessageAsync(string text, bool isTTS = false, IEnumerable<Embed> embeds = null, | |||
string username = null, string avatarUrl = null, RequestOptions options = null) | |||
{ | |||
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; | |||
await ApiClient.CreateWebhookMessageAsync(_webhookId, args, options).ConfigureAwait(false); | |||
} | |||
=> WebhookClientHelper.SendMessageAsync(this, text, isTTS, embeds, username, avatarUrl, options); | |||
#if FILESYSTEM | |||
public async Task SendFileAsync(string filePath, string text, bool isTTS = false, | |||
string username = null, string avatarUrl = null, RequestOptions options = null) | |||
/// <summary> Send a message to the channel for this webhook with an attachment. Returns the ID of the created message. </summary> | |||
public Task<ulong> SendFileAsync(string filePath, string text, bool isTTS = false, | |||
IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null, RequestOptions options = null) | |||
=> WebhookClientHelper.SendFileAsync(this, filePath, text, isTTS, embeds, username, avatarUrl, options); | |||
#endif | |||
/// <summary> Send a message to the channel for this webhook with an attachment. Returns the ID of the created message. </summary> | |||
public Task<ulong> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, | |||
IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null, RequestOptions options = null) | |||
=> WebhookClientHelper.SendFileAsync(this, stream, filename, text, isTTS, embeds, username, avatarUrl, options); | |||
/// <summary> Modifies the properties of this webhook. </summary> | |||
public Task ModifyWebhookAsync(Action<WebhookProperties> func, RequestOptions options = null) | |||
=> Webhook.ModifyAsync(func, options); | |||
/// <summary> Deletes this webhook from Discord and disposes the client. </summary> | |||
public async Task DeleteWebhookAsync(RequestOptions options = null) | |||
{ | |||
string filename = Path.GetFileName(filePath); | |||
using (var file = File.OpenRead(filePath)) | |||
await SendFileAsync(file, filename, text, isTTS, username, avatarUrl, options).ConfigureAwait(false); | |||
await Webhook.DeleteAsync(options).ConfigureAwait(false); | |||
Dispose(); | |||
} | |||
#endif | |||
public async Task SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, | |||
string username = null, string avatarUrl = null, RequestOptions options = null) | |||
public void Dispose() | |||
{ | |||
var args = new UploadWebhookFileParams(stream) { Filename = filename, Content = text, IsTTS = isTTS }; | |||
if (username != null) | |||
args.Username = username; | |||
if (avatarUrl != null) | |||
args.AvatarUrl = username; | |||
await ApiClient.UploadWebhookFileAsync(_webhookId, args, options).ConfigureAwait(false); | |||
ApiClient?.Dispose(); | |||
} | |||
} | |||
} |
@@ -0,0 +1,66 @@ | |||
using System; | |||
using System.Diagnostics; | |||
using System.Threading.Tasks; | |||
using Model = Discord.API.Webhook; | |||
namespace Discord.Webhook | |||
{ | |||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")] | |||
internal class RestInternalWebhook : IWebhook | |||
{ | |||
private DiscordWebhookClient _client; | |||
public ulong Id { get; } | |||
public ulong ChannelId { get; } | |||
public string Token { get; } | |||
public string Name { get; private set; } | |||
public string AvatarId { get; private set; } | |||
public ulong? GuildId { get; private set; } | |||
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id); | |||
internal RestInternalWebhook(DiscordWebhookClient apiClient, Model model) | |||
{ | |||
_client = apiClient; | |||
Id = model.Id; | |||
ChannelId = model.Id; | |||
Token = model.Token; | |||
} | |||
internal static RestInternalWebhook Create(DiscordWebhookClient client, Model model) | |||
{ | |||
var entity = new RestInternalWebhook(client, model); | |||
entity.Update(model); | |||
return entity; | |||
} | |||
internal void Update(Model model) | |||
{ | |||
if (model.Avatar.IsSpecified) | |||
AvatarId = model.Avatar.Value; | |||
if (model.GuildId.IsSpecified) | |||
GuildId = model.GuildId.Value; | |||
if (model.Name.IsSpecified) | |||
Name = model.Name.Value; | |||
} | |||
public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) | |||
=> CDN.GetUserAvatarUrl(Id, AvatarId, size, format); | |||
public async Task ModifyAsync(Action<WebhookProperties> func, RequestOptions options = null) | |||
{ | |||
var model = await WebhookClientHelper.ModifyAsync(_client, func, options); | |||
Update(model); | |||
} | |||
public Task DeleteAsync(RequestOptions options = null) | |||
=> WebhookClientHelper.DeleteAsync(_client, options); | |||
public override string ToString() => $"Webhook: {Name}:{Id}"; | |||
private string DebuggerDisplay => $"Webhook: {Name} ({Id})"; | |||
IUser IWebhook.Creator => null; | |||
ITextChannel IWebhook.Channel => null; | |||
IGuild IWebhook.Guild => null; | |||
} | |||
} |
@@ -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); | |||
} | |||
} | |||
} |