Skip to content

Commit

Permalink
1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
egorprnn committed Mar 31, 2020
1 parent 4da58ce commit 363722a
Show file tree
Hide file tree
Showing 15 changed files with 343 additions and 221 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
worker: node index.js
worker: npm start
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

<p align="center">
Перед началом работы настройте config.json!
<br/>
Скрипт запускается командой <b>npm start</b>!
</p>
<p align="center"><a href="https://www.codefactor.io/repository/github/mrzillagold/vk2discord"><img src="https://www.codefactor.io/repository/github/mrzillagold/vk2discord/badge" alt="CodeFactor" /></a></p>

Expand All @@ -19,7 +21,11 @@
"longpoll": false // Использовать Longpoll API. true = Вкл. / false = Выкл.
},
"discord": {
"webhook_url": "https://discordapp.com/api/webhooks/", // Ваш WebHook URL.
"webhook_urls": [
"https://discordapp.com/api/webhooks/",
"https://discordapp.com/api/webhooks/",
...
], // Ссылки на Webhook, можно использовать несколько ссылок на разные каналы Discord.
"bot_name": "VK2DISCORD", // Имя вашего WebHook, выcвечиваетеся в качестве имени бота.
"color": "#aabbcc" // Цвет рамки сообщения Discord в формате HEX.
},
Expand Down
7 changes: 5 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
"longpoll": false
},
"discord": {
"webhook_url": "https://discordapp.com/api/webhooks/",
"webhook_urls": [
"https://discordapp.com/api/webhooks/",
"https://discordapp.com/api/webhooks/"
],
"bot_name": "VK2DISCORD",
"color": "#aabbcc"
},
"interval": 30
}
}
1 change: 0 additions & 1 deletion index.js

This file was deleted.

1 change: 1 addition & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./modules/handler";
16 changes: 16 additions & 0 deletions modules/discord.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import webhook from "webhook-discord";

import config from "../config";

const name = config.discord.bot_name.slice(0, 32);
const color = config.discord.color.match(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/m) ? config.discord.color : "#aabbcc";
const urls = config.discord.webhook_urls;
/*const sendAll = config.discord.send_all;*/

export {
webhook,
name,
color,
urls,
/*sendAll*/
};
60 changes: 0 additions & 60 deletions modules/functions.js

This file was deleted.

75 changes: 0 additions & 75 deletions modules/handler.js

This file was deleted.

80 changes: 80 additions & 0 deletions modules/handler.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { updates, api, longpoll, groupId, interval, filter } from "./vk";

import { webhook } from "./discord";

import { Sender } from "./sender";

if (!longpoll) {
if (interval < 30000) console.log("[!] Не рекомендуем ставить интервал получения постов меньше 30 секунд, во избежания лимитов ВКонтакте!");

setInterval(() => {
const sender = new Sender();

const groupIdMatch = groupId.match(/^(?:public|group)([\d]+)$/);
const userIdMatch = groupId.match(/^id([\d]+)$/);
const id = groupIdMatch ?
{
owner_id: -groupIdMatch[1]
}
:
userIdMatch ?
{
owner_id: userIdMatch[1]
}
:
{
domain: groupId
};

api.wall.get({
...id,
count: 2,
extended: 1,
filter: filter ? "owner" : "all",
v: "5.103"
})
.then(data => {
const builder = new webhook.MessageBuilder();

if (data.groups.length > 0 && groupIdMatch) { // Устанавливаем footer от типа отправителя записи
builder.setFooter(data.groups[0].name, data.groups[0].photo_50);
} else if (data.profiles.length > 0) {
builder.setFooter(`${data.profiles[0].first_name} ${data.profiles[0].last_name}`, data.profiles[0].photo_50);
}

const posts = data.items; // Проверяем наличие закрепа, если он есть берем свежую запись
const post1 = posts[0];
const post2 = posts[1];

if (posts.length > 0) {
const postData = posts.length === 2 && post2.date > post1.date ? post2 : post1;

sender.Post(builder, postData);
} else {
console.log("[!] Не получено ни одной записи. Проверьте наличие записей в группе или измените значение фильтра в конфигурации.");
}

})
.catch(err => errorHandler(err));
}, interval);
} else {
updates.on("new_wall_post", context => {
const sender = new Sender();

sender.Post(new webhook.MessageBuilder(), context.wall)
});

updates.start()
.then(() => console.log("[VK2DISCORD] Подключен к ВКонтакте!"))
.catch(err => errorHandler(err));
}

console.log("[VK2DISCORD] Запущен.");

function errorHandler(error) {
console.log(`[!] Возникла ошибка: ${error}. Если не понимаете в чем причина, свяжитесь со мной: https://vk.com/id233731786`);
}

export {
errorHandler
};
75 changes: 0 additions & 75 deletions modules/send.js

This file was deleted.

Loading

0 comments on commit 363722a

Please sign in to comment.