mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-04-30 03:22:04 -04:00
Allow !legitcheck to use usernames (#96)
* Keep track of users in chat * Allow usernames for legitcheck
This commit is contained in:
@@ -36,7 +36,8 @@ public class ChatBot
|
||||
private DateTime _lastReconnectAttempt = DateTime.UtcNow;
|
||||
private List<ScheduledAutoDeleteModel> _scheduledDeletions = [];
|
||||
private Task _scheduledAutoDeleteTask;
|
||||
|
||||
private List<UserModel> _currentUsersInChat = [];
|
||||
|
||||
public ChatBot()
|
||||
{
|
||||
_logger.Info("Bot starting!");
|
||||
@@ -558,12 +559,13 @@ public class ChatBot
|
||||
}
|
||||
|
||||
public class SentMessageNotFoundException : Exception;
|
||||
|
||||
|
||||
private void OnUsersJoined(object sender, List<UserModel> users, UsersJsonModel jsonPayload)
|
||||
{
|
||||
var settings = SettingsProvider.GetMultipleValuesAsync([BuiltIn.Keys.GambaSeshUserId, BuiltIn.Keys.GambaSeshDetectEnabled, BuiltIn.Keys.BotKeesSeen])
|
||||
.Result;
|
||||
_logger.Debug($"Received {users.Count} user join events");
|
||||
_currentUsersInChat.AddRange(users);
|
||||
using var db = new ApplicationDbContext();
|
||||
foreach (var user in users)
|
||||
{
|
||||
@@ -606,6 +608,7 @@ public class ChatBot
|
||||
|
||||
private void OnUsersParted(object sender, List<int> userIds)
|
||||
{
|
||||
_currentUsersInChat.RemoveAll(u => userIds.Contains(u.Id));
|
||||
var settings = SettingsProvider.GetMultipleValuesAsync([BuiltIn.Keys.GambaSeshUserId, BuiltIn.Keys.GambaSeshDetectEnabled])
|
||||
.Result;
|
||||
if (userIds.Contains(settings[BuiltIn.Keys.GambaSeshUserId].ToType<int>()) && settings[BuiltIn.Keys.GambaSeshDetectEnabled].ToBoolean())
|
||||
@@ -653,6 +656,7 @@ public class ChatBot
|
||||
_logger.Error($"Sneedchat disconnected due to {disconnectionInfo.Type}");
|
||||
_logger.Error($"Close Status => {disconnectionInfo.CloseStatus}; Close Status Description => {disconnectionInfo.CloseStatusDescription}");
|
||||
_logger.Error(disconnectionInfo.Exception);
|
||||
_currentUsersInChat.Clear();
|
||||
if (disconnectionInfo.Exception != null && disconnectionInfo.Exception.Message.Contains("status code '203'"))
|
||||
{
|
||||
_logger.Info("Chat 203'd, getting a new token");
|
||||
@@ -673,6 +677,11 @@ public class ChatBot
|
||||
KfClient.JoinRoom(roomId);
|
||||
}
|
||||
|
||||
public UserModel? FindUserByName(string username)
|
||||
{
|
||||
return _currentUsersInChat.FirstOrDefault(u => u.Username.Equals(username, StringComparison.CurrentCulture));
|
||||
}
|
||||
|
||||
public enum LengthLimitBehavior
|
||||
{
|
||||
// Append …
|
||||
|
||||
@@ -23,6 +23,8 @@ public class LegitCheckCommand : ICommand
|
||||
[
|
||||
new Regex(@"^legitcheck (?<user_id>\d+)$", RegexOptions.IgnoreCase),
|
||||
new Regex(@"^legitcheck (?<user_id>\d+) all$", RegexOptions.IgnoreCase),
|
||||
new Regex(@"^legitcheck @(?<username>.+) all$", RegexOptions.IgnoreCase),
|
||||
new Regex(@"^legitcheck @(?<username>.+)$", RegexOptions.IgnoreCase),
|
||||
new Regex(@"^legitcheck$", RegexOptions.IgnoreCase),
|
||||
new Regex(@"^legitcheck all$", RegexOptions.IgnoreCase),
|
||||
];
|
||||
@@ -46,15 +48,34 @@ public class LegitCheckCommand : ICommand
|
||||
{
|
||||
await using var db = new ApplicationDbContext();
|
||||
|
||||
var targetUserId = arguments["user_id"].Success
|
||||
? int.Parse(arguments["user_id"].Value)
|
||||
: user.KfId;
|
||||
var targetUser = await db.Users.FirstOrDefaultAsync(u => u.KfId == targetUserId, ctx);
|
||||
var isAll = message.MessageRaw.EndsWith(" all");
|
||||
|
||||
UserDbModel? targetUser;
|
||||
if (arguments["username"].Success)
|
||||
{
|
||||
var chatUser = botInstance.FindUserByName(arguments["username"].Value);
|
||||
if (chatUser == null)
|
||||
{
|
||||
await botInstance.SendChatMessageAsync(
|
||||
$"{user.FormatUsername()}, couldn't find that user in chat. They must be present in chat to look up by username.",
|
||||
true);
|
||||
return;
|
||||
}
|
||||
targetUser = await db.Users.FirstOrDefaultAsync(u => u.KfId == chatUser.Id, ctx);
|
||||
}
|
||||
else if (arguments["user_id"].Success)
|
||||
{
|
||||
var targetUserId = int.Parse(arguments["user_id"].Value);
|
||||
targetUser = await db.Users.FirstOrDefaultAsync(u => u.KfId == targetUserId, ctx);
|
||||
}
|
||||
else
|
||||
{
|
||||
targetUser = await db.Users.FirstOrDefaultAsync(u => u.KfId == user.KfId, ctx);
|
||||
}
|
||||
|
||||
if (targetUser == null)
|
||||
{
|
||||
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, the user ID you gave doesn't exist.",
|
||||
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, that user doesn't exist.",
|
||||
true);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user