Browse Source

Webhook REST implementation.

pull/843/head
Alex Gravely 8 years ago
parent
commit
5379249a2e
9 changed files with 41 additions and 40 deletions
  1. +0
    -2
      src/Discord.Net.Rest/API/Rest/CreateWebhookMessageParams.cs
  2. +2
    -0
      src/Discord.Net.Rest/API/Rest/ModifyWebhookParams.cs
  3. +5
    -1
      src/Discord.Net.Rest/API/Rest/UploadWebhookFileParams.cs
  4. +1
    -1
      src/Discord.Net.Rest/BaseDiscordClient.cs
  5. +2
    -2
      src/Discord.Net.Rest/ClientHelper.cs
  6. +4
    -8
      src/Discord.Net.Rest/DiscordRestApiClient.cs
  7. +4
    -4
      src/Discord.Net.Rest/DiscordRestClient.cs
  8. +16
    -20
      src/Discord.Net.Rest/Entities/Webhooks/RestWebhook.cs
  9. +7
    -2
      src/Discord.Net.Rest/Entities/Webhooks/WebhookHelper.cs

+ 0
- 2
src/Discord.Net.Rest/API/Rest/CreateWebhookMessageParams.cs View File

@@ -8,8 +8,6 @@ namespace Discord.API.Rest
{
[JsonProperty("content")]
public string Content { get; }
[JsonProperty("wait")]
public bool ReturnCreatedMessage { get; set; } = true;

[JsonProperty("nonce")]
public Optional<string> Nonce { get; set; }


+ 2
- 0
src/Discord.Net.Rest/API/Rest/ModifyWebhookParams.cs View File

@@ -10,5 +10,7 @@ namespace Discord.API.Rest
public Optional<string> Name { get; set; }
[JsonProperty("avatar")]
public Optional<Image?> Avatar { get; set; }
[JsonProperty("channel_id")]
public Optional<ulong> ChannelId { get; set; }
}
}

+ 5
- 1
src/Discord.Net.Rest/API/Rest/UploadWebhookFileParams.cs View File

@@ -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;
}
}


+ 1
- 1
src/Discord.Net.Rest/BaseDiscordClient.cs View File

@@ -164,7 +164,7 @@ namespace Discord.Rest
Task<IVoiceRegion> IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options)
=> Task.FromResult<IVoiceRegion>(null);

Task<IWebhook> IDiscordClient.GetWebhookAsync(ulong id, string webhookToken, RequestOptions options)
Task<IWebhook> IDiscordClient.GetWebhookAsync(ulong id, RequestOptions options)
=> Task.FromResult<IWebhook>(null);

Task IDiscordClient.StartAsync()


+ 2
- 2
src/Discord.Net.Rest/ClientHelper.cs View File

@@ -144,9 +144,9 @@ namespace Discord.Rest
return null;
}

public static async Task<RestWebhook> GetWebhookAsync(BaseDiscordClient client, ulong id, string webhookToken, RequestOptions options)
public static async Task<RestWebhook> GetWebhookAsync(BaseDiscordClient client, ulong id, RequestOptions options)
{
var model = await client.ApiClient.GetWebhookAsync(id, webhookToken);
var model = await client.ApiClient.GetWebhookAsync(id);
if (model != null)
return RestWebhook.Create(client, (IGuild)null, model);
return null;


+ 4
- 8
src/Discord.Net.Rest/DiscordRestApiClient.cs View File

@@ -486,12 +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);

if (args.ReturnCreatedMessage)
return await SendJsonAsync<Message>("POST", () => $"webhooks/{webhookId}/{AuthToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);

await SendJsonAsync("POST", () => $"webhooks/{webhookId}/{AuthToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);
return null;
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)
{
@@ -507,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.");
@@ -526,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)
{


+ 4
- 4
src/Discord.Net.Rest/DiscordRestClient.cs View File

@@ -92,8 +92,8 @@ namespace Discord.Rest
public Task<RestVoiceRegion> GetVoiceRegionAsync(string id, RequestOptions options = null)
=> ClientHelper.GetVoiceRegionAsync(this, id, options);
/// <inheritdoc />
public Task<RestWebhook> GetWebhookAsync(ulong id, string webhookToken = null, RequestOptions options = null)
=> ClientHelper.GetWebhookAsync(this, id, webhookToken, options);
public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null)
=> ClientHelper.GetWebhookAsync(this, id, options);

//IDiscordClient
async Task<IApplication> IDiscordClient.GetApplicationInfoAsync(RequestOptions options)
@@ -164,7 +164,7 @@ namespace Discord.Rest
async Task<IVoiceRegion> IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options)
=> await GetVoiceRegionAsync(id, options).ConfigureAwait(false);

async Task<IWebhook> IDiscordClient.GetWebhookAsync(ulong id, string webhookToken, RequestOptions options)
=> await GetWebhookAsync(id, webhookToken, options);
async Task<IWebhook> IDiscordClient.GetWebhookAsync(ulong id, RequestOptions options)
=> await GetWebhookAsync(id, options);
}
}

+ 16
- 20
src/Discord.Net.Rest/Entities/Webhooks/RestWebhook.cs View File

@@ -10,41 +10,45 @@ namespace Discord.Rest
{
internal IGuild Guild { get; private set; }
internal ITextChannel Channel { get; private set; }
public string Token { 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 ChannelId { get; private set; }
public ulong GuildId { 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)
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)
: this(discord, channel.Guild, id)
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);
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);
var entity = new RestWebhook(discord, channel, model.Id, model.Token, model.ChannelId);
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)
@@ -73,16 +77,8 @@ namespace Discord.Rest
public Task DeleteAsync(RequestOptions options = null)
=> WebhookHelper.DeleteAsync(this, Discord, options);

public async Task<RestTextChannel> GetChannelAsync(RequestOptions options = null)
{
Channel = await ClientHelper.GetChannelAsync(Discord, ChannelId, options) as RestTextChannel;
Guild = Channel.Guild;

return Channel as RestTextChannel;
}

public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id})";
public override string ToString() => $"Webhook: {Name}:{Id}";
private string DebuggerDisplay => $"Webhook: {Name} ({Id})";

//IWebhook
IGuild IWebhook.Guild


+ 7
- 2
src/Discord.Net.Rest/Entities/Webhooks/WebhookHelper.cs View File

@@ -1,6 +1,6 @@
using Discord.API.Rest;
using System;
using System;
using System.Threading.Tasks;
using Discord.API.Rest;
using ImageModel = Discord.API.Image;
using Model = Discord.API.Webhook;

@@ -22,6 +22,11 @@ namespace Discord.Rest
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)


Loading…
Cancel
Save