mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-05-02 04:22:04 -04:00
Added an admin command to set a user's right
This commit is contained in:
35
KfChatDotNetBot/Commands/AdminCommands.cs
Normal file
35
KfChatDotNetBot/Commands/AdminCommands.cs
Normal file
@@ -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<Regex> Patterns => [
|
||||
new Regex(@"^admin role set (?<user>\d+) (?<right>\d+)$"),
|
||||
new Regex(@"^admin right set (?<user>\d+) (?<right>\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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user