'System.ArgumentException: boundary' 使用 Discord.net API 上传文件到 Discord 时

'System.ArgumentException: boundary' When uploading files to Discord using Discord.net API

我正在尝试使用 .net Discord API 从托管在 raspberry pi 3 运行 raspbian 精简版。

但是,在这样做的过程中,我得到了 System.ArgumentException: boundary:

Discord.Commands.CommandException: Error occurred executing "$phtake" for yangman#3640 in YANGNET/moderator-channel. ---> System.ArgumentException: boundary
  at System.Net.Http.MultipartContent..ctor (System.String subtype, System.String boundary) [0x00062] in <17d6c7facb7941c2ae864b4fe324e825>:0
  at System.Net.Http.MultipartFormDataContent..ctor (System.String boundary) [0x00000] in <17d6c7facb7941c2ae864b4fe324e825>:0
  at Discord.Net.Rest.DefaultRestClient.SendAsync (System.String method, System.String endpoint, System.Collections.Generic.IReadOnlyDictionary`2[TKey,TValue] multipartParams, System.Threading.CancellationToken cancelToken, System.Boolean headerOnly, System.String reason) [0x0008d] in <384170f933f2420883fb2b21834d3dc7>:0
  at Discord.Net.Queue.MultipartRestRequest.SendAsync () [0x000a4] in <384170f933f2420883fb2b21834d3dc7>:0
  at Discord.Net.Queue.RequestBucket.SendAsync (Discord.Net.Queue.RestRequest request) [0x001b9] in <384170f933f2420883fb2b21834d3dc7>:0
  at Discord.Net.Queue.RequestQueue.SendAsync (Discord.Net.Queue.RestRequest request) [0x00108] in <384170f933f2420883fb2b21834d3dc7>:0
  at Discord.API.DiscordRestApiClient.SendInternalAsync (System.String method, System.String endpoint, Discord.Net.Queue.RestRequest request) [0x000e0] in <384170f933f2420883fb2b21834d3dc7>:0
  at Discord.API.DiscordRestApiClient.SendMultipartAsync[TResponse] (System.String method, System.String endpoint, System.Collections.Generic.IReadOnlyDictionary`2[TKey,TValue] multipartArgs, System.String bucketId, Discord.Net.Queue.ClientBucketType clientBucket, Discord.RequestOptions options) [0x000c9] in <384170f933f2420883fb2b21834d3dc7>:0
  at Discord.API.DiscordRestApiClient.UploadFileAsync (System.UInt64 channelId, Discord.API.Rest.UploadFileParams args, Discord.RequestOptions options) [0x001df] in <384170f933f2420883fb2b21834d3dc7>:0
  at Discord.Rest.ChannelHelper.SendFileAsync (Discord.IMessageChannel channel, Discord.Rest.BaseDiscordClient client, System.IO.Stream stream, System.String filename, System.String text, System.Boolean isTTS, Discord.Embed embed, Discord.RequestOptions options, System.Boolean isSpoiler) [0x000f7] in <384170f933f2420883fb2b21834d3dc7>:0
  at Discord.Rest.ChannelHelper.SendFileAsync (Discord.IMessageChannel channel, Discord.Rest.BaseDiscordClient client, System.String filePath, System.String text, System.Boolean isTTS, Discord.Embed embed, Discord.RequestOptions options, System.Boolean isSpoiler) [0x000b6] in <384170f933f2420883fb2b21834d3dc7>:0
  at Discord.WebSocket.SocketTextChannel.Discord.IMessageChannel.SendFileAsync (System.String filePath, System.String text, System.Boolean isTTS, Discord.Embed embed, Discord.RequestOptions options, System.Boolean isSpoiler) [0x00090] in <3331075733c74092b9fa467c888c9e7b>:0
  at DISCORDBOT.Modules.ExampleCommands.phTake () [0x0018c] in <f7377afb943742a18fbcc33b92191af1>:0
  at Discord.Commands.ModuleClassBuilder+<>c__DisplayClass6_0.<BuildCommand>g__ExecuteCallback|0 (Discord.Commands.ICommandContext context, System.Object[] args, System.IServiceProvider services, Discord.Commands.CommandInfo cmd) [0x00151] in <daa3127b39d34db2bae7cf69a3b7da92>:0
  at Discord.Commands.CommandInfo.ExecuteInternalAsync (Discord.Commands.ICommandContext context, System.Object[] args, System.IServiceProvider services) [0x001ae] in <daa3127b39d34db2bae7cf69a3b7da92>:0
   --- End of inner exception stack trace ---

我的 windows 机器上没有发生这样的错误 - 它成功发送了图像 - 所以我猜这可能是由于某种过时的包造成的,但是,我认为那不是案例.

那么为什么会出现这个异常呢?

这是我发送图片的简单方法:

        [Command("$phtake")]
        [RequireContext(ContextType.Guild)]
        public async Task phTake()
        {

            string path = $@"{Directory.GetParent(Directory.GetCurrentDirectory())}/bin/image.jpg";
            Console.WriteLine(path);

            await Context.Channel.SendFileAsync(path, "your image: ");

        }

我在 raspberry pi 上尝试了不同的图像,以检查图像是否引发了这个错误,唉,它没有。

我还注意到使用 discord.net EmbedBuilder() 可以嵌入图像,但是,它在嵌入本地文件时也会引发错误。可以将此文件托管到 Web 服务并以某种方式将图像 URL 嵌入到 Embed Builder 中,但这需要脚本上传文件并检索它,我是新手。
编辑:使用 SendFileAsync() 函数似乎会引发此错误。

如果有人对解决此问题的可能方法或可能的解决方法有任何想法,我们将不胜感激。

几天后,我制定了一个解决方法:

我决定使用 EmbedBuilder()

嵌入图像,而不是使用 SendFileAsync() 方法

因为 SendFileAsync() 的实例不起作用,我使用 Imgur API 发送和检索我的图像作为 URL。通过附加此 URL,我能够发送相同的图像。