I have a command below that should send a random word into the context channel.
[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));
}
}
However, I receive and error saying that I have exceeded Discord's 2000 character message limit on this line.
var allLines = File.ReadAllLines(Resources.english_uk_wordlist);
This is strange to me because reading these lines should have nothing to do with Discord.Net or Discord's API. It is important to note that when I put this text file outside of my Resources.resx
, it works fine.
I have also tried using StreamReader
which results in the same problem. If it helps, the value of allLines.Length
is 854236.
Thanks.
The issue is that Resources.english_wordlist_uk
is a string, not a path.
Code A
var location = Resources.english_wordlist_uk;
var readText = File.ReadAllLines(location);
The above code reads Resources.english_wordlist_uk
and uses its contents as a path. It does not recognize the file itself as a path.
Code B
var location = Resources.english_wordlist_uk;
using (StringReader sr = new StringReader(location))
{
var readText = sr.ReadToEndAsync();
}
This code reads the content of Resources.english_wordlist_uk
as if it were a string, resulting in readText
being the entire contents of the file.
Explanation
The reason I received a 2000 character limit error is because of Code A. Since the content of Resources.english_uk_wordlist
is not a path, the error message was something along the lines of: "Specified path (content of the file) does not exist."
Since my command handler sends the error message in the channel, and the error message is hundreds of thousands of characters, Discord could not send the error message. Code B is a clean solution to my problem.