Skip to content

Commit

Permalink
code saving
Browse files Browse the repository at this point in the history
  • Loading branch information
melod1n committed Oct 8, 2021
1 parent ffab674 commit 4552bf0
Show file tree
Hide file tree
Showing 38 changed files with 1,021 additions and 460 deletions.
7 changes: 7 additions & 0 deletions .run/index.js.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="index.js" type="NodeJSConfigurationType" nameIsGenerated="true" path-to-js-file="src/index.js" working-dir="$PROJECT_DIR$">
<method v="2">
<option name="TypeScript.Before.Run" enabled="true" FAIL_ON_ERROR="true" CONFIG_PATH="$PROJECT_DIR$/tsconfig.json" />
</method>
</configuration>
</component>
Binary file removed data/database.sql
Binary file not shown.
6 changes: 3 additions & 3 deletions data/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sendActionMessage": true,
"messagesReceived": 696,
"messagesSent": 377,
"testAnswer": true,
"messagesReceived": 0,
"messagesSent": 0
"sendActionMessage": true
}
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"inversify": "^5.1.1",
"node-fetch": "^2.6.1",
"reflect-metadata": "^0.1.13",
"sql-template-strings": "^2.2.2",
"sqlite": "^4.0.23",
"sqlite3": "^5.0.2",
"systeminformation": "^5.6.1",
"ts-node": "^9.1.1",
Expand Down
15 changes: 10 additions & 5 deletions src/api/loader/chats-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {Loader} from '../../model/loader';
import {VkChat} from '../../model/vk-chat';
import {vk} from '../../index';
import {VkUser} from '../../model/vk-user';
import {CacheStorage} from '../../database/cache-storage';
import {MemoryCache} from '../../database/memory-cache';

export class ChatsLoader extends Loader<VkChat> {

Expand All @@ -25,8 +27,8 @@ export class ChatsLoader extends Loader<VkChat> {

const jsonMembers = await vk.api.call('messages.getConversationMembers',
{
peer_id: chat.peerId,
fields: 'photo200, online,online_mobile,online_info, sex'
peer_id: chat.id,
fields: 'photo200, online, online_mobile, online_info, sex'
}
);

Expand All @@ -44,10 +46,13 @@ export class ChatsLoader extends Loader<VkChat> {

resolve(chats);

// const usersPromise = this.cacheStorage.users.store(users);
// const chatsPromise = this.cacheStorage.chats.store(chats);
chats.forEach(chat => MemoryCache.appendChat(chat));
users.forEach(user => MemoryCache.appendUser(user));

// await Promise.all([usersPromise, chatsPromise]);
const usersPromise = CacheStorage.users.store(users);
const chatsPromise = CacheStorage.chats.store(chats);

await Promise.all([usersPromise, chatsPromise]);
} catch (e) {
reject(e);
}
Expand Down
8 changes: 6 additions & 2 deletions src/api/loader/users-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Loader} from '../../model/loader';
import {VkUser} from '../../model/vk-user';
import {vk} from '../../index';
import {CacheStorage} from '../../database/cache-storage';
import {MemoryCache} from '../../database/memory-cache';

export class UsersLoader extends Loader<VkUser> {

Expand All @@ -16,13 +17,16 @@ export class UsersLoader extends Loader<VkUser> {
const users = VkUser.parse(r);
resolve(users);

users.forEach(user => MemoryCache.appendUser(user));

await CacheStorage.users.store(users);
});
});
}

async loadSingle(params: any): Promise<VkUser> {
return Promise.resolve(undefined);
async loadSingle(userId: number): Promise<VkUser> {
if (isFinite(userId)) return null;
return new Promise((resolve, reject) => this.load([userId]).then(users => resolve(users[0])).catch(reject));
}

}
63 changes: 0 additions & 63 deletions src/commands/add-admin.ts

This file was deleted.

123 changes: 116 additions & 7 deletions src/commands/admins.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,141 @@
import {Command} from '../model/chat-command';
import {Command, Requirements} from '../model/chat-command';
import {Api} from '../api/api';
import {LoadManager} from '../api/load-manager';
import {MemoryCache} from '../database/memory-cache';
import {CacheStorage} from '../database/cache-storage';
import {MessageContext, MessageForwardsCollection} from 'vk-io';
import {TAG, TAG_ERROR} from '../index';

export class Admins extends Command {
class AdminsList extends Command {
regexp = /^\/admins/i;
title = '/admins';
description = 'list of bot\'s admins';

async execute(context) {
if (MemoryCache.admins.length == 0) {
if (MemoryCache.isAdminsEmpty()) {
await Api.sendMessage(context, 'Администраторов нет 😞');
return;
}

let text = 'Список администраторов:\n\n';

for (let i = 0; i < MemoryCache.admins.length; i++) {
const id = MemoryCache.admins[i];
for (let i = 0; i < MemoryCache.adminsSize(); i++) {
const id = MemoryCache.getAdminByIndex(i);

let user = await CacheStorage.users.getSingle(id);
let user = await MemoryCache.getUser(id);
if (!user) user = await LoadManager.users.loadSingle(id);

text += `❤ @id${id}(${user.firstName} ${user.lastName})\n`;
}

await Api.sendMessage(context, text, true);
}
}

}
class AdminAdd extends Command {

regexp = /^\/addadmin/i;
title = '/addAdmin';
description = 'adds bot\'s admin by id or replied message';

requirements = Requirements.Build().apply(true);

async execute(
context: MessageContext,
params: string[],
fwd?: MessageForwardsCollection,
reply?: MessageContext
): Promise<void> {

let userId: number = null;

if (reply) {
userId = reply.senderId;
}

if (!userId) {
if (context.text.includes(' ')) {
const split = context.text.split(' ');
try {
userId = Number(split[1]);
if (isNaN(userId)) userId = -1;
} catch (e) {
userId = -1;
}
}
}

if (!userId || userId < 0) {
console.log(`${TAG_ERROR}: /addAdmin: wrong userId`);
await Api.sendMessage(context, 'Неверный userId.', null, context.id);
} else {
console.log(`${TAG}: /addAdmin: added new admin: ${userId}`);

let user = await MemoryCache.getUser(userId);
if (!user) {
await Api.sendMessage(context, 'секунду...');
user = await LoadManager.users.loadSingle(userId);
}

const message = `@id${user.id}(${user.firstName} ${user.lastName}) теперь администратор! 🥳`;

const sendMessagePromise = Api.sendMessage(context, message, true);
const storeUserPromise = CacheStorage.users.storeSingle(user);
const storeAdminPromise = CacheStorage.admins.storeSingle(userId);

await Promise.all([sendMessagePromise, storeUserPromise, storeAdminPromise]);
}
}

}

class AdminRemove extends Command {

regexp = /^\/removeAdmin/i;
title = '/removeAdmin';
name = '/removeAdmin';
description = 'removes bot\'s admin';

requirements = Requirements.Build().apply(true);

async execute(context, params, fwd, reply): Promise<void> {

let userId: number = null;

if (reply) {
userId = reply.senderId;
}

if (!userId) {
if (context.text.includes(' ')) {
try {
userId = Number(context.text.split(' ')[1]);
if (isNaN(userId)) userId = null;
} catch (e) {
userId = null;
}
}
}

if (!userId || userId < 0) {
console.log(`${TAG_ERROR}: /removeAdmin: wrong userId`);
await Api.sendMessage(context, 'Неверный userId.', null, context.id);
} else {
console.log(`${TAG}: /removeAdmin: removed admin: ${userId}`);
await CacheStorage.admins.deleteSingle(userId);

let user = await MemoryCache.getUser(userId);
if (!user) {
await Api.sendMessage(context, 'секунду...');
user = await LoadManager.users.loadSingle(userId);
}

const message = `@id${user.id}(${user.firstName} ${user.lastName}) больше не администратор 💔`;

await Api.sendMessage(context, message, true);
}
}

}

export {AdminsList, AdminAdd, AdminRemove};
2 changes: 1 addition & 1 deletion src/commands/bat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class Bat extends Command {
name = '/bat';
description = 'executed value in cmd';

requirements = Requirements.builder().apply(true);
requirements = Requirements.Build().apply(true);

async execute(context, params) {
const text = params[1];
Expand Down
2 changes: 1 addition & 1 deletion src/commands/json-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class JsonRequest extends Command {
title = '/json';
description = 'retrieved json from url';

requirements = Requirements.builder().apply(false, true, false, false, false, false);
requirements = Requirements.Build().apply(false, true, false, false, false, false);

async execute(context, params) {
let url = params[1];
Expand Down
2 changes: 1 addition & 1 deletion src/commands/kick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class Kick extends Command {
name = '/kick';
description = 'kicks user from chat';

requirements = Requirements.builder().apply(false, true, true, true, false, true);
requirements = Requirements.Build().apply(false, true, true, true, false, true);

async execute(context) {
const message = await LoadManager.messages.loadByConversationMessageId(
Expand Down
Loading

0 comments on commit 4552bf0

Please sign in to comment.