Skip to content

Commit

Permalink
Add voice messages (discord/discord-api-docs#6082)
Browse files Browse the repository at this point in the history
  • Loading branch information
KubaZ2 committed Apr 21, 2023
1 parent 2a2a580 commit c41204f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
12 changes: 7 additions & 5 deletions NetCord/Attachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ public class Attachment : Entity, IJsonModel<JsonModels.JsonAttachment>
JsonModels.JsonAttachment IJsonModel<JsonModels.JsonAttachment>.JsonModel => _jsonModel;
private protected readonly JsonModels.JsonAttachment _jsonModel;

public Attachment(JsonModels.JsonAttachment jsonModel)
{
_jsonModel = jsonModel;
}

public override ulong Id => _jsonModel.Id;

/// <summary>
Expand Down Expand Up @@ -44,15 +49,12 @@ public class Attachment : Entity, IJsonModel<JsonModels.JsonAttachment>
/// </summary>
public bool Ephemeral => _jsonModel.Ephemeral;

private protected Attachment(JsonModels.JsonAttachment jsonModel)
{
_jsonModel = jsonModel;
}

public static Attachment CreateFromJson(JsonModels.JsonAttachment jsonModel)
{
if (jsonModel.Width.HasValue)
return new ImageAttachment(jsonModel);
else if (jsonModel.DurationSeconds.HasValue)
return new VoiceAttachment(jsonModel);
else
return new Attachment(jsonModel);
}
Expand Down
8 changes: 4 additions & 4 deletions NetCord/ImageAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

public class ImageAttachment : Attachment
{
public ImageAttachment(JsonModels.JsonAttachment jsonModel) : base(jsonModel)
{
}

/// <summary>
/// Height of file.
/// </summary>
Expand All @@ -11,8 +15,4 @@ public class ImageAttachment : Attachment
/// Width of file.
/// </summary>
public int Width => _jsonModel.Width.GetValueOrDefault();

public ImageAttachment(JsonModels.JsonAttachment jsonModel) : base(jsonModel)
{
}
}
6 changes: 6 additions & 0 deletions NetCord/JsonModels/JsonAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public partial class JsonAttachment : JsonEntity
[JsonPropertyName("ephemeral")]
public bool Ephemeral { get; set; }

[JsonPropertyName("duration_secs")]
public double? DurationSeconds { get; set; }

[JsonPropertyName("waveform")]
public string? Waveform { get; set; }

[JsonSerializable(typeof(JsonAttachment))]
public partial class JsonAttachmentSerializerContext : JsonSerializerContext
{
Expand Down
15 changes: 15 additions & 0 deletions NetCord/MessageFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,59 @@ public enum MessageFlags : uint
/// This message has been published to subscribed channels (via Channel Following).
/// </summary>
Crossposted = 1 << 0,

/// <summary>
/// This message originated from a message in another channel (via Channel Following).
/// </summary>
IsCrosspost = 1 << 1,

/// <summary>
/// Do not include any embeds when serializing this message.
/// </summary>
SuppressEmbeds = 1 << 2,

/// <summary>
/// The source message for this crosspost has been deleted (via Channel Following).
/// </summary>
SourceMessageDeleted = 1 << 3,

/// <summary>
/// This message came from the urgent message system.
/// </summary>
Urgent = 1 << 4,

/// <summary>
/// This message has an associated thread, with the same id as the message.
/// </summary>
HasThread = 1 << 5,

/// <summary>
/// This message is only visible to the user who invoked the Interaction.
/// </summary>
Ephemeral = 1 << 6,

/// <summary>
/// This message is an Interaction Response and the bot is "thinking".
/// </summary>
Loading = 1 << 7,

/// <summary>
/// This message failed to mention some roles and add their members to the thread.
/// </summary>
FailedToMentionSomeRolesInThread = 1 << 8,

/// <summary>
/// This message contains an abusive website link, pops up a warning when clicked.
/// </summary>
ShouldShowLinkNotDiscordWarning = 1 << 10,

/// <summary>
/// This message will not trigger push and desktop notifications.
/// </summary>
SuppressNotifications = 1 << 12,

/// <summary>
/// This message is a voice message.
/// </summary>
IsVoiceMessage = 1 << 13,
}
5 changes: 5 additions & 0 deletions NetCord/Permissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,9 @@ public enum Permissions : ulong
/// Allows the usage of custom soundboard sounds from other servers.
/// </summary>
UseExternalSounds = 1uL << 45,

/// <summary>
/// Allows sending voice messages.
/// </summary>
SendVoiceMessages = 1uL << 46,
}
20 changes: 20 additions & 0 deletions NetCord/VoiceAttachment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NetCord.JsonModels;

namespace NetCord;

public class VoiceAttachment : Attachment
{
public VoiceAttachment(JsonAttachment jsonModel) : base(jsonModel)
{
}

/// <summary>
/// The duration of the audio file.
/// </summary>
public TimeSpan Duration => TimeSpan.FromSeconds(_jsonModel.DurationSeconds.GetValueOrDefault());

/// <summary>
/// Byte array representing a sampled waveform. It is intended to be a preview of the entire voice message. Clients sample the recording at most once per 100 milliseconds, but will downsample so that no more than 256 datapoints are in the waveform.
/// </summary>
public byte[] Waveform => Convert.FromBase64String(_jsonModel.Waveform!);
}

0 comments on commit c41204f

Please sign in to comment.