Browse Source

Fixed file being disposed on upload (#1995)

tags/3.1.0
Quin Lynch GitHub 3 years ago
parent
commit
ad20e03a98
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 19 deletions
  1. +3
    -3
      src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
  2. +6
    -8
      src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs
  3. +6
    -8
      src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs

+ 3
- 3
src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs View File

@@ -344,11 +344,11 @@ namespace Discord.Rest
} }


/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception> /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
public static Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
Stream stream, string filename, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options, bool isSpoiler, Embed[] embeds) Stream stream, string filename, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options, bool isSpoiler, Embed[] embeds)
{ {
using var file = new FileAttachment(stream, filename, isSpoiler: isSpoiler);
return SendFileAsync(channel, client, file, text, isTTS, embed, allowedMentions, messageReference, components, stickers, options, embeds);
using (var file = new FileAttachment(stream, filename, isSpoiler: isSpoiler))
return await SendFileAsync(channel, client, file, text, isTTS, embed, allowedMentions, messageReference, components, stickers, options, embeds).ConfigureAwait(false);
} }


/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception> /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>


+ 6
- 8
src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs View File

@@ -183,7 +183,7 @@ namespace Discord.Rest
} }


/// <inheritdoc/> /// <inheritdoc/>
public override Task<RestFollowupMessage> FollowupWithFileAsync(
public override async Task<RestFollowupMessage> FollowupWithFileAsync(
Stream fileStream, Stream fileStream,
string fileName, string fileName,
string text = null, string text = null,
@@ -198,18 +198,15 @@ namespace Discord.Rest
if (!IsValidToken) if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid"); throw new InvalidOperationException("Interaction token is no longer valid");


embeds ??= Array.Empty<Embed>();
if (embed != null)
embeds = new[] { embed }.Concat(embeds).ToArray();

Preconditions.NotNull(fileStream, nameof(fileStream), "File Stream must have data"); Preconditions.NotNull(fileStream, nameof(fileStream), "File Stream must have data");
Preconditions.NotNullOrEmpty(fileName, nameof(fileName), "File Name must not be empty or null"); Preconditions.NotNullOrEmpty(fileName, nameof(fileName), "File Name must not be empty or null");


return FollowupWithFileAsync(new FileAttachment(fileStream, fileName), text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options);
using(var file = new FileAttachment(fileStream, fileName))
return await FollowupWithFileAsync(file, text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options).ConfigureAwait(false);
} }


/// <inheritdoc/> /// <inheritdoc/>
public override Task<RestFollowupMessage> FollowupWithFileAsync(
public override async Task<RestFollowupMessage> FollowupWithFileAsync(
string filePath, string filePath,
string fileName = null, string fileName = null,
string text = null, string text = null,
@@ -226,7 +223,8 @@ namespace Discord.Rest
fileName ??= Path.GetFileName(filePath); fileName ??= Path.GetFileName(filePath);
Preconditions.NotNullOrEmpty(fileName, nameof(fileName), "File Name must not be empty or null"); Preconditions.NotNullOrEmpty(fileName, nameof(fileName), "File Name must not be empty or null");


return FollowupWithFileAsync(new FileAttachment(File.OpenRead(filePath), fileName), text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options);
using (var file = new FileAttachment(File.OpenRead(filePath), fileName))
return await FollowupWithFileAsync(file, text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options).ConfigureAwait(false);
} }


/// <inheritdoc/> /// <inheritdoc/>


+ 6
- 8
src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs View File

@@ -275,7 +275,7 @@ namespace Discord.Rest
} }


/// <inheritdoc/> /// <inheritdoc/>
public override Task<RestFollowupMessage> FollowupWithFileAsync(
public override async Task<RestFollowupMessage> FollowupWithFileAsync(
Stream fileStream, Stream fileStream,
string fileName, string fileName,
string text = null, string text = null,
@@ -290,18 +290,15 @@ namespace Discord.Rest
if (!IsValidToken) if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid"); throw new InvalidOperationException("Interaction token is no longer valid");


embeds ??= Array.Empty<Embed>();
if (embed != null)
embeds = new[] { embed }.Concat(embeds).ToArray();

Preconditions.NotNull(fileStream, nameof(fileStream), "File Stream must have data"); Preconditions.NotNull(fileStream, nameof(fileStream), "File Stream must have data");
Preconditions.NotNullOrEmpty(fileName, nameof(fileName), "File Name must not be empty or null"); Preconditions.NotNullOrEmpty(fileName, nameof(fileName), "File Name must not be empty or null");


return FollowupWithFileAsync(new FileAttachment(fileStream, fileName), text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options);
using(var file = new FileAttachment(fileStream, fileName))
return await FollowupWithFileAsync(file, text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options).ConfigureAwait(false);
} }


/// <inheritdoc/> /// <inheritdoc/>
public override Task<RestFollowupMessage> FollowupWithFileAsync(
public override async Task<RestFollowupMessage> FollowupWithFileAsync(
string filePath, string filePath,
string fileName = null, string fileName = null,
string text = null, string text = null,
@@ -318,7 +315,8 @@ namespace Discord.Rest
fileName ??= Path.GetFileName(filePath); fileName ??= Path.GetFileName(filePath);
Preconditions.NotNullOrEmpty(fileName, nameof(fileName), "File Name must not be empty or null"); Preconditions.NotNullOrEmpty(fileName, nameof(fileName), "File Name must not be empty or null");


return FollowupWithFileAsync(new FileAttachment(File.OpenRead(filePath), fileName), text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options);
using(var file = new FileAttachment(File.OpenRead(filePath), fileName))
return await FollowupWithFileAsync(file, text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options).ConfigureAwait(false);
} }


/// <inheritdoc/> /// <inheritdoc/>


Loading…
Cancel
Save