Skip to content

Commit

Permalink
Улучшения
Browse files Browse the repository at this point in the history
  • Loading branch information
egorprnn committed Mar 13, 2020
1 parent 738b0fa commit 3b5ca6e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
30 changes: 16 additions & 14 deletions modules/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ function parseLinks(text) {
return `${text.replace(/(?:\[([^]+?)\|([^]+?)])/g, "[$2](https://vk.com/$1)")}\n\n`;
}

function parseText(text) {
return text.replace(/\[([^]+)]\([^]+\)/g, "$1");
}

function checkKeywords(keywords, text) {
if (keywords.length > 0) {
return keywords.some(keyword => {
Expand All @@ -19,33 +15,33 @@ function checkKeywords(keywords, text) {
async function getAttachments(attachments, webhookBuilder, longpoll) {
let text = "";

await attachments.reverse().forEach(item => {
await attachments.forEach(item => {
const type = item.type;

switch (type) {
case "photo":
if (!webhookBuilder.data.attachments[0].image_url) webhookBuilder.setImage(longpoll ? item.sizes.pop().url : item.photo.sizes.pop().url);
if (!webhookBuilder.data.attachments[0].image_url) webhookBuilder.setImage((longpoll ? item.sizes : item.photo.sizes).pop().url);
break;
case "video":
text += `\n[:video_camera: Смотреть видео: ${longpoll ? item.title : item.video.title}](https://vk.com/video${longpoll ? item.ownerId : item.video.owner_id}_${longpoll ? item.id : item.video.id})`;
text += `\n[:video_camera: Смотреть видео: ${(longpoll ? item : item.video).title}](https://vk.com/video${longpoll ? item.ownerId : item.video.owner_id}_${(longpoll ? item : item.video).id})`;
break;
case "link":
text += `\n[:link: ${(longpoll ? item.button_text : item.link.button_text) || "Ссылка"}: ${longpoll ? item.title : item.link.title}](${longpoll ? item.url : item.link.url})`;
text += `\n[:link: ${(longpoll ? item : item.link).button_text || "Ссылка"}: ${(longpoll ? item : item.link).title}](${(longpoll ? item : item.link).url})`;
break;
case "doc":
text += `\n[:page_facing_up: Документ: ${longpoll ? item.title : item.doc.title}](${longpoll ? item.url : item.doc.url})`;
text += `\n[:page_facing_up: Документ: ${(longpoll ? item : item.doc).title}](${(longpoll ? item : item.doc).url})`;
break;
case "audio":
const artist = longpoll ? item.artist : item.audio.artist;
const title = longpoll ? item.title : item.audio.title;
const artist = (longpoll ? item : item.audio).artist;
const title = (longpoll ? item : item.audio).title;

text += `\n[:musical_note: Музыка: ${artist} - ${title}](https://vk.com/search?c[section]=audio&c[q]=${encodeURI(artist.replace(/&/g, "и"))}%20-%20${encodeURI(title)}&c[performer]=1)`;
break;
case "poll":
let answers = "";

(longpoll ? item.answers : item.poll.answers).forEach(item => answers += `\n• ${item.text}`);
text += `\n[:bar_chart: Опрос: ${longpoll ? item.question : item.poll.question}](https://vk.com/feed?w=poll${longpoll ? item.ownerId : item.poll.owner_id}_${longpoll ? item.id : item.poll.id})`;
(longpoll ? item : item.poll).answers.forEach(item => answers += `\n• ${item.text}`);
text += `\n[:bar_chart: Опрос: ${(longpoll ? item : item.poll).question}](https://vk.com/feed?w=poll${longpoll ? item.ownerId : item.poll.owner_id}_${(longpoll ? item : item.poll).id})`;
break;
}
});
Expand All @@ -56,4 +52,10 @@ function errorHandler(error) {
console.log(`[!] Возникла ошибка: ${error}. Если не понимаете в чем причина, свяжитесь со мной: https://vk.com/id233731786`);
}

module.exports = {parseLinks, parseText, checkKeywords, getAttachments, errorHandler};
module.exports = {
parseLinks,
parseText,
checkKeywords,
getAttachments,
errorHandler
};
4 changes: 2 additions & 2 deletions modules/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const webhook = require("webhook-discord");
const news = require("../news");
const config = require("../config");

const { parseText, getAttachments, parseLinks, checkKeywords, errorHandler } = require("./functions");
const { getAttachments, parseLinks, checkKeywords, errorHandler } = require("./functions");

const keywords = config.vk.keywords;
const name = config.discord.bot_name;
Expand Down Expand Up @@ -49,7 +49,7 @@ module.exports = async (webhookBuilder, postData, longpoll) => {

const allPost = postText + attachments + repostText + reportAttachments;

webhookBuilder.setDescription(parseText(allPost).length > 2048 ?
webhookBuilder.setDescription(allPost.length > 2048 ?
(postText ? postText.slice(0, repostText ? 1021 - attachments.length : 2045 - attachments.length) + (postData.text ? "…\n\n" : "") : "")
+ attachments
+ (repostText ? repostText.slice(0, postText ? 1021 - reportAttachments.length : 2045 - reportAttachments.length) + (repost.text ? "…\n\n" : "") : "")
Expand Down

0 comments on commit 3b5ca6e

Please sign in to comment.