added undone command

This commit is contained in:
liyunze 2023-08-16 22:37:00 +08:00
parent 601a81c38b
commit f3af7d3348
3 changed files with 138 additions and 6 deletions

View file

@ -3,7 +3,7 @@ const configs = (function () {
// settings // settings
const enableLimit = true; // true or false const enableLimit = true; // true or false
const limit = 5; // integer const limit = 10; // integer
// animation // animation
const scrollSpeed = 30; // milliseconds (DO NOT PUT BELOW 5, COMPUTER MIGHT CRASH) the lower the value, the faster the scroll const scrollSpeed = 30; // milliseconds (DO NOT PUT BELOW 5, COMPUTER MIGHT CRASH) the lower the value, the faster the scroll
@ -126,6 +126,20 @@ const configs = (function () {
"!finished", "!finished",
]; ];
// Unfinish task commands - please add commands in the exact format
const unfinishTaskCommands = [
"!tasku",
"!taskunfinish",
"!utask",
"!unfinishtask",
"!taskud",
"!taskundone",
"!undonetask",
"!undone",
"!unfinish",
"!unfinished",
];
// Check task commands - please add commands in the exact format // Check task commands - please add commands in the exact format
const checkCommands = [ const checkCommands = [
"!taskc", "!taskc",
@ -187,7 +201,8 @@ const configs = (function () {
const noTaskAdded = const noTaskAdded =
"Looks like you already hit the limit of incomplete tasks, {user}"; "Looks like you already hit the limit of incomplete tasks, {user}";
const noTaskContent = "Try using !task the-task-you-are-working-on {user}"; const noTaskContent = "Try using !task the-task-you-are-working-on {user}";
const taskDeleted = 'Task "{task}" has been deleted successfully, {user}'; const taskDeleted =
'Task(s) "{task}" has been deleted successfully, {user}';
const specifyTaskIndex = "Try specifying the index of the task {user}"; const specifyTaskIndex = "Try specifying the index of the task {user}";
const duplicateTask = const duplicateTask =
@ -198,6 +213,8 @@ const configs = (function () {
const adminDeleteTasks = "All of {mentioned}'s tasks have been deleted"; const adminDeleteTasks = "All of {mentioned}'s tasks have been deleted";
const taskFinished = 'Good job on finishing the task "{task}", {user}!'; const taskFinished = 'Good job on finishing the task "{task}", {user}!';
const taskUnfinished =
'Task(s) "{task}" has been unmarked as done, {user}!';
const taskEdited = const taskEdited =
"Task '{originalTask}' has been edited to '{task}' successfully, {user}"; "Task '{originalTask}' has been edited to '{task}' successfully, {user}";
const noTaskEdit = "Try doing !edit [index] [new task] {user}"; const noTaskEdit = "Try doing !edit [index] [new task] {user}";
@ -290,6 +307,7 @@ const configs = (function () {
editTaskCommands, editTaskCommands,
deleteTaskCommands, deleteTaskCommands,
finishTaskCommands, finishTaskCommands,
unfinishTaskCommands,
helpCommands, helpCommands,
checkCommands, checkCommands,
clearMyDoneCommands, clearMyDoneCommands,
@ -308,6 +326,7 @@ const configs = (function () {
specifyTaskIndex, specifyTaskIndex,
duplicateTask, duplicateTask,
taskFinished, taskFinished,
taskUnfinished,
taskEdited, taskEdited,
noTaskEdit, noTaskEdit,
noTask, noTask,

View file

@ -107,6 +107,27 @@ ComfyJS.onCommand = (user, command, message, flags, extra) => {
respond(finishedResponse, params); respond(finishedResponse, params);
} }
} else if (commands.unfinishTaskCommands.includes(command)) {
let unfinishStatus = markTaskUndone(user, message);
if (unfinishStatus === 0) {
// user has no tasks
respond(responses.noTask, params);
} else if (unfinishStatus === 1) {
// invalid input
respond(responses.specifyTaskIndex, params);
} else {
// task unfinished
let unfinishedResponse = responses.taskUnfinished;
params.task = unfinishStatus.markedTasks;
if (unfinishStatus.failedTasks !== "") {
unfinishedResponse += ` | Failed to unfinish task(s): "${unfinishStatus.failedTasks}"`;
}
respond(unfinishedResponse, params);
}
} else if (commands.checkCommands.includes(command)) { } else if (commands.checkCommands.includes(command)) {
if (message === "") { if (message === "") {
let response = checkTasks(user); let response = checkTasks(user);

View file

@ -264,7 +264,7 @@ function removeTask(username, task) {
if (index === -1) { if (index === -1) {
if (isInt(t)) { if (isInt(t)) {
index = parseInt(t) - 1; // ACTUAL INDEX index = parseInt(t) - 1; // ACTUAL INDEX
} else if (incompleteTasks.length) { } else if (incompleteTasks.length === 1) {
index = tasks[username].todos.findIndex((t) => !t.done); index = tasks[username].todos.findIndex((t) => !t.done);
tasksRemoved.push(tasks[username].todos[index].text); tasksRemoved.push(tasks[username].todos[index].text);
removedTaskIndex.push(index); removedTaskIndex.push(index);
@ -314,7 +314,7 @@ function removeTask(username, task) {
if (index === -1) { if (index === -1) {
if (isInt(task)) { if (isInt(task)) {
index = parseInt(task) - 1; // ACTUAL INDEX index = parseInt(task) - 1; // ACTUAL INDEX
} else if (incompleteTasks.length) { } else if (incompleteTasks.length === 1) {
index = tasks[username].todos.findIndex((t) => !t.done); index = tasks[username].todos.findIndex((t) => !t.done);
} else { } else {
return 1; return 1;
@ -403,7 +403,7 @@ function markTaskDone(username, task) {
if (index === -1) { if (index === -1) {
if (isInt(t)) { if (isInt(t)) {
index = parseInt(t) - 1; // ACTUAL INDEX index = parseInt(t) - 1; // ACTUAL INDEX
} else if (incompleteTasks.length) { } else if (incompleteTasks.length === 1) {
index = tasks[username].todos.findIndex((t) => !t.done); index = tasks[username].todos.findIndex((t) => !t.done);
tasksMarkedComplete.push(tasks[username].todos[index].text); tasksMarkedComplete.push(tasks[username].todos[index].text);
} else { } else {
@ -443,7 +443,7 @@ function markTaskDone(username, task) {
if (index === -1) { if (index === -1) {
if (isInt(task)) { if (isInt(task)) {
index = parseInt(task) - 1; // ACTUAL INDEX index = parseInt(task) - 1; // ACTUAL INDEX
} else if (incompleteTasks.length) { } else if (incompleteTasks.length === 1) {
index = tasks[username].todos.findIndex((t) => !t.done); index = tasks[username].todos.findIndex((t) => !t.done);
} else { } else {
return 1; return 1;
@ -465,6 +465,98 @@ function markTaskDone(username, task) {
} }
} }
function markTaskUndone(username, task) {
const tasks = JSON.parse(localStorage.tasks);
// user does not have any tasks
if (!tasks[username] || tasks[username].todos.length === 0) {
return 0;
}
const completedTasks = tasks[username].todos.filter((t) => t.done);
// match regex: integers separated by space e.g. '1 2 3 4'
if (task.match(/^(\d+ )*\d+$/)) {
// insert commas between integers
task = task.replace(/(\d+)/g, "$1,");
// remove trailing comma
task = task.slice(0, -1);
}
let tasksMarkedUndone = [];
let tasksFailedToMarkUndone = [];
// check if there's a comma in the task (multiple tasks)
if (task.includes(",")) {
let tasksToMarkUndone = task.split(",").map((t) => t.trim());
for (const t of tasksToMarkUndone) {
let index = tasks[username].todos.findIndex(
(task) => task.text.toLowerCase() === t.toLowerCase()
);
if (index === -1) {
// user input isn't the task name
if (isInt(t)) {
index = parseInt(t) - 1; // ACTUAL INDEX
} else {
tasksFailedToMarkUndone.push(t);
continue;
}
if (index < 0 || index > tasks[username].todos.length - 1) {
tasksFailedToMarkUndone.push(t);
continue;
} else {
tasksMarkedUndone.push(tasks[username].todos[index].text);
}
} else {
tasksMarkedUndone.push(tasks[username].todos[index].text);
}
tasks[username].todos[index].done = false;
}
if (tasksMarkedUndone.length === 0) {
return 1;
}
localStorage.setItem(`tasks`, JSON.stringify(tasks));
if (!scrolling) {
renderTaskListToDOM();
}
return {
markedTasks: tasksMarkedUndone.join('", "'),
failedTasks: tasksFailedToMarkUndone.join('", "'),
};
} else {
let index = tasks[username].todos.findIndex(
(t) => t.text.toLowerCase() === task.toLowerCase()
);
if (index === -1) {
if (isInt(task)) {
index = parseInt(task) - 1; // ACTUAL INDEX
} else if (completedTasks.length === 1) {
index = tasks[username].todos.findIndex((t) => t.done);
} else {
return 1;
}
if (index < 0 || index > tasks[username].todos.length - 1) {
return 1;
}
tasks[username].todos[index].done = false;
localStorage.setItem(`tasks`, JSON.stringify(tasks));
renderTaskListToDOM();
return {
markedTasks: tasks[username].todos[index].text,
failedTasks: "",
};
}
}
}
// 0: user has no tasks // 0: user has no tasks
// 1: invalid input // 1: invalid input
// [original task, new task]: succcess // [original task, new task]: succcess