1
+ using System ;
2
+ using System . IO ;
3
+
1
4
using DisCatSharp . Entities ;
2
5
3
6
namespace DisCatSharp . Experimental . Entities ;
@@ -12,15 +15,139 @@ public static class DiscordMessageBuilderMethodHooks
12
15
/// </summary>
13
16
/// <param name="builder">The <see cref="DiscordMessageBuilder" /> to add the attachment to.</param>
14
17
/// <param name="gcpAttachment">The attachment to add.</param>
18
+ /// <param name="isVoice">Whether this is a voice message attachment.</param>
19
+ /// <param name="originalStream">The voice message's stream, required if <paramref name="isVoice"/> is <see langword="true"/>.</param>
15
20
/// <returns>The chained <see cref="DiscordMessageBuilder" />.</returns>
16
- public static DiscordMessageBuilder AddGcpAttachment ( this DiscordMessageBuilder builder , GcpAttachmentUploadInformation gcpAttachment )
21
+ public static DiscordMessageBuilder AddGcpAttachment ( this DiscordMessageBuilder builder , GcpAttachmentUploadInformation gcpAttachment , bool isVoice = false , Stream ? originalStream = null )
22
+ {
23
+ if ( ! isVoice )
24
+ builder . AttachmentsInternal . Add ( new ( )
25
+ {
26
+ Filename = gcpAttachment . Filename ,
27
+ UploadedFilename = gcpAttachment . UploadFilename ,
28
+ Description = gcpAttachment . Description
29
+ } ) ;
30
+ else
31
+ {
32
+ ArgumentNullException . ThrowIfNull ( originalStream , nameof ( originalStream ) ) ;
33
+ var ( _, durationSeconds , waveform ) = originalStream . GetDurationAndWaveformBytes ( ) ;
34
+ Console . WriteLine ( $ "Waveform length: { waveform . Length } bytes") ;
35
+ builder . AttachmentsInternal . Add ( new ( )
36
+ {
37
+ Filename = gcpAttachment . Filename ,
38
+ UploadedFilename = gcpAttachment . UploadFilename ,
39
+ Description = gcpAttachment . Description ,
40
+ DurationSecs = durationSeconds ,
41
+ WaveForm = waveform
42
+ } ) ;
43
+ builder . AsVoiceMessage ( ) ;
44
+ }
45
+
46
+ return builder ;
47
+ }
48
+
49
+ /// <summary>
50
+ /// Adds a <see cref="GcpAttachment" /> to the <see cref="DiscordInteractionResponseBuilder" />.
51
+ /// </summary>
52
+ /// <param name="builder">The <see cref="DiscordInteractionResponseBuilder" /> to add the attachment to.</param>
53
+ /// <param name="gcpAttachment">The attachment to add.</param>
54
+ /// <returns>The chained <see cref="DiscordInteractionResponseBuilder" />.</returns>
55
+ public static DiscordInteractionResponseBuilder AddGcpAttachment ( this DiscordInteractionResponseBuilder builder , GcpAttachmentUploadInformation gcpAttachment )
56
+ {
57
+ var isVoice = false ;
58
+ Stream ? originalStream = null ;
59
+ if ( ! isVoice )
60
+ builder . AttachmentsInternal . Add ( new ( )
61
+ {
62
+ Filename = gcpAttachment . Filename ,
63
+ UploadedFilename = gcpAttachment . UploadFilename ,
64
+ Description = gcpAttachment . Description
65
+ } ) ;
66
+ else
67
+ {
68
+ ArgumentNullException . ThrowIfNull ( originalStream , nameof ( originalStream ) ) ;
69
+ var ( _, durationSeconds , waveform ) = originalStream . GetDurationAndWaveformBytes ( ) ;
70
+ Console . WriteLine ( $ "Waveform length: { waveform . Length } bytes") ;
71
+ builder . AttachmentsInternal . Add ( new ( )
72
+ {
73
+ Filename = gcpAttachment . Filename ,
74
+ UploadedFilename = gcpAttachment . UploadFilename ,
75
+ Description = gcpAttachment . Description ,
76
+ DurationSecs = durationSeconds ,
77
+ WaveForm = waveform
78
+ } ) ;
79
+ builder . AsVoiceMessage ( ) ;
80
+ }
81
+
82
+ return builder ;
83
+ }
84
+
85
+ /// <summary>
86
+ /// Adds a <see cref="GcpAttachment" /> to the <see cref="DiscordWebhookBuilder" />.
87
+ /// </summary>
88
+ /// <param name="builder">The <see cref="DiscordWebhookBuilder" /> to add the attachment to.</param>
89
+ /// <param name="gcpAttachment">The attachment to add.</param>
90
+ /// <returns>The chained <see cref="DiscordWebhookBuilder" />.</returns>
91
+ public static DiscordWebhookBuilder AddGcpAttachment ( this DiscordWebhookBuilder builder , GcpAttachmentUploadInformation gcpAttachment )
92
+ {
93
+ var isVoice = false ;
94
+ Stream ? originalStream = null ;
95
+ if ( ! isVoice )
96
+ builder . AttachmentsInternal . Add ( new ( )
97
+ {
98
+ Filename = gcpAttachment . Filename ,
99
+ UploadedFilename = gcpAttachment . UploadFilename ,
100
+ Description = gcpAttachment . Description
101
+ } ) ;
102
+ else
103
+ {
104
+ ArgumentNullException . ThrowIfNull ( originalStream , nameof ( originalStream ) ) ;
105
+ var ( _, durationSeconds , waveform ) = originalStream . GetDurationAndWaveformBytes ( ) ;
106
+ builder . AttachmentsInternal . Add ( new ( )
107
+ {
108
+ Filename = gcpAttachment . Filename ,
109
+ UploadedFilename = gcpAttachment . UploadFilename ,
110
+ Description = gcpAttachment . Description ,
111
+ DurationSecs = durationSeconds ,
112
+ WaveForm = waveform
113
+ } ) ;
114
+ builder . AsVoiceMessage ( ) ;
115
+ }
116
+
117
+ return builder ;
118
+ }
119
+
120
+ /// <summary>
121
+ /// Adds a <see cref="GcpAttachment" /> to the <see cref="DiscordFollowupMessageBuilder" />.
122
+ /// </summary>
123
+ /// <param name="builder">The <see cref="DiscordFollowupMessageBuilder" /> to add the attachment to.</param>
124
+ /// <param name="gcpAttachment">The attachment to add.</param>
125
+ /// <returns>The chained <see cref="DiscordFollowupMessageBuilder" />.</returns>
126
+ public static DiscordFollowupMessageBuilder AddGcpAttachment ( this DiscordFollowupMessageBuilder builder , GcpAttachmentUploadInformation gcpAttachment )
17
127
{
18
- builder . AttachmentsInternal . Add ( new ( )
128
+ var isVoice = false ;
129
+ Stream ? originalStream = null ;
130
+ if ( ! isVoice )
131
+ builder . AttachmentsInternal . Add ( new ( )
132
+ {
133
+ Filename = gcpAttachment . Filename ,
134
+ UploadedFilename = gcpAttachment . UploadFilename ,
135
+ Description = gcpAttachment . Description
136
+ } ) ;
137
+ else
19
138
{
20
- Filename = gcpAttachment . Filename ,
21
- UploadedFilename = gcpAttachment . UploadFilename ,
22
- Description = gcpAttachment . Description
23
- } ) ;
139
+ ArgumentNullException . ThrowIfNull ( originalStream , nameof ( originalStream ) ) ;
140
+ var ( _, durationSeconds , waveform ) = originalStream . GetDurationAndWaveformBytes ( ) ;
141
+ builder . AttachmentsInternal . Add ( new ( )
142
+ {
143
+ Filename = gcpAttachment . Filename ,
144
+ UploadedFilename = gcpAttachment . UploadFilename ,
145
+ Description = gcpAttachment . Description ,
146
+ DurationSecs = durationSeconds ,
147
+ WaveForm = waveform
148
+ } ) ;
149
+ builder . AsVoiceMessage ( ) ;
150
+ }
24
151
25
152
return builder ;
26
153
}
0 commit comments