From f25b4ea2ef0952bdb6f90ca4eeb031dd21ba10f1 Mon Sep 17 00:00:00 2001 From: "Kim, Hyeonseo" Date: Sun, 17 Dec 2023 07:22:24 +0000 Subject: [PATCH 1/7] Add Aladin response type definition --- src/@types/{aladin.d.ts => aladin-response.d.ts} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/@types/{aladin.d.ts => aladin-response.d.ts} (97%) diff --git a/src/@types/aladin.d.ts b/src/@types/aladin-response.d.ts similarity index 97% rename from src/@types/aladin.d.ts rename to src/@types/aladin-response.d.ts index 353b439..8324b33 100644 --- a/src/@types/aladin.d.ts +++ b/src/@types/aladin-response.d.ts @@ -1,4 +1,4 @@ -type ItemSearchResult = { +type ItemSearchResponse = { version: string logo: string title: string From 5b853c1aa3b272506b05882be12283cbede18c76 Mon Sep 17 00:00:00 2001 From: "Kim, Hyeonseo" Date: Sun, 17 Dec 2023 07:23:28 +0000 Subject: [PATCH 2/7] Change ItemSearch to aladin --- index.ts | 2 +- src/{commands/itemSearch.ts => aladin.ts} | 13 ++++++++----- src/commands/index.ts | 4 ---- 3 files changed, 9 insertions(+), 10 deletions(-) rename src/{commands/itemSearch.ts => aladin.ts} (91%) delete mode 100644 src/commands/index.ts diff --git a/index.ts b/index.ts index 9a4d641..47767dc 100644 --- a/index.ts +++ b/index.ts @@ -1,5 +1,5 @@ import { Client, Interaction } from 'discord.js' -import commands from './src/commands' +import commands from './src/aladin' const client = new Client({ intents: [], diff --git a/src/commands/itemSearch.ts b/src/aladin.ts similarity index 91% rename from src/commands/itemSearch.ts rename to src/aladin.ts index f878ab5..72a4a9c 100644 --- a/src/commands/itemSearch.ts +++ b/src/aladin.ts @@ -1,9 +1,9 @@ -import { ApplicationCommandOptionType, EmbedBuilder, time } from 'discord.js' -import type { SlashCommand } from '../@types/discord' +import { ApplicationCommandOptionType, EmbedBuilder } from 'discord.js' +import type { SlashCommand } from './@types/discord' import ky from 'ky' //The option names should be all lowercased, -export const itemSearch: SlashCommand = { +const aladin: SlashCommand = { name: 'aladin', description: '상품 검색', options: [ @@ -19,7 +19,6 @@ export const itemSearch: SlashCommand = { description: '몇 건의 검색을 볼지 정합니다.', type: ApplicationCommandOptionType.Integer, }, - // TODO: 이 부분 고치기, 응답의 link로 접근하면 글자가 깨져서 들어가는 증상이 있다. { name: '검색어-종류', description: @@ -94,7 +93,7 @@ export const itemSearch: SlashCommand = { )}&QueryType=${queryType}&MaxResults=${count}&start=1&SearchTarget=${searchTarget}&output=js&Version=20131101` try { - const data = await ky.get(URL).json() + const data = await ky.get(URL).json() const { item, totalResults } = data const bookInfos = item.map((i: any): [string, string] => [ @@ -146,3 +145,7 @@ export const itemSearch: SlashCommand = { } // https://embed.dan.onl/ + +const availableCommands = [aladin] + +export default availableCommands diff --git a/src/commands/index.ts b/src/commands/index.ts deleted file mode 100644 index 915390d..0000000 --- a/src/commands/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { itemSearch } from './itemSearch' -const availableCommands = [itemSearch] - -export default availableCommands From b6342f0c436a57d8e7a87e260c17ca97bb58e90a Mon Sep 17 00:00:00 2001 From: "Kim, Hyeonseo" Date: Mon, 18 Dec 2023 09:06:01 +0000 Subject: [PATCH 3/7] Update vs code extensions setting --- .devcontainer/devcontainer.json | 9 +++++++++ .vscode/extensions.json | 7 +++++++ 2 files changed, 16 insertions(+) create mode 100644 .vscode/extensions.json diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index d14ac45..00deaf7 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -3,5 +3,14 @@ "features": { "ghcr.io/devcontainers/features/git:1": {}, "ghcr.io/shyim/devcontainers-features/bun:0": {}, + }, + "customizations": { + "vscode": { + "extensions": [ + "esbenp.prettier-vscode", + "dbaeumer.vscode-eslint", + "oven.bun-vscode" + ] + } } } diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..b2ffa5d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + "recommendations": [ + "dbaeumer.vscode-eslint", + "esbenp.prettier-vscode", + "oven.bun-vscode" + ] +} \ No newline at end of file From 65d3a5868cd2e89fbe969f96cb187457b2dc5cbc Mon Sep 17 00:00:00 2001 From: "Kim, Hyeonseo" Date: Mon, 18 Dec 2023 09:18:57 +0000 Subject: [PATCH 4/7] Add types for QueryType and SearchTarget --- src/@types/aladin-request.d.ts | 47 ++++++++++++++++++++++++++++++++++ src/aladin.ts | 11 +++++--- 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 src/@types/aladin-request.d.ts diff --git a/src/@types/aladin-request.d.ts b/src/@types/aladin-request.d.ts new file mode 100644 index 0000000..615f0fc --- /dev/null +++ b/src/@types/aladin-request.d.ts @@ -0,0 +1,47 @@ +type QueryType = + | { + name: '제목+저자' + value: 'Keyword' + } + | { + name: '제목' + value: 'Title' + } + | { + name: '저자' + value: 'Author' + } + | { + name: '출판사' + value: 'Publisher' + } + +type SearchTarget = + | { + name: '도서' + value: 'Book' + } + | { + name: '외국도서' + value: 'Foreign' + } + | { + name: '음반' + value: 'Music' + } + | { + name: 'dvd' + value: 'DVD' + } + | { + name: '중고' + value: 'Used' + } + | { + name: '전자책' + value: 'eBook' + } + | { + name: '통합검색' + value: 'All' + } diff --git a/src/aladin.ts b/src/aladin.ts index 72a4a9c..e14833a 100644 --- a/src/aladin.ts +++ b/src/aladin.ts @@ -82,10 +82,13 @@ const aladin: SlashCommand = { }, ], execute: async (_, interaction) => { - const count = interaction.options.get('개수')?.value || 5 - const query = interaction.options.get('검색어')?.value - const queryType = interaction.options.get('검색어-종류')?.value || 'Keyword' - const searchTarget = interaction.options.get('검색-대상')?.value || 'All' + const count = (interaction.options.get('개수')?.value || 5) as number + const query = interaction.options.get('검색어')?.value as string + const queryType = (interaction.options.get('검색어-종류')?.value || + 'Keyword') as QueryType['value'] + const searchTarget = (interaction.options.get('검색-대상')?.value || + 'All') as SearchTarget['value'] + const URL = `http://www.aladin.co.kr/ttb/api/ItemSearch.aspx?ttbkey=${ process.env.ALADIN_TOKEN }&Query=${encodeURI( From cc2116cca87e53b7cb45260a95356a06f0c33265 Mon Sep 17 00:00:00 2001 From: "Kim, Hyeonseo" Date: Mon, 18 Dec 2023 10:01:21 +0000 Subject: [PATCH 5/7] Add utils.ts file for generating URL query parameters --- src/utils.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/utils.ts diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..1648c25 --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,24 @@ +const queryParameters = [ + 'chkKeyTitle', + 'chkKeyAuthor', + 'chkKeyPublisher', + 'chkKeyISBN', + 'chkKeyTag', + // NOTE: 이 두 쿼리는 어디 다 쓰는 지 모르겠음. + 'chkKeyToc', + 'chkKeySubject', +] + +const queryTypeSettings = { + Keyword: ['chkKeyTitle', 'chkKeyAuthor'], + Title: ['chkKeyTitle'], + Author: ['chkKeyAuthor'], + Publisher: ['chkKeyPublisher'], +} + +export const generateUrlQueryForType = (queryType: QueryType['value']) => { + const activeParameters = queryTypeSettings[queryType] || [] + return queryParameters + .map((param) => `${param}=${activeParameters.includes(param) ? 'on' : ''}`) + .join('&') +} From 8b6e8168e04802f4fdf4b8e55de5696f6a2de6f9 Mon Sep 17 00:00:00 2001 From: "Kim, Hyeonseo" Date: Mon, 18 Dec 2023 10:04:19 +0000 Subject: [PATCH 6/7] =?UTF-8?q?=E2=9C=A8=20Apply=20Category=20selection=20?= =?UTF-8?q?in=20Embeded=20message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/aladin.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/aladin.ts b/src/aladin.ts index e14833a..b128660 100644 --- a/src/aladin.ts +++ b/src/aladin.ts @@ -1,6 +1,7 @@ import { ApplicationCommandOptionType, EmbedBuilder } from 'discord.js' import type { SlashCommand } from './@types/discord' import ky from 'ky' +import { generateUrlQueryForType } from './utils' //The option names should be all lowercased, const aladin: SlashCommand = { @@ -103,7 +104,11 @@ const aladin: SlashCommand = { `${i.title} | ${i.author}`, i.link, ]) - + console.log( + `http://www.aladin.co.kr/search/wsearchresult.aspx?KeyWord=${encodeURI( + String(query) + )}&SearchTarget=${searchTarget}&${generateUrlQueryForType(queryType)}` + ) const embed = new EmbedBuilder() .setAuthor({ name: '알라딘 도서검색', @@ -111,9 +116,9 @@ const aladin: SlashCommand = { }) .setTitle(`검색결과 : ${query}`) .setURL( - `http://www.aladin.co.kr/search/wsearchresult.aspx?KeyWord=${encodeURI( + `http://www.aladin.co.kr/search/wsearchresult.aspx?SearchWord=${encodeURI( String(query) - )}&SearchTarget=${searchTarget}` + )}&SearchTarget=${searchTarget}&${generateUrlQueryForType(queryType)}` ) .setDescription(`총 ${totalResults}건 검색`) .addFields( @@ -130,7 +135,8 @@ const aladin: SlashCommand = { await interaction.followUp({ embeds: [embed], }) - } catch { + } catch (err) { + console.error(err) const embed = new EmbedBuilder() .setAuthor({ name: '알라딘 도서검색', From 4a4c32bd896dd34e0d6fcb6ab80b8f8245df4a5f Mon Sep 17 00:00:00 2001 From: "Kim, Hyeonseo" Date: Mon, 18 Dec 2023 10:11:11 +0000 Subject: [PATCH 7/7] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Change=20description?= =?UTF-8?q?=20about=20=EA=B2=80=EC=83=89=EC=96=B4-=EC=A2=85=EB=A5=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/aladin.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/aladin.ts b/src/aladin.ts index b128660..0a2bc70 100644 --- a/src/aladin.ts +++ b/src/aladin.ts @@ -22,8 +22,7 @@ const aladin: SlashCommand = { }, { name: '검색어-종류', - description: - '검색어 종류, 현재 전체 목록 보기는 기본값인 제목+저자만 지원합니다.', + description: '검색어 종류', type: ApplicationCommandOptionType.String, required: false, choices: [