I am writing a bot in C # for chat, and I want to determine if the message contains too many capital letters. A message contains too many capital letters if its total number of capital letters exceeds one-thirdits total message length and if its total length is greater than 13. This is to prevent tagging from smaller posts.
Right now, I look through each character and see that it is uppercase. This is normal for a reasonable message. However, if you receive a spammer or troll, they will not always send messages of reasonable duration. The maximum character limit 2000, and I can’t change it. With my method, it starts to stop around characters 500+. This gives the spammer enough time to insert a message and send it again, effectively chatting while the bot is struggling to keep up.
The code I have is:
bool isMostlyUpper = (message.Count(c => char.IsUpper(c)) >= message.Length * 0.3f) && message.Length > 13;
I cannot compare the message with string.ToUpper(), because I still want to determine if the message is mostly uppercase, not uppercase.
Is there a way to do this without skewing over each char? Or a way to get to the result faster? I can add checks to see if there is a message > 500, but sometimes there are long messages 500+that you can go through.
Does anyone have smart solutions? Thank.
source
share