Skip to content

Commit 5904ecd

Browse files
authored
[Feature] Support sending voice messages (#3024)
* yup * a
1 parent 8b92969 commit 5904ecd

6 files changed

Lines changed: 52 additions & 5 deletions

File tree

src/Discord.Net.Core/Entities/Messages/FileAttachment.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ public struct FileAttachment : IDisposable
2727
/// </summary>
2828
public bool IsThumbnail { get; set; }
2929

30+
/// <summary>
31+
/// Gets or sets the duration of a voice message. <see langword="null"/> if the attachment is not a voice message.
32+
/// </summary>
33+
public double? DurationSeconds { get; set; }
34+
35+
/// <summary>
36+
/// Gets or sets bytearray representing a sampled waveform. <see langword="null"/> if the attachment is not a voice message.
37+
/// </summary>
38+
public byte[] Waveform { get; set; }
39+
3040
#pragma warning disable IDISP008
3141
/// <summary>
3242
/// Gets the stream containing the file content.
@@ -44,7 +54,7 @@ public struct FileAttachment : IDisposable
4454
/// <param name="description">The description of the attachment.</param>
4555
/// <param name="isSpoiler">Whether or not the attachment is a spoiler.</param>
4656
/// <param name="isThumbnail">Whether or not this attachment should be a thumbnail for a media channel post.</param>
47-
public FileAttachment(Stream stream, string fileName, string description = null, bool isSpoiler = false, bool isThumbnail = false)
57+
public FileAttachment(Stream stream, string fileName, string description = null, bool isSpoiler = false, bool isThumbnail = false, double? durationSeconds = null, byte[] waveform = null)
4858
{
4959
_isDisposed = false;
5060
FileName = fileName;
@@ -57,6 +67,8 @@ public FileAttachment(Stream stream, string fileName, string description = null,
5767
}
5868
catch { }
5969
IsSpoiler = isSpoiler;
70+
DurationSeconds = durationSeconds;
71+
Waveform = waveform;
6072
}
6173

6274
/// <summary>
@@ -91,14 +103,16 @@ public FileAttachment(Stream stream, string fileName, string description = null,
91103
/// <exception cref="FileNotFoundException">The file specified in <paramref name="path" /> was not found.
92104
/// </exception>
93105
/// <exception cref="IOException">An I/O error occurred while opening the file. </exception>
94-
public FileAttachment(string path, string fileName = null, string description = null, bool isSpoiler = false, bool isThumbnail = false)
106+
public FileAttachment(string path, string fileName = null, string description = null, bool isSpoiler = false, bool isThumbnail = false, double? durationSeconds = null, byte[] waveform = null)
95107
{
96108
_isDisposed = false;
97109
Stream = File.OpenRead(path);
98110
FileName = fileName ?? Path.GetFileName(path);
99111
Description = description;
100112
IsSpoiler = isSpoiler;
101113
IsThumbnail = isThumbnail;
114+
DurationSeconds = durationSeconds;
115+
Waveform = waveform;
102116
}
103117

104118
public void Dispose()

src/Discord.Net.Core/Entities/Messages/IAttachment.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public interface IAttachment : ISnowflakeEntity
7676
/// </summary>
7777
public string Waveform { get; }
7878

79+
/// <summary>
80+
/// Gets the bytearray representing a sampled waveform. <see langword="null"/> if the attachment is not a voice message.
81+
/// </summary>
82+
public byte[] WaveformBytes { get; }
83+
7984
/// <summary>
8085
/// Gets flags related to this to this attachment.
8186
/// </summary>

src/Discord.Net.Rest/API/Rest/UploadFileParams.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Discord.Net.Converters;
22
using Discord.Net.Rest;
33
using Newtonsoft.Json;
4+
using System;
45
using System.Collections.Generic;
56
using System.IO;
67
using System.Linq;
@@ -34,6 +35,9 @@ public IReadOnlyDictionary<string, object> ToDictionary()
3435
{
3536
var d = new Dictionary<string, object>();
3637

38+
if (Files.Any(x => x.Waveform is not null && x.DurationSeconds is not null))
39+
Flags = Flags.GetValueOrDefault(MessageFlags.None) | MessageFlags.VoiceMessage;
40+
3741
var payload = new Dictionary<string, object>();
3842
if (Content.IsSpecified)
3943
payload["content"] = Content.Value;
@@ -71,7 +75,11 @@ public IReadOnlyDictionary<string, object> ToDictionary()
7175
{
7276
id = (ulong)n,
7377
filename = filename,
74-
description = attachment.Description ?? Optional<string>.Unspecified
78+
description = attachment.Description ?? Optional<string>.Unspecified,
79+
duration_secs = attachment.DurationSeconds ?? Optional<double>.Unspecified,
80+
waveform = attachment.Waveform is null
81+
? Optional<string>.Unspecified
82+
: Convert.ToBase64String(attachment.Waveform)
7583
});
7684
}
7785

src/Discord.Net.Rest/API/Rest/UploadInteractionFileParams.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public IReadOnlyDictionary<string, object> ToDictionary()
4444
{
4545
var d = new Dictionary<string, object>();
4646

47+
if (Files.Any(x => x.Waveform is not null && x.DurationSeconds is not null))
48+
Flags = Flags.GetValueOrDefault(MessageFlags.None) | MessageFlags.VoiceMessage;
4749

4850
var payload = new Dictionary<string, object>();
4951
payload["type"] = Type;
@@ -79,7 +81,11 @@ public IReadOnlyDictionary<string, object> ToDictionary()
7981
{
8082
id = (ulong)n,
8183
filename = filename,
82-
description = attachment.Description ?? Optional<string>.Unspecified
84+
description = attachment.Description ?? Optional<string>.Unspecified,
85+
duration_secs = attachment.DurationSeconds ?? Optional<double>.Unspecified,
86+
waveform = attachment.Waveform is null
87+
? Optional<string>.Unspecified
88+
: Convert.ToBase64String(attachment.Waveform)
8389
});
8490
}
8591

src/Discord.Net.Rest/API/Rest/UploadWebhookFileParams.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using Discord.Net.Converters;
22
using Discord.Net.Rest;
33
using Newtonsoft.Json;
4+
using System;
45
using System.Collections.Generic;
56
using System.IO;
7+
using System.Linq;
68
using System.Text;
79

810
namespace Discord.API.Rest
@@ -35,6 +37,9 @@ public IReadOnlyDictionary<string, object> ToDictionary()
3537
{
3638
var d = new Dictionary<string, object>();
3739

40+
if (Files.Any(x => x.Waveform is not null && x.DurationSeconds is not null))
41+
Flags = Flags.GetValueOrDefault(MessageFlags.None) | MessageFlags.VoiceMessage;
42+
3843
var payload = new Dictionary<string, object>();
3944
if (Content.IsSpecified)
4045
payload["content"] = Content.Value;
@@ -76,7 +81,11 @@ public IReadOnlyDictionary<string, object> ToDictionary()
7681
{
7782
id = (ulong)n,
7883
filename = filename,
79-
description = attachment.Description ?? Optional<string>.Unspecified
84+
description = attachment.Description ?? Optional<string>.Unspecified,
85+
duration_secs = attachment.DurationSeconds ?? Optional<double>.Unspecified,
86+
waveform = attachment.Waveform is null
87+
? Optional<string>.Unspecified
88+
: Convert.ToBase64String(attachment.Waveform)
8089
});
8190
}
8291

src/Discord.Net.Rest/Entities/Messages/Attachment.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Discord.Rest;
22
using System;
3+
using System.Buffers.Text;
34
using System.Collections.Generic;
45
using System.Collections.Immutable;
56
using System.Diagnostics;
@@ -35,6 +36,8 @@ public class Attachment : IAttachment
3536
/// <inheritdoc />
3637
public string Waveform { get; }
3738
/// <inheritdoc />
39+
public byte[] WaveformBytes { get; }
40+
/// <inheritdoc />
3841
public double? Duration { get; }
3942

4043
/// <inheritdoc cref="IAttachment.ClipParticipants" />
@@ -69,6 +72,8 @@ internal Attachment(ulong id, string filename, string url, string proxyUrl, int
6972
Title = title;
7073
ClipParticipants = clipParticipants;
7174
ClipCreatedAt = clipCreatedAt;
75+
if (waveform is not null)
76+
WaveformBytes = Convert.FromBase64String(waveform);
7277
}
7378

7479
internal static Attachment Create(Model model, BaseDiscordClient discord)

0 commit comments

Comments
 (0)