Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Clips support #2796

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ internal Attachment(ulong id, string filename, string url, string proxyUrl, int
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 @@ -80,7 +80,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 @@ -84,7 +84,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