From dffbecf1eca0cab4ec18ff40b9087a4c5dd2cf75 Mon Sep 17 00:00:00 2001 From: barelyprofessional <150058423+barelyprofessional@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:25:36 +0800 Subject: [PATCH] Added an admin command to set a user's right --- KfChatDotNetBot/Commands/AdminCommands.cs | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 KfChatDotNetBot/Commands/AdminCommands.cs diff --git a/KfChatDotNetBot/Commands/AdminCommands.cs b/KfChatDotNetBot/Commands/AdminCommands.cs new file mode 100644 index 0000000..d7df7e9 --- /dev/null +++ b/KfChatDotNetBot/Commands/AdminCommands.cs @@ -0,0 +1,35 @@ +using System.Text.RegularExpressions; +using Humanizer; +using KfChatDotNetBot.Models.DbModels; +using KfChatDotNetWsClient.Models.Events; +using Microsoft.EntityFrameworkCore; + +namespace KfChatDotNetBot.Commands; + +public class SetRightCommand : ICommand +{ + public List Patterns => [ + new Regex(@"^admin role set (?\d+) (?\d+)$"), + new Regex(@"^admin right set (?\d+) (?\d+)$") + ]; + + public string? HelpText => "Set a user's right"; + public UserRight RequiredRight => UserRight.Admin; + public TimeSpan Timeout => TimeSpan.FromSeconds(10); + public async Task RunCommand(ChatBot botInstance, MessageModel message, UserDbModel user, GroupCollection arguments, CancellationToken ctx) + { + await using var db = new ApplicationDbContext(); + var targetUserId = Convert.ToInt32(arguments["user"].Value); + var right = (UserRight)Convert.ToInt32(arguments["right"].Value); + var targetUser = await db.Users.FirstOrDefaultAsync(u => u.KfId == targetUserId, cancellationToken: ctx); + if (targetUser == null) + { + botInstance.SendChatMessage($"User '{targetUserId}' does not exist", true); + return; + } + + targetUser.UserRight = right; + await db.SaveChangesAsync(ctx); + botInstance.SendChatMessage($"@{message.Author.Username}, {targetUser.KfUsername}'s right set to {right.Humanize()}", true); + } +} \ No newline at end of file