Skip to content

Commit df05d3a

Browse files
committed
fix: Fix typeofweb command
1 parent 1076ebf commit df05d3a

File tree

4 files changed

+181
-31
lines changed

4 files changed

+181
-31
lines changed

.env.dev-sample

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ SPOTIFY_SECRET=
88

99
GITHUB_WEBHOOK_SECRET=
1010
GITHUB_WEBHOOK_DISCORD_URL=
11+
12+
ALGOLIA_APP_ID=
13+
ALGOLIA_API_KEY=
14+
ALGOLIA_INDEX_NAME=

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"author": "Michał Miszczyszyn <[email protected]> (https://typeofweb.com/)",
2424
"license": "ISC",
2525
"dependencies": {
26+
"algoliasearch": "4.12.0",
2627
"bufferutil": "4.0.3",
2728
"discord.js": "12.5.3",
2829
"dotenv": "10.0.0",

src/commands/towarticle.ts

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,78 @@
11
import { Command } from '../types';
2-
import fetch from 'node-fetch';
32
import { polishPlurals } from 'polish-plurals';
3+
import { getConfig } from '../config';
4+
import Algoliasearch from 'algoliasearch';
5+
import { MessageEmbed } from 'discord.js';
46

57
const pluralize = (count: number) => polishPlurals('artykuł', 'artykuły', 'artykułów', count);
68

9+
const PER_PAGE = 3;
10+
711
const typeofweb: Command = {
812
name: 'typeofweb',
913
description: 'Wyszukuje artykuły z Type of Web',
1014
args: true,
1115
async execute(msg, args) {
12-
const query = encodeURIComponent(args.join(' '));
13-
const res = await fetch(
14-
`https://typeofweb.com/wp-json/wp/v2/search?search=${query}&per_page=3`,
15-
);
16+
const ALGOLIA_APP_ID = getConfig('ALGOLIA_APP_ID');
17+
const ALGOLIA_API_KEY = getConfig('ALGOLIA_API_KEY');
18+
const ALGOLIA_INDEX_NAME = getConfig('ALGOLIA_INDEX_NAME');
19+
20+
const client = Algoliasearch(ALGOLIA_APP_ID, ALGOLIA_API_KEY);
21+
const index = client.initIndex(ALGOLIA_INDEX_NAME);
22+
23+
const query = args.join(' ');
24+
const res = await index.search<TypeOfWebAlgoliaResult>(query, {
25+
hitsPerPage: PER_PAGE,
26+
});
27+
const total = res.hits.length;
1628

17-
const data = (await res.json()) as ToWSearchResponse[];
18-
if (!data.length) {
29+
if (!total) {
1930
return msg.channel.send(`Niestety nic nie znalazłam 😭`);
2031
}
21-
const total = data.length;
22-
const message = `Pokazuję pierwsze ${total} ${pluralize(total)}` + ':';
2332

24-
const article = data.slice(0, total);
25-
return msg.channel.send([message, ...article.map((doc) => doc.url)]);
33+
console.log(res.hits.map((h) => h.authors));
34+
35+
const results = res.hits.map((h) =>
36+
new MessageEmbed()
37+
.setTitle(h.title)
38+
.setURL(`https://typeofweb.com/${h.permalink}`)
39+
.setThumbnail(`https://typeofweb.com/${h.img.url.replace(/^\/public\//, '')}`)
40+
.setFooter(`https://typeofweb.com/${h.permalink}`)
41+
.setColor([28, 160, 86])
42+
.setAuthor(
43+
h.authors.map((a) => a.replace(/(?:^|-)(\w)/g, (_, m) => ' ' + m.toUpperCase()).trim()),
44+
)
45+
.setDescription(h.excerpt),
46+
);
47+
48+
return results.reduce(async (acc, embed) => {
49+
await acc;
50+
return msg.channel.send({ embed });
51+
}, msg.channel.send(`Pokazuję pierwsze ${total} ${pluralize(total)}:`));
2652
},
2753
};
2854

2955
export default typeofweb;
3056

31-
interface ToWSearchResponse {
32-
id: number;
57+
interface TypeOfWebAlgoliaResult {
3358
title: string;
34-
url: string;
59+
date: Date;
3560
type: string;
36-
subtype: string;
37-
_links: Links;
38-
}
39-
40-
interface Links {
41-
self: Self[];
42-
about: About[];
43-
collection: Collection[];
61+
permalink: string;
62+
authors: string[];
63+
excerpt: string;
64+
content: string;
65+
img: Img;
66+
category: Category;
4467
}
4568

46-
interface Self {
47-
embeddable: boolean;
48-
href: string;
69+
interface Category {
70+
slug: string;
71+
name: string;
4972
}
5073

51-
interface About {
52-
href: string;
53-
}
54-
55-
interface Collection {
56-
href: string;
74+
interface Img {
75+
url: string;
76+
width: number;
77+
height: number;
5778
}

yarn.lock

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,110 @@
22
# yarn lockfile v1
33

44

5+
"@algolia/[email protected]":
6+
version "4.12.0"
7+
resolved "https://registry.yarnpkg.com/@algolia/cache-browser-local-storage/-/cache-browser-local-storage-4.12.0.tgz#1f873e4f28a39d25b0a589ebe8f826509458e1fb"
8+
integrity sha512-l+G560B6N1k0rIcOjTO1yCzFUbg2Zy2HCii9s03e13jGgqduVQmk79UUCYszjsJ5GPJpUEKcVEtAIpP7tjsXVA==
9+
dependencies:
10+
"@algolia/cache-common" "4.12.0"
11+
12+
"@algolia/[email protected]":
13+
version "4.12.0"
14+
resolved "https://registry.yarnpkg.com/@algolia/cache-common/-/cache-common-4.12.0.tgz#c1111a4d3e9ba2d52cadb4523152580db0887293"
15+
integrity sha512-2Z8BV+NX7oN7RmmQbLqmW8lfN9aAjOexX1FJjzB0YfKC9ifpi9Jl4nSxlnbU+iLR6QhHo0IfuyQ7wcnucCGCGQ==
16+
17+
"@algolia/[email protected]":
18+
version "4.12.0"
19+
resolved "https://registry.yarnpkg.com/@algolia/cache-in-memory/-/cache-in-memory-4.12.0.tgz#f4bdcbf8a6419f0166cfc7ef5594af871741e29e"
20+
integrity sha512-b6ANkZF6vGAo+sYv6g25W5a0u3o6F549gEAgtTDTVA1aHcdWwe/HG/dTJ7NsnHbuR+A831tIwnNYQjRp3/V/Jw==
21+
dependencies:
22+
"@algolia/cache-common" "4.12.0"
23+
24+
"@algolia/[email protected]":
25+
version "4.12.0"
26+
resolved "https://registry.yarnpkg.com/@algolia/client-account/-/client-account-4.12.0.tgz#b28445b47e2abf81dc76982d16ba8458f5c99521"
27+
integrity sha512-gzXN75ZydNheNXUN3epS+aLsKnB/PHFVlGUUjXL8WHs4lJP3B5FtHvaA/NCN5DsM3aamhuY5p0ff1XIA+Lbcrw==
28+
dependencies:
29+
"@algolia/client-common" "4.12.0"
30+
"@algolia/client-search" "4.12.0"
31+
"@algolia/transporter" "4.12.0"
32+
33+
"@algolia/[email protected]":
34+
version "4.12.0"
35+
resolved "https://registry.yarnpkg.com/@algolia/client-analytics/-/client-analytics-4.12.0.tgz#470f115517256c92a5605ae95762531c7906ec74"
36+
integrity sha512-rO2cZCt00Opk66QBZb7IBGfCq4ZE3EiuGkXssf2Monb5urujy0r8CknK2i7bzaKtPbd2vlvhmLP4CEHQqF6SLQ==
37+
dependencies:
38+
"@algolia/client-common" "4.12.0"
39+
"@algolia/client-search" "4.12.0"
40+
"@algolia/requester-common" "4.12.0"
41+
"@algolia/transporter" "4.12.0"
42+
43+
"@algolia/[email protected]":
44+
version "4.12.0"
45+
resolved "https://registry.yarnpkg.com/@algolia/client-common/-/client-common-4.12.0.tgz#402395e2cffad89188d76b83615acffb3e45e658"
46+
integrity sha512-fcrFN7FBmxiSyjeu3sF4OnPkC1l7/8oyQ8RMM8CHpVY8cad6/ay35MrfRfgfqdzdFA8LzcBYO7fykuJv0eOqxw==
47+
dependencies:
48+
"@algolia/requester-common" "4.12.0"
49+
"@algolia/transporter" "4.12.0"
50+
51+
"@algolia/[email protected]":
52+
version "4.12.0"
53+
resolved "https://registry.yarnpkg.com/@algolia/client-personalization/-/client-personalization-4.12.0.tgz#09c89c1558a91db3bfa60d17f7258ae63861352b"
54+
integrity sha512-wCJfSQEmX6ZOuJBJGjy+sbXiW0iy7tMNAhsVMV9RRaJE4727e5WAqwFWZssD877WQ74+/nF/VyTaB1+wejo33Q==
55+
dependencies:
56+
"@algolia/client-common" "4.12.0"
57+
"@algolia/requester-common" "4.12.0"
58+
"@algolia/transporter" "4.12.0"
59+
60+
"@algolia/[email protected]":
61+
version "4.12.0"
62+
resolved "https://registry.yarnpkg.com/@algolia/client-search/-/client-search-4.12.0.tgz#ac099ee9f8de85ec204d840bcac734224c7d150c"
63+
integrity sha512-ik6dswcTQtOdZN+8aKntI9X2E6Qpqjtyda/+VANiHThY9GD2PBXuNuuC2HvlF26AbBYp5xaSE/EKxn1DIiIJ4Q==
64+
dependencies:
65+
"@algolia/client-common" "4.12.0"
66+
"@algolia/requester-common" "4.12.0"
67+
"@algolia/transporter" "4.12.0"
68+
69+
"@algolia/[email protected]":
70+
version "4.12.0"
71+
resolved "https://registry.yarnpkg.com/@algolia/logger-common/-/logger-common-4.12.0.tgz#0f9dbe7ace88194b395a2cb958490eb47ac91f8e"
72+
integrity sha512-V//9rzLdJujA3iZ/tPhmKR/m2kjSZrymxOfUiF3024u2/7UyOpH92OOCrHUf023uMGYHRzyhBz5ESfL1oCdh7g==
73+
74+
"@algolia/[email protected]":
75+
version "4.12.0"
76+
resolved "https://registry.yarnpkg.com/@algolia/logger-console/-/logger-console-4.12.0.tgz#a40edeb989bf0d7ff79d989171dad64cd0f01225"
77+
integrity sha512-pHvoGv53KXRIJHLk9uxBwKirwEo12G9+uo0sJLWESThAN3v5M+ycliU1AkUXQN8+9rds2KxfULAb+vfyfBKf8A==
78+
dependencies:
79+
"@algolia/logger-common" "4.12.0"
80+
81+
"@algolia/[email protected]":
82+
version "4.12.0"
83+
resolved "https://registry.yarnpkg.com/@algolia/requester-browser-xhr/-/requester-browser-xhr-4.12.0.tgz#64e8e4d4f0724e477421454215195400351cfe61"
84+
integrity sha512-rGlHNMM3jIZBwSpz33CVkeXHilzuzHuFXEEW1icP/k3KW7kwBrKFJwBy42RzAJa5BYlLsTCFTS3xkPhYwTQKLg==
85+
dependencies:
86+
"@algolia/requester-common" "4.12.0"
87+
88+
"@algolia/[email protected]":
89+
version "4.12.0"
90+
resolved "https://registry.yarnpkg.com/@algolia/requester-common/-/requester-common-4.12.0.tgz#b4d96f3cbd73206b6042e523d414a34cc005c2e2"
91+
integrity sha512-qgfdc73nXqpVyOMr6CMTx3nXvud9dP6GcMGDqPct+fnxogGcJsp24cY2nMqUrAfgmTJe9Nmy7Lddv0FyHjONMg==
92+
93+
"@algolia/[email protected]":
94+
version "4.12.0"
95+
resolved "https://registry.yarnpkg.com/@algolia/requester-node-http/-/requester-node-http-4.12.0.tgz#8d8e1b67edbaec8e8e8b8c7c606945b969667267"
96+
integrity sha512-mOTRGf/v/dXshBoZKNhMG00ZGxoUH9QdSpuMKYnuWwIgstN24uj3DQx+Ho3c+uq0TYfq7n2v71uoJWuiW32NMQ==
97+
dependencies:
98+
"@algolia/requester-common" "4.12.0"
99+
100+
"@algolia/[email protected]":
101+
version "4.12.0"
102+
resolved "https://registry.yarnpkg.com/@algolia/transporter/-/transporter-4.12.0.tgz#e375e10731df95f1be3593b32e86b5c6452cc213"
103+
integrity sha512-MOQVHZ4BcBpf3LtOY/3fqXHAcvI8MahrXDHk9QrBE/iGensQhDiZby5Dn3o2JN/zd9FMnVbdPQ8gnkiMwZiakQ==
104+
dependencies:
105+
"@algolia/cache-common" "4.12.0"
106+
"@algolia/logger-common" "4.12.0"
107+
"@algolia/requester-common" "4.12.0"
108+
5109
6110
version "7.12.11"
7111
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.11.tgz#f4ad435aa263db935b8f10f2c552d23fb716a63f"
@@ -767,6 +871,26 @@ ajv@^8.0.1:
767871
require-from-string "^2.0.2"
768872
uri-js "^4.2.2"
769873

874+
875+
version "4.12.0"
876+
resolved "https://registry.yarnpkg.com/algoliasearch/-/algoliasearch-4.12.0.tgz#30f2619b6e3a5b79b6aa0f18ab66fbce88240aba"
877+
integrity sha512-fZOMMm+F3Bi5M/MoFIz7hiuyCitJza0Hu+r8Wzz4LIQClC6YGMRq7kT6NNU1fSSoFDSeJIwMfedbbi5G9dJoVQ==
878+
dependencies:
879+
"@algolia/cache-browser-local-storage" "4.12.0"
880+
"@algolia/cache-common" "4.12.0"
881+
"@algolia/cache-in-memory" "4.12.0"
882+
"@algolia/client-account" "4.12.0"
883+
"@algolia/client-analytics" "4.12.0"
884+
"@algolia/client-common" "4.12.0"
885+
"@algolia/client-personalization" "4.12.0"
886+
"@algolia/client-search" "4.12.0"
887+
"@algolia/logger-common" "4.12.0"
888+
"@algolia/logger-console" "4.12.0"
889+
"@algolia/requester-browser-xhr" "4.12.0"
890+
"@algolia/requester-common" "4.12.0"
891+
"@algolia/requester-node-http" "4.12.0"
892+
"@algolia/transporter" "4.12.0"
893+
770894
[email protected], ansi-colors@^4.1.1:
771895
version "4.1.1"
772896
resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-4.1.1.tgz#cbb9ae256bf750af1eab344f229aa27fe94ba348"

0 commit comments

Comments
 (0)