Skip to content

Commit

Permalink
poggers (#2796)
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha-133 committed Nov 18, 2023
1 parent b988a18 commit 89bebc3
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 18 deletions.
28 changes: 19 additions & 9 deletions src/Discord.Net.Core/Entities/Messages/IAttachment.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
using System;
using System.Collections.Generic;

namespace Discord
{
/// <summary>
/// Represents a message attachment found in a <see cref="IUserMessage"/>.
/// </summary>
public interface IAttachment
public interface IAttachment : ISnowflakeEntity
{
/// <summary>
/// Gets the ID of this attachment.
/// </summary>
/// <returns>
/// A snowflake ID associated with this attachment.
/// </returns>
ulong Id { get; }

/// <summary>
/// Gets the filename of this attachment.
/// </summary>
Expand Down Expand Up @@ -85,5 +80,20 @@ public interface IAttachment
/// Gets flags related to this to this attachment.
/// </summary>
public AttachmentFlags Flags { get; }

/// <summary>
/// Gets users who participated in the clip.
/// </summary>
public IReadOnlyCollection<IUser> ClipParticipants { get; }

/// <summary>
/// Gets the title of the clip. <see langword="null"/> if the clip has no title set.
/// </summary>
public string Title { get; }

/// <summary>
/// Gets the timestamp of the clip. <see langword="null"/> if the attachment is not a clip.
/// </summary>
public DateTimeOffset? ClipCreatedAt { get; }
}
}
10 changes: 10 additions & 0 deletions src/Discord.Net.Rest/API/Common/Attachment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System;

namespace Discord.API;

Expand Down Expand Up @@ -42,4 +43,13 @@ internal class Attachment

[JsonProperty("flags")]
public Optional<AttachmentFlags> Flags { get; set; }

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

[JsonProperty("clip_created_at")]
public Optional<DateTimeOffset> ClipCreatedAt { get; set; }

[JsonProperty("clip_participants")]
public Optional<User[]> ClipParticipants { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ internal async Task PopulateAsync(DiscordRestClient discord, RestGuild guild, IR
{
foreach (var attachment in resolved.Attachments.Value)
{
var discordAttachment = Attachment.Create(attachment.Value);
var discordAttachment = Attachment.Create(attachment.Value, discord);

Attachments.Add(ulong.Parse(attachment.Key), discordAttachment);
}
Expand Down
38 changes: 33 additions & 5 deletions src/Discord.Net.Rest/Entities/Messages/Attachment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using Discord.Rest;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Model = Discord.API.Attachment;

namespace Discord
Expand Down Expand Up @@ -32,11 +37,21 @@ public class Attachment : IAttachment
/// <inheritdoc />
public double? Duration { get; }

/// <inheritdoc cref="IAttachment.ClipParticipants" />
public IReadOnlyCollection<RestUser> ClipParticipants { get; }

/// <inheritdoc />
public string Title { get; }

/// <inheritdoc />
public DateTimeOffset? ClipCreatedAt { get; }

/// <inheritdoc />
public AttachmentFlags Flags { get; }

internal Attachment(ulong id, string filename, string url, string proxyUrl, int size, int? height, int? width,
bool? ephemeral, string description, string contentType, double? duration, string waveform, AttachmentFlags flags)
bool? ephemeral, string description, string contentType, double? duration, string waveform, AttachmentFlags flags, string title,
IReadOnlyCollection<RestUser> clipParticipants, DateTimeOffset? clipCreatedAt)
{
Id = id;
Filename = filename;
Expand All @@ -51,19 +66,29 @@ public class Attachment : IAttachment
Duration = duration;
Waveform = waveform;
Flags = flags;
Title = title;
ClipParticipants = clipParticipants;
ClipCreatedAt = clipCreatedAt;
}
internal static Attachment Create(Model model)

internal static Attachment Create(Model model, BaseDiscordClient discord)
{
return new Attachment(model.Id, model.Filename, model.Url, model.ProxyUrl, model.Size,
model.Height.IsSpecified ? model.Height.Value : (int?)null,
model.Width.IsSpecified ? model.Width.Value : (int?)null,
model.Height.IsSpecified ? model.Height.Value : null,
model.Width.IsSpecified ? model.Width.Value : null,
model.Ephemeral.ToNullable(), model.Description.GetValueOrDefault(),
model.ContentType.GetValueOrDefault(),
model.DurationSeconds.IsSpecified ? model.DurationSeconds.Value : null,
model.Waveform.GetValueOrDefault(null),
model.Flags.GetValueOrDefault(AttachmentFlags.None));
model.Flags.GetValueOrDefault(AttachmentFlags.None),
model.Title.GetValueOrDefault(null),
model.ClipParticipants.GetValueOrDefault(Array.Empty<API.User>()).Select(x => RestUser.Create(discord, x)).ToImmutableArray(),
model.ClipCreatedAt.IsSpecified ? model.ClipCreatedAt.Value : null);
}

/// <inheritdoc />
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);

/// <summary>
/// Returns the filename of this attachment.
/// </summary>
Expand All @@ -72,5 +97,8 @@ internal static Attachment Create(Model model)
/// </returns>
public override string ToString() => Filename;
private string DebuggerDisplay => $"{Filename} ({Size} bytes)";

/// <inheritdoc />
IReadOnlyCollection<IUser> IAttachment.ClipParticipants => ClipParticipants;
}
}
2 changes: 1 addition & 1 deletion src/Discord.Net.Rest/Entities/Messages/RestUserMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ internal override void Update(Model model)
{
var attachments = ImmutableArray.CreateBuilder<Attachment>(value.Length);
for (int i = 0; i < value.Length; i++)
attachments.Add(Attachment.Create(value[i]));
attachments.Add(Attachment.Create(value[i], Discord));
_attachments = attachments.ToImmutable();
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ internal SocketResolvableData(DiscordSocketClient discord, ulong? guildId, T mod
{
foreach (var attachment in resolved.Attachments.Value)
{
var discordAttachment = Attachment.Create(attachment.Value);
var discordAttachment = Attachment.Create(attachment.Value, discord);

Attachments.Add(ulong.Parse(attachment.Key), discordAttachment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ internal override void Update(ClientState state, Model model)
{
var attachments = ImmutableArray.CreateBuilder<Attachment>(value.Length);
for (int i = 0; i < value.Length; i++)
attachments.Add(Attachment.Create(value[i]));
attachments.Add(Attachment.Create(value[i], Discord));
_attachments = attachments.ToImmutable();
}
else
Expand Down

0 comments on commit 89bebc3

Please sign in to comment.