@@ -1,4 +1,6 @@ | |||||
using System; | using System; | ||||
using System.Collections.Generic; | |||||
using System.IO; | |||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
namespace Discord | namespace Discord | ||||
@@ -13,5 +15,12 @@ namespace Discord | |||||
/// <summary> Modifies this text channel. </summary> | /// <summary> Modifies this text channel. </summary> | ||||
Task ModifyAsync(Action<TextChannelProperties> func, RequestOptions options = null); | 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); | |||||
} | } | ||||
} | } |
@@ -117,5 +117,10 @@ namespace Discord | |||||
Task DownloadUsersAsync(); | Task DownloadUsersAsync(); | ||||
/// <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> | /// <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); | 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); | |||||
} | } | ||||
} | } |
@@ -1,6 +1,5 @@ | |||||
namespace Discord | namespace Discord | ||||
{ | { | ||||
//TODO: Add webhook endpoints | |||||
public interface IWebhookUser : IGuildUser | public interface IWebhookUser : IGuildUser | ||||
{ | { | ||||
ulong WebhookId { get; } | 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, string webhookToken = null, RequestOptions options = null); | |||||
} | |||||
} |
@@ -0,0 +1,27 @@ | |||||
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; } | |||||
} | |||||
} |
@@ -34,5 +34,7 @@ namespace Discord | |||||
Task<IReadOnlyCollection<IVoiceRegion>> GetVoiceRegionsAsync(RequestOptions options = null); | Task<IReadOnlyCollection<IVoiceRegion>> GetVoiceRegionsAsync(RequestOptions options = null); | ||||
Task<IVoiceRegion> GetVoiceRegionAsync(string id, RequestOptions options = null); | Task<IVoiceRegion> GetVoiceRegionAsync(string id, RequestOptions options = null); | ||||
Task<IWebhook> GetWebhookAsync(ulong id, string webhookToken = null, 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; } | |||||
} | |||||
} |
@@ -8,6 +8,8 @@ namespace Discord.API.Rest | |||||
{ | { | ||||
[JsonProperty("content")] | [JsonProperty("content")] | ||||
public string Content { get; } | public string Content { get; } | ||||
[JsonProperty("wait")] | |||||
public bool ReturnCreatedMessage { get; set; } | |||||
[JsonProperty("nonce")] | [JsonProperty("nonce")] | ||||
public Optional<string> Nonce { get; set; } | public Optional<string> Nonce { 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,14 @@ | |||||
#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; } | |||||
} | |||||
} |
@@ -164,6 +164,9 @@ namespace Discord.Rest | |||||
Task<IVoiceRegion> IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options) | Task<IVoiceRegion> IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options) | ||||
=> Task.FromResult<IVoiceRegion>(null); | => Task.FromResult<IVoiceRegion>(null); | ||||
Task<IWebhook> IDiscordClient.GetWebhookAsync(ulong id, string webhookToken, RequestOptions options) | |||||
=> Task.FromResult<IWebhook>(null); | |||||
Task IDiscordClient.StartAsync() | Task IDiscordClient.StartAsync() | ||||
=> Task.Delay(0); | => Task.Delay(0); | ||||
Task IDiscordClient.StopAsync() | Task IDiscordClient.StopAsync() | ||||
@@ -144,6 +144,14 @@ namespace Discord.Rest | |||||
return null; | return null; | ||||
} | } | ||||
public static async Task<RestWebhook> GetWebhookAsync(BaseDiscordClient client, ulong id, string webhookToken, RequestOptions options) | |||||
{ | |||||
var model = await client.ApiClient.GetWebhookAsync(id, webhookToken); | |||||
if (model != null) | |||||
return RestWebhook.Create(client, (IGuild)null, model); | |||||
return null; | |||||
} | |||||
public static async Task<IReadOnlyCollection<RestVoiceRegion>> GetVoiceRegionsAsync(BaseDiscordClient client, RequestOptions options) | public static async Task<IReadOnlyCollection<RestVoiceRegion>> GetVoiceRegionsAsync(BaseDiscordClient client, RequestOptions options) | ||||
{ | { | ||||
var models = await client.ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false); | var models = await client.ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false); | ||||
@@ -473,11 +473,13 @@ namespace Discord.API | |||||
var ids = new BucketIds(channelId: channelId); | var ids = new BucketIds(channelId: channelId); | ||||
return await SendJsonAsync<Message>("POST", () => $"channels/{channelId}/messages", args, ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); | 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, string webhookToken = null, RequestOptions options = null) | |||||
{ | { | ||||
if (AuthTokenType != TokenType.Webhook) | |||||
if (AuthTokenType != TokenType.Webhook && string.IsNullOrWhiteSpace(webhookToken)) | |||||
throw new InvalidOperationException($"This operation may only be called with a {nameof(TokenType.Webhook)} token."); | throw new InvalidOperationException($"This operation may only be called with a {nameof(TokenType.Webhook)} token."); | ||||
webhookToken = webhookToken ?? AuthToken; | |||||
Preconditions.NotNull(args, nameof(args)); | Preconditions.NotNull(args, nameof(args)); | ||||
Preconditions.NotEqual(webhookId, 0, nameof(webhookId)); | Preconditions.NotEqual(webhookId, 0, nameof(webhookId)); | ||||
if (!args.Embeds.IsSpecified || args.Embeds.Value == null || args.Embeds.Value.Length == 0) | if (!args.Embeds.IsSpecified || args.Embeds.Value == null || args.Embeds.Value.Length == 0) | ||||
@@ -487,7 +489,11 @@ namespace Discord.API | |||||
throw new ArgumentException($"Message content is too long, length must be less or equal to {DiscordConfig.MaxMessageSize}.", nameof(args.Content)); | throw new ArgumentException($"Message content is too long, length must be less or equal to {DiscordConfig.MaxMessageSize}.", nameof(args.Content)); | ||||
options = RequestOptions.CreateOrClone(options); | options = RequestOptions.CreateOrClone(options); | ||||
await SendJsonAsync("POST", () => $"webhooks/{webhookId}/{AuthToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); | |||||
if (args.ReturnCreatedMessage) | |||||
return await SendJsonAsync<Message>("POST", () => $"webhooks/{webhookId}/{webhookToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); | |||||
await SendJsonAsync("POST", () => $"webhooks/{webhookId}/{webhookToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); | |||||
return null; | |||||
} | } | ||||
public async Task<Message> UploadFileAsync(ulong channelId, UploadFileParams args, RequestOptions options = null) | public async Task<Message> UploadFileAsync(ulong channelId, UploadFileParams args, RequestOptions options = null) | ||||
{ | { | ||||
@@ -1154,6 +1160,79 @@ namespace Discord.API | |||||
return await SendAsync<IReadOnlyCollection<VoiceRegion>>("GET", () => $"guilds/{guildId}/regions", ids, options: options).ConfigureAwait(false); | 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); | |||||
return await SendJsonAsync<Webhook>("POST", () => $"channels/{channelId}/webhooks", args, new BucketIds(), options: options); | |||||
} | |||||
public async Task<Webhook> GetWebhookAsync(ulong webhookId, string webhookToken = null, RequestOptions options = null) | |||||
{ | |||||
Preconditions.NotEqual(webhookId, 0, nameof(webhookId)); | |||||
options = RequestOptions.CreateOrClone(options); | |||||
if (!string.IsNullOrWhiteSpace(webhookToken)) | |||||
{ | |||||
webhookToken = "/" + webhookToken; | |||||
options.IgnoreState = true; | |||||
} | |||||
try | |||||
{ | |||||
return await SendAsync<Webhook>("GET", () => $"webhooks/{webhookId}{webhookToken}", 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, string webhookToken = null, 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 (!string.IsNullOrWhiteSpace(webhookToken)) | |||||
{ | |||||
webhookToken = "/" + webhookToken; | |||||
options.IgnoreState = true; | |||||
} | |||||
return await SendJsonAsync<Webhook>("PATCH", () => $"webhooks/{webhookId}{webhookToken}", args, new BucketIds(), options: options).ConfigureAwait(false); | |||||
} | |||||
public async Task DeleteWebhookAsync(ulong webhookId, string webhookToken = null, RequestOptions options = null) | |||||
{ | |||||
Preconditions.NotEqual(webhookId, 0, nameof(webhookId)); | |||||
options = RequestOptions.CreateOrClone(options); | |||||
options.IgnoreState = true; | |||||
if (!string.IsNullOrWhiteSpace(webhookToken)) | |||||
{ | |||||
webhookToken = "/" + webhookToken; | |||||
options.IgnoreState = true; | |||||
} | |||||
await SendAsync("DELETE", () => $"webhooks/{webhookId}{webhookToken}", 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 | //Helpers | ||||
protected void CheckState() | protected void CheckState() | ||||
{ | { | ||||
@@ -91,6 +91,9 @@ namespace Discord.Rest | |||||
/// <inheritdoc /> | /// <inheritdoc /> | ||||
public Task<RestVoiceRegion> GetVoiceRegionAsync(string id, RequestOptions options = null) | public Task<RestVoiceRegion> GetVoiceRegionAsync(string id, RequestOptions options = null) | ||||
=> ClientHelper.GetVoiceRegionAsync(this, id, options); | => ClientHelper.GetVoiceRegionAsync(this, id, options); | ||||
/// <inheritdoc /> | |||||
public Task<RestWebhook> GetWebhookAsync(ulong id, string webhookToken = null, RequestOptions options = null) | |||||
=> ClientHelper.GetWebhookAsync(this, id, webhookToken, options); | |||||
//IDiscordClient | //IDiscordClient | ||||
async Task<IApplication> IDiscordClient.GetApplicationInfoAsync(RequestOptions options) | async Task<IApplication> IDiscordClient.GetApplicationInfoAsync(RequestOptions options) | ||||
@@ -160,5 +163,8 @@ namespace Discord.Rest | |||||
=> await GetVoiceRegionsAsync(options).ConfigureAwait(false); | => await GetVoiceRegionsAsync(options).ConfigureAwait(false); | ||||
async Task<IVoiceRegion> IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options) | async Task<IVoiceRegion> IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options) | ||||
=> await GetVoiceRegionAsync(id, options).ConfigureAwait(false); | => await GetVoiceRegionAsync(id, options).ConfigureAwait(false); | ||||
async Task<IWebhook> IDiscordClient.GetWebhookAsync(ulong id, string webhookToken, RequestOptions options) | |||||
=> await GetWebhookAsync(id, webhookToken, options); | |||||
} | } | ||||
} | } |
@@ -7,6 +7,7 @@ using System.Linq; | |||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
using Model = Discord.API.Channel; | using Model = Discord.API.Channel; | ||||
using UserModel = Discord.API.User; | using UserModel = Discord.API.User; | ||||
using WebhookModel = Discord.API.Webhook; | |||||
namespace Discord.Rest | namespace Discord.Rest | ||||
{ | { | ||||
@@ -280,6 +281,30 @@ namespace Discord.Rest | |||||
RequestOptions options) | RequestOptions options) | ||||
=> new TypingNotifier(client, channel, 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 | //Helpers | ||||
private static IUser GetAuthor(BaseDiscordClient client, IGuild guild, UserModel model, ulong? webhookId) | 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) | public IDisposable EnterTypingState(RequestOptions options = null) | ||||
=> ChannelHelper.EnterTypingState(this, Discord, options); | => 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)"; | 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 | //IMessageChannel | ||||
async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options) | async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options) | ||||
{ | { | ||||
@@ -253,5 +253,19 @@ namespace Discord.Rest | |||||
model = await client.ApiClient.BeginGuildPruneAsync(guild.Id, args, options).ConfigureAwait(false); | model = await client.ApiClient.BeginGuildPruneAsync(guild.Id, args, options).ConfigureAwait(false); | ||||
return model.Pruned; | 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(); | |||||
} | |||||
} | } | ||||
} | } |
@@ -257,6 +257,12 @@ namespace Discord.Rest | |||||
public Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null) | public Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null) | ||||
=> GuildHelper.PruneUsersAsync(this, Discord, days, simulate, options); | => 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; | public override string ToString() => Name; | ||||
private string DebuggerDisplay => $"{Name} ({Id})"; | private string DebuggerDisplay => $"{Name} ({Id})"; | ||||
@@ -386,5 +392,10 @@ namespace Discord.Rest | |||||
return ImmutableArray.Create<IGuildUser>(); | return ImmutableArray.Create<IGuildUser>(); | ||||
} | } | ||||
Task IGuild.DownloadUsersAsync() { throw new NotSupportedException(); } | 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,89 @@ | |||||
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; } | |||||
internal ITextChannel Channel { get; } | |||||
public string Token { get; private set; } | |||||
public string Name { get; private set; } | |||||
public string AvatarId { get; private set; } | |||||
public ulong ChannelId { 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) | |||||
: base(discord, id) | |||||
{ | |||||
Guild = guild; | |||||
} | |||||
internal RestWebhook(BaseDiscordClient discord, ITextChannel channel, ulong id) | |||||
: this(discord, channel.Guild, id) | |||||
{ | |||||
Channel = channel; | |||||
} | |||||
internal static RestWebhook Create(BaseDiscordClient discord, IGuild guild, Model model) | |||||
{ | |||||
var entity = new RestWebhook(discord, guild, model.Id); | |||||
entity.Update(model); | |||||
return entity; | |||||
} | |||||
internal static RestWebhook Create(BaseDiscordClient discord, ITextChannel channel, Model model) | |||||
{ | |||||
var entity = new RestWebhook(discord, channel, model.Id); | |||||
entity.Update(model); | |||||
return entity; | |||||
} | |||||
internal void Update(Model model) | |||||
{ | |||||
Token = model.Token; | |||||
ChannelId = model.ChannelId; | |||||
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, Token, 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, string webhookToken = null, RequestOptions options = null) | |||||
{ | |||||
var model = await WebhookHelper.ModifyAsync(this, Discord, func, webhookToken, options).ConfigureAwait(false); | |||||
Update(model); | |||||
} | |||||
public Task DeleteAsync(string webhookToken = null, RequestOptions options = null) | |||||
=> WebhookHelper.DeleteAsync(this, Discord, webhookToken, options); | |||||
public override string ToString() => Name; | |||||
private string DebuggerDisplay => $"{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, string webhookToken, RequestOptions options) | |||||
=> ModifyAsync(func, webhookToken, options); | |||||
Task IDeletable.DeleteAsync(RequestOptions options) | |||||
=> DeleteAsync(Token, options); | |||||
} | |||||
} |
@@ -0,0 +1,34 @@ | |||||
using Discord.API.Rest; | |||||
using System; | |||||
using System.Threading.Tasks; | |||||
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, string webhookToken, 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); | |||||
return await client.ApiClient.ModifyWebhookAsync(webhook.Id, apiArgs, webhookToken, options).ConfigureAwait(false); | |||||
} | |||||
public static async Task DeleteAsync(IWebhook webhook, BaseDiscordClient client, string webhookToken, | |||||
RequestOptions options) | |||||
{ | |||||
await client.ApiClient.DeleteWebhookAsync(webhook.Id, webhookToken, options).ConfigureAwait(false); | |||||
} | |||||
} | |||||
} |
@@ -68,11 +68,25 @@ namespace Discord.Rpc | |||||
=> ChannelHelper.TriggerTypingAsync(this, Discord, options); | => ChannelHelper.TriggerTypingAsync(this, Discord, options); | ||||
public IDisposable EnterTypingState(RequestOptions options = null) | public IDisposable EnterTypingState(RequestOptions options = null) | ||||
=> ChannelHelper.EnterTypingState(this, Discord, options); | => 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)"; | private string DebuggerDisplay => $"{Name} ({Id}, Text)"; | ||||
//ITextChannel | //ITextChannel | ||||
string ITextChannel.Topic { get { throw new NotSupportedException(); } } | 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 | //IMessageChannel | ||||
async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options) | 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; | 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)"; | private string DebuggerDisplay => $"{Name} ({Id}, Text)"; | ||||
internal new SocketTextChannel Clone() => MemberwiseClone() as SocketTextChannel; | 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 | //IGuildChannel | ||||
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | ||||
=> Task.FromResult<IGuildUser>(GetUser(id)); | => Task.FromResult<IGuildUser>(GetUser(id)); | ||||
@@ -433,6 +433,12 @@ namespace Discord.WebSocket | |||||
_downloaderPromise.TrySetResultAsync(true); | _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); | |||||
//Voice States | //Voice States | ||||
internal async Task<SocketVoiceState> AddOrUpdateVoiceStateAsync(ClientState state, VoiceStateModel model) | internal async Task<SocketVoiceState> AddOrUpdateVoiceStateAsync(ClientState state, VoiceStateModel model) | ||||
{ | { | ||||
@@ -672,5 +678,10 @@ namespace Discord.WebSocket | |||||
Task<IGuildUser> IGuild.GetOwnerAsync(CacheMode mode, RequestOptions options) | Task<IGuildUser> IGuild.GetOwnerAsync(CacheMode mode, RequestOptions options) | ||||
=> Task.FromResult<IGuildUser>(Owner); | => Task.FromResult<IGuildUser>(Owner); | ||||
Task IGuild.DownloadUsersAsync() { throw new NotSupportedException(); } | 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); | |||||
} | } | ||||
} | } |
@@ -56,7 +56,7 @@ namespace Discord.Webhook | |||||
args.Username = username; | args.Username = username; | ||||
if (avatarUrl != null) | if (avatarUrl != null) | ||||
args.AvatarUrl = avatarUrl; | args.AvatarUrl = avatarUrl; | ||||
await ApiClient.CreateWebhookMessageAsync(_webhookId, args, options).ConfigureAwait(false); | |||||
await ApiClient.CreateWebhookMessageAsync(_webhookId, args, options: options).ConfigureAwait(false); | |||||
} | } | ||||
#if FILESYSTEM | #if FILESYSTEM | ||||