[Feature] Update webhook implementation (#2723)

* initial commit

* add partial guild & channel props

* Apply suggestions from code review

Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com>

---------

Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com>
This commit is contained in:
Misha133
2023-08-10 16:14:00 +03:00
committed by GitHub
parent 5c8d83cf85
commit 2b8584d07d
5 changed files with 115 additions and 41 deletions

View File

@@ -1,26 +1,39 @@
using Newtonsoft.Json;
namespace Discord.API
namespace Discord.API;
internal class Webhook
{
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("id")]
public ulong Id { 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("type")]
public WebhookType Type { get; set; }
[JsonProperty("user")]
public Optional<User> Creator { get; set; }
[JsonProperty("application_id")]
public ulong? ApplicationId { get; set; }
}
[JsonProperty("guild_id")]
public Optional<ulong?> GuildId { get; set; }
[JsonProperty("channel_id")]
public ulong? ChannelId { get; set; }
[JsonProperty("user")]
public Optional<User> Creator { get; set; }
[JsonProperty("name")]
public Optional<string> Name { get; set; }
[JsonProperty("avatar")]
public Optional<string> Avatar { get; set; }
[JsonProperty("token")]
public Optional<string> Token { get; set; }
[JsonProperty("application_id")]
public ulong? ApplicationId { get; set; }
[JsonProperty("source_guild")]
public Optional<PartialGuild> Guild { get; set; }
[JsonProperty("source_channel")]
public Optional<Channel> Channel { get; set; }
}

View File

@@ -9,6 +9,7 @@ namespace Discord.Rest
public class RestWebhook : RestEntity<ulong>, IWebhook, IUpdateable
{
#region RestWebhook
internal IGuild Guild { get; private set; }
internal IIntegrationChannel Channel { get; private set; }
@@ -16,43 +17,81 @@ namespace Discord.Rest
public string Token { get; }
/// <inheritdoc />
public ulong ChannelId { get; private set; }
public ulong? ChannelId { get; private set; }
/// <inheritdoc />
public string Name { get; private set; }
/// <inheritdoc />
public string AvatarId { get; private set; }
/// <inheritdoc />
public ulong? GuildId { get; private set; }
/// <inheritdoc />
public IUser Creator { get; private set; }
/// <inheritdoc />
public ulong? ApplicationId { get; private set; }
/// <inheritdoc />
public WebhookType Type { get; private set; }
/// <summary>
/// Gets the partial guild of the followed channel. <see langword="null"/> if <see cref="Type"/> is not <see cref="WebhookType.ChannelFollower"/>.
/// </summary>
public PartialGuild PartialGuild { get; private set; }
/// <summary>
/// Gets the id of the followed channel. <see langword="null"/> if <see cref="Type"/> is not <see cref="WebhookType.ChannelFollower"/>.
/// </summary>
public ulong? FollowedChannelId { get; private set; }
/// <summary>
/// Gets the name of the followed channel. <see langword="null"/> if <see cref="Type"/> is not <see cref="WebhookType.ChannelFollower"/>.
/// </summary>
public string FollowedChannelName { get; private set; }
/// <inheritdoc />
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
internal RestWebhook(BaseDiscordClient discord, IGuild guild, ulong id, string token, ulong channelId)
internal RestWebhook(BaseDiscordClient discord, IGuild guild, ulong id, string token, ulong? channelId, WebhookType type, PartialGuild partialGuild,
ulong? followedChannelId, string followedChannelName)
: base(discord, id)
{
Guild = guild;
Token = token;
ChannelId = channelId;
Type = type;
PartialGuild = partialGuild;
FollowedChannelId = followedChannelId;
FollowedChannelName = followedChannelName;
}
internal RestWebhook(BaseDiscordClient discord, IIntegrationChannel channel, ulong id, string token, ulong channelId)
: this(discord, channel.Guild, id, token, channelId)
internal RestWebhook(BaseDiscordClient discord, IIntegrationChannel channel, ulong id, string token, ulong? channelId, WebhookType type, PartialGuild partialGuild,
ulong? followedChannelId, string followedChannelName)
: this(discord, channel.Guild, id, token, channelId, type, partialGuild, followedChannelId, followedChannelName)
{
Channel = channel;
}
internal static RestWebhook Create(BaseDiscordClient discord, IGuild guild, Model model)
{
var entity = new RestWebhook(discord, guild, model.Id, model.Token, model.ChannelId);
var entity = new RestWebhook(discord, guild, model.Id, model.Token.GetValueOrDefault(null), model.ChannelId, model.Type,
model.Guild.IsSpecified ? PartialGuildExtensions.Create(model.Guild.Value) : null,
model.Channel.IsSpecified ? model.Channel.Value.Id : null,
model.Channel.IsSpecified ? model.Channel.Value.Name.GetValueOrDefault(null) : null
);
entity.Update(model);
return entity;
}
internal static RestWebhook Create(BaseDiscordClient discord, IIntegrationChannel channel, Model model)
{
var entity = new RestWebhook(discord, channel, model.Id, model.Token, model.ChannelId);
var entity = new RestWebhook(discord, channel, model.Id, model.Token.GetValueOrDefault(null), model.ChannelId, model.Type,
model.Guild.IsSpecified ? PartialGuildExtensions.Create(model.Guild.Value) : null,
model.Channel.IsSpecified ? model.Channel.Value.Id : null,
model.Channel.IsSpecified ? model.Channel.Value.Name.GetValueOrDefault(null) : null);
entity.Update(model);
return entity;
}