sharp-chat/SharpChat/ClientCommands/AFKClientCommand.cs

35 lines
1.1 KiB
C#
Raw Normal View History

2025-04-26 22:28:41 +00:00
using System.Globalization;
using System.Text;
2022-08-30 17:00:58 +02:00
namespace SharpChat.ClientCommands;
2022-08-30 17:00:58 +02:00
public class AFKClientCommand : ClientCommand {
private const string DEFAULT = "AFK";
public const int MAX_GRAPHEMES = 5;
public const int MAX_BYTES = MAX_GRAPHEMES * 10;
2022-08-30 17:00:58 +02:00
public bool IsMatch(ClientCommandContext ctx) {
return ctx.NameEquals("afk");
}
public async Task Dispatch(ClientCommandContext ctx) {
string? statusText = ctx.Args.FirstOrDefault();
if(string.IsNullOrWhiteSpace(statusText))
statusText = DEFAULT;
else {
statusText = statusText.Trim();
2022-08-30 17:00:58 +02:00
StringInfo sti = new(statusText);
if(Encoding.UTF8.GetByteCount(statusText) > MAX_BYTES
|| sti.LengthInTextElements > MAX_GRAPHEMES)
statusText = sti.SubstringByTextElements(0, Math.Min(sti.LengthInTextElements, MAX_GRAPHEMES)).Trim();
2022-08-30 17:00:58 +02:00
}
await ctx.Chat.UpdateUser(
ctx.User,
status: UserStatus.Away,
statusText: statusText
);
2022-08-30 17:00:58 +02:00
}
}