未发送消息时收到 Discord.Net 2000 个字符限制警告

Receiving a Discord.Net 2000 character limit warning when I am not sending a message

我在下面有一个命令,它应该将一个随机词发送到上下文通道中。

[Command("word"), Summary("Chooses a random English word.")]
public async Task RandomWord(string culture = "uk")
{
    if (culture == "uk")
    {
        // Reads all the lines of the word list.  
        var allLines = File.ReadAllLines(Resources.english_uk_wordlist);

        // Chooses a random line and its word.
        var word = allLines[_random.Next(0, allLines.Length - 1)];

        // Sends random chosen word to channel.
        await ReplyAsync(word.First().ToString().ToUpper() + word.Substring(1));
    }
}

但是,我收到错误消息说我已经超过了 Discord 在此行的 2000 个字符的消息限制。

var allLines = File.ReadAllLines(Resources.english_uk_wordlist);

这对我来说很奇怪,因为阅读这些行应该与 Discord.Net 或 Discord 的 API 无关。重要的是要注意,当我将此文本文件放在我的 Resources.resx 之外时,它工作正常。

我也尝试过使用 StreamReader 导致同样的问题。如果有帮助,allLines.Length的值为854236。

谢谢。

如果你能说出allLines.Length的值,那么ReadAllLines一定成功了。我认为 await 混淆了调试器。

await 行设置断点。在调试模式下编译和 运行。那么 Discord 将不会受到影响。它还将允许您查看 word 变量中的内容。

请注意,Random.Next(min, max) 的上限是唯一的。所以它可能应该是_random.Next(0, allLines.Length),除非最后一行是空的。

问题是 Resources.english_wordlist_uk 是一个字符串,而不是路径。

代码A

var location = Resources.english_wordlist_uk;
var readText = File.ReadAllLines(location);

以上代码读取Resources.english_wordlist_uk并将其内容用作路径。它不将文件本身识别为路径。

代码B

var location = Resources.english_wordlist_uk;
using (StringReader sr = new StringReader(location))
{
    var readText = sr.ReadToEndAsync();
}

此代码读取 Resources.english_wordlist_uk 的内容,就好像它是一个字符串,导致 readText 成为文件的全部内容。

说明

我收到 2000 个字符限制错误的原因是因为 代码 A。由于 Resources.english_uk_wordlist 的内容不是路径,因此错误消息类似于:"Specified path (content of the file) does not exist."

由于我的command handler在频道中发送了错误信息,而且错误信息有几十万个字符,所以Discord无法发送错误信息。 代码 B 是我的问题的干净解决方案。