const { VK, uploadErrors, Keyboard, Context } = require('vk-io');
const vk = new VK();
const commands = [];
const users = require('./users.json');
const chats = require('./chats.json');
const fs = require('fs');
const { NONAME } = require('dns');
setInterval(async () => {
await saveUsers();
}, 1000);
async function saveUsers()
{
require('fs').writeFileSync('./users.json', JSON.stringify(users, null, '\t'));
require('fs').writeFileSync('./chats.json', JSON.stringify(chats, null, '\t'));
return true;
}
vk.setOptions({ token: '', pollingGroupId: });
/* const { updates, snippets } = vk; */
vk.updates.on('message', async (message, next) => {
if(Number(message.senderId) <= 0) return;
if(/\[club209536507\|(.*)\]/i.test(message.text)) message.text = message.text.replace(/\[clubсудаайдигруппы\|(.*)\]/ig, '').trim();
if(!chats.find(x=>x.id===message.chatId))
{
const chat = await vk.api.messages.getConversationsById({ peer_ids: message.peerId, extended: 1, fields: 'chat_settings', group_id: 209536507 }); // это обязательно
const [peer] = chat.items;
const chat_info = peer.chat_settings;
message.send(`Ваша беседа успешно активирована.`)
chats.push({
id: message.chatId,
cid: chats.length,
name: `${chat_info.title}`,
owner: chat_info.owner_id,
greeting: null,
members: []
})
}
const peer = chats.filter(x => x.id === message.chatId)[0];
const user = peer.members.filter(x => x.id === message.senderId)[0];
if(!peer.members.find(x=>x.id===message.senderId))
{
const [user_info] = await vk.api.users.get({ user_id: message.senderId });
peer.members.push({
id: message.senderId,
ban: false,
points: 0,
warn: 0,
mute: 0,
role: 0
})
}
if(peer.owner==message.senderId) return user.role=4
if(!users.find(x=>x.id===message.senderId))
{
const [user_info] = await vk.api.users.get({ user_id: message.senderId });
users.push({
id: message.senderId,
gban: false,
tag: `${user_info.first_name}`
})
}
message.user = users.find(x=> x.id === message.senderId);
const bot = (text, params) => {
return message.send(` ${message.user.mention ? `@id${message.user.id} (${message.user.nick})` : `${message.user.nick}`}, ${text}`, params);
}
const command = commands.find(x=> x[0].test(message.text));
if(!command) return;
message.args = message.text.match(command[0]);
await command[1](message, bot);
let date = new Date;
message.user.last_sms = date;
message.user.message_sms += 1;
console.log();
return next();
});
vk.updates.on(['chat_kick_user'], (next, context) => {
const peer = chats.filter(x => x.id === next.chatId)[0];
const user = peer.members.filter(x => x.id === next.eventMemberId)[0];
vk.api.messages.removeChatUser({ chat_id: next.chatId, user_id: user.id });
});
vk.updates.hear(/^(?:greeting)$/i, async (message, bot) => {
console.log(message)
return message.send(`Команда заработала.`)
});