public override void ExecuteCommand(EvtChatCommandArgs args) { List <string> arguments = args.Command.ArgumentsAsList; int argCount = arguments.Count; //Ignore with not enough arguments if (argCount != 4) { QueueMessage(UsageMessage); return; } string username = arguments[0]; User restrictedUser = DataHelper.GetUser(username); //Check for the user if (restrictedUser == null) { QueueMessage("A user with this name does not exist in the database!"); return; } string consoleStr = arguments[1].ToLowerInvariant(); string inputName = arguments[2].ToLowerInvariant(); int consoleID = -1; InputData inputData = null; using (BotDBContext context = DatabaseManager.OpenContext()) { GameConsole console = context.Consoles.FirstOrDefault(c => c.Name == consoleStr); if (console == null) { QueueMessage($"No console named \"{consoleStr}\" found."); return; } consoleID = console.ID; //Check if the input exists inputData = console.InputList.FirstOrDefault((inpData) => inpData.Name == inputName); if (inputData == null) { QueueMessage($"Input \"{inputName}\" does not exist in console \"{consoleStr}\"."); return; } } //Compare this user's level with the user they're trying to restrict User thisUser = DataHelper.GetUser(args.Command.ChatMessage.Username); if (thisUser == null) { QueueMessage("Huh? The user calling this doesn't exist in the database!"); return; } if (thisUser.Level <= restrictedUser.Level) { QueueMessage("Cannot restrict inputs for users greater than or equal to you in level!"); return; } DateTime nowUTC = DateTime.UtcNow; string expirationArg = arguments[3].ToLowerInvariant(); DateTime?expiration = null; if (expirationArg != NULL_EXPIRATION_ARG) { if (Helpers.TryParseTimeModifierFromStr(expirationArg, out TimeSpan timeFromNow) == false) { QueueMessage("Unable to parse expiration time from now."); return; } //Set the time to this amount from now expiration = nowUTC + timeFromNow; } using (BotDBContext context = DatabaseManager.OpenContext()) { restrictedUser = DataHelper.GetUserNoOpen(username, context); //See if a restricted input already exists RestrictedInput restInput = restrictedUser.RestrictedInputs.FirstOrDefault(r => r.inputData.Name == inputName && r.inputData.ConsoleID == consoleID); //Already restricted - update the expiration if (restInput != null) { restInput.Expiration = expiration; QueueMessage($"Updated \"{inputName}\" restriction for the \"{consoleStr}\" console on {restrictedUser.Name}! Expires in {expirationArg}!"); } //Add a new restricted input else { //Add the restricted input RestrictedInput newRestrictedInput = new RestrictedInput(restrictedUser.ID, inputData.ID, expiration); restrictedUser.RestrictedInputs.Add(newRestrictedInput); QueueMessage($"Restricted {restrictedUser.Name} from inputting \"{inputName}\" for the \"{consoleStr}\" console! Expires in {expirationArg}!"); } //Save context.SaveChanges(); } }
public override void ExecuteCommand(EvtChatCommandArgs args) { List <string> arguments = args.Command.ArgumentsAsList; int argCount = arguments.Count; //Ignore with not enough arguments if (argCount != 3) { QueueMessage(UsageMessage); return; } string username = arguments[0]; User restrictedUser = DataHelper.GetUser(username); //Check for the user if (restrictedUser == null) { QueueMessage("A user with this name does not exist in the database!"); return; } //Compare this user's level with the user they're trying to restrict User thisUser = DataHelper.GetUser(args.Command.ChatMessage.Username); if (thisUser == null) { QueueMessage("Huh? The user calling this doesn't exist in the database!"); return; } if (thisUser.Level <= restrictedUser.Level) { QueueMessage("Cannot remove restricted inputs for users greater than or equal to you in level!"); return; } string consoleStr = arguments[1].ToLowerInvariant(); long consoleID = 0L; using (BotDBContext context = DatabaseManager.OpenContext()) { GameConsole console = context.Consoles.FirstOrDefault(c => c.Name == consoleStr); if (console == null) { QueueMessage($"No console named \"{consoleStr}\" found."); return; } consoleID = console.ID; } string inputName = arguments[2].ToLowerInvariant(); using (BotDBContext context = DatabaseManager.OpenContext()) { restrictedUser = DataHelper.GetUserNoOpen(username, context); //Check if the restricted input exists for this console RestrictedInput restrictedInput = restrictedUser.RestrictedInputs.FirstOrDefault(r => r.inputData.Name == inputName && r.inputData.ConsoleID == consoleID); //Not restricted if (restrictedInput == null) { QueueMessage($"{restrictedUser.Name} already has no restrictions on inputting \"{inputName}\" on the \"{consoleStr}\" console!"); return; } //Remove the restricted input and save restrictedUser.RestrictedInputs.Remove(restrictedInput); context.SaveChanges(); } QueueMessage($"Lifted the restriction for {restrictedUser.Name} on inputting \"{inputName}\" on the \"{consoleStr}\" console!"); }