Skip to content

Commit

Permalink
* multiple notes deletion
Browse files Browse the repository at this point in the history
* fix adding note with more than one new line
  • Loading branch information
melod1n committed Mar 26, 2024
1 parent de836eb commit 5516921
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
61 changes: 42 additions & 19 deletions src/commands/notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ class NotesList extends Command {

class NoteAdd extends Command {

regexp = /^\/note\s([^]+)\n/i;
title = "/note [title]\n[content]";
regexp = /^\/noteadd\s([^]+)/i;
title = "/noteadd [title]\\n[content]";
description = "Add note";
requirements = Requirements.Build(Requirement.BOT_ADMIN);

async execute(context: MessageContext<ContextDefaultState>, params: string[]): Promise<void> {
const title = params[1].trim();
const content = context.text.split("\n")[1];
const split = params[1].split("\n");
const title = split[0].trim();
const content = params[1].replace(split[0], "").trim();

const note = new Note(title, content);

Expand All @@ -64,7 +65,7 @@ class NoteInfo extends Command {
const param = params[1].trim();

const titlesSplit = param.split(",");
if (titlesSplit.length > 0) {
if (titlesSplit.length > 1) {
const titles = titlesSplit.map(split => split.trim());
const quotedTitles = titles.map(title => `"${title}"`);

Expand Down Expand Up @@ -100,29 +101,51 @@ class NoteInfo extends Command {
}

class NoteDelete extends Command {
regexp = /^\/delnote\s([^]+)/i;
title = "/delnote [title]";
description = "Delete note by it's title";
regexp = /^\/notedel\s([^]+)/i;
title = "/notedel [titles]";
description = "Delete notes by its titles";
requirements = Requirements.Build(Requirement.BOT_ADMIN);

async execute(context: MessageContext, params: string[]): Promise<void> {
const title = params[1].trim();

CacheStorage.notes.deleteSingle(title).then(async (r) => {
if (r) {
await Api.sendMessage(context, `Заметка "${title}" успешно удалена.`);
} else {
await Api.sendMessage(context, "Заметка с таким названием не найдена.");
}
}).catch(async () => {
await Api.sendMessage(context, "Произошла ошибка при удалении заметки.");
});
const titlesSplit = params[1].split(",");
if (titlesSplit.length > 1) {
const titles = titlesSplit.map(split => split.trim());
const quotedTitles = titles.map(title => `"${title}"`);

CacheStorage.notes.delete(titles).then(async (deleted) => {
if (deleted) {
const text = `Удалены следующие заметки: ${quotedTitles.join(", ")}.`;

await Api.sendMessage(context, text);
} else {
const text = `Произошла ошибка при удалении следующих заметок: ${quotedTitles.join(", ")}.`;

await Api.sendMessage(context, text);
}
}).catch(async () => {
const text = `Произошла ошибка при удалении следующих заметок: ${quotedTitles.join(", ")}.`;

await Api.sendMessage(context, text);
});
} else {
CacheStorage.notes.deleteSingle(title).then(async (r) => {
if (r) {
await Api.sendMessage(context, `Заметка "${title}" успешно удалена.`);
} else {
await Api.sendMessage(context, "Заметка с таким названием не найдена.");
}
}).catch(async () => {
await Api.sendMessage(context, "Произошла ошибка при удалении заметки.");
});
}
}
}

class NotesClear extends Command {
regexp = /^\/clearnotes$/i;
title = "/clearnotes";
regexp = /^\/notesclear$/i;
title = "/notesclear";
description = "Clear all notes";
requirements = Requirements.Build(Requirement.BOT_ADMIN);

Expand Down
12 changes: 7 additions & 5 deletions src/commands/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Command} from "../model/chat-command";
import {Utils} from "../util/utils";
import {Api} from "../api/api";
import {StorageManager} from "../database/storage-manager";
import {MessageContext} from "vk-io";

export let receivedMessagesCount = 0;
export let sentMessagesCount = 0;
Expand All @@ -23,15 +24,16 @@ export class Stats extends Command {
title = "/stats";
description = "bot's stats";

async execute(context) {
async execute(context: MessageContext) {
let text = "Статистика бота.\n\n";

text += `⏳ Время работы: ${Utils.getUptime()}\n\n`;
text += `📩 Получено сообщений: ${receivedMessagesCount}\n`;
text += `📧 Отправлено сообщений: ${sentMessagesCount}\n\n`;

text += `✉ Всего получено сообщений: ${StorageManager.stats.receivedCount}\n`;
text += `✉ Всего отправлено сообщений: ${StorageManager.stats.sentCount}`;
text += `📥 Получено сообщений: ${receivedMessagesCount}\n`;
text += `📤 Отправлено сообщений: ${sentMessagesCount}\n\n`;

text += `📥 Получено сообщений (всего): ${StorageManager.stats.receivedCount}\n`;
text += `📤 Отправлено сообщений (всего): ${StorageManager.stats.sentCount}`;

await Api.sendMessage(context, text);
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const BOT_VERSION = "0.0.3";
export const BOT_VERSION = "0.0.4";

0 comments on commit 5516921

Please sign in to comment.