mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-05-03 04:52:04 -04:00
Also refactored the way tasks are handled so instead of adding to an array and checking in on them next time someone sends a message, it instead delegates it to a very basic async handler that'll await the command, report errors and kill the task if it takes too long.
29 lines
1.2 KiB
C#
29 lines
1.2 KiB
C#
using System.Text.RegularExpressions;
|
|
using KfChatDotNetBot.Models.DbModels;
|
|
using KfChatDotNetWsClient.Models.Events;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace KfChatDotNetBot.Commands;
|
|
|
|
public class WhoisCommand : ICommand
|
|
{
|
|
public List<Regex> Patterns => [
|
|
new Regex("^whois (?<user>.+)")
|
|
];
|
|
|
|
public string? HelpText => "Lookup user IDs by username";
|
|
public UserRight RequiredRight => UserRight.Guest;
|
|
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 query = arguments["user"].Value.TrimStart('@').TrimEnd(',').TrimEnd();
|
|
var queryUser = await db.Users.FirstOrDefaultAsync(u => u.KfUsername == query, cancellationToken: ctx);
|
|
if (queryUser == null)
|
|
{
|
|
botInstance.SendChatMessage($"Requested user '{query}' does not exist. (Note this is case-sensitive)", true);
|
|
return;
|
|
}
|
|
botInstance.SendChatMessage($"@{message.Author.Username}, {queryUser.KfUsername}'s ID is {queryUser.KfId}", true);
|
|
}
|
|
} |