Skip to content

Commit

Permalink
πŸ”€πŸš€ Merge pull request #7 from dodok8/dodok8/issue3
Browse files Browse the repository at this point in the history
dodok8/issue3
  • Loading branch information
dodok8 committed Dec 18, 2023
2 parents ec0db1d + 4a4c32b commit 681af56
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 21 deletions.
9 changes: 9 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
}
}
7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"recommendations": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"oven.bun-vscode"
]
}
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -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: [],
Expand Down
47 changes: 47 additions & 0 deletions src/@types/aladin-request.d.ts
Original file line number Diff line number Diff line change
@@ -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'
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type ItemSearchResult = {
type ItemSearchResponse = {
version: string
logo: string
title: string
Expand Down
41 changes: 26 additions & 15 deletions src/commands/itemSearch.ts β†’ src/aladin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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'
import { generateUrlQueryForType } from './utils'

//The option names should be all lowercased,
export const itemSearch: SlashCommand = {
const aladin: SlashCommand = {
name: 'aladin',
description: 'μƒν’ˆ 검색',
options: [
Expand All @@ -19,11 +20,9 @@ export const itemSearch: SlashCommand = {
description: 'λͺ‡ 건의 검색을 볼지 μ •ν•©λ‹ˆλ‹€.',
type: ApplicationCommandOptionType.Integer,
},
// TODO: 이 λΆ€λΆ„ 고치기, μ‘λ‹΅μ˜ link둜 μ ‘κ·Όν•˜λ©΄ κΈ€μžκ°€ κΉ¨μ Έμ„œ λ“€μ–΄κ°€λŠ” 증상이 μžˆλ‹€.
{
name: '검색어-μ’…λ₯˜',
description:
'검색어 μ’…λ₯˜, ν˜„μž¬ 전체 λͺ©λ‘ λ³΄κΈ°λŠ” 기본값인 제λͺ©+μ €μžλ§Œ μ§€μ›ν•©λ‹ˆλ‹€.',
description: '검색어 μ’…λ₯˜',
type: ApplicationCommandOptionType.String,
required: false,
choices: [
Expand Down Expand Up @@ -83,35 +82,42 @@ export const itemSearch: 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(
String(query)
)}&QueryType=${queryType}&MaxResults=${count}&start=1&SearchTarget=${searchTarget}&output=js&Version=20131101`

try {
const data = await ky.get(URL).json<ItemSearchResult>()
const data = await ky.get(URL).json<ItemSearchResponse>()

const { item, totalResults } = data
const bookInfos = item.map((i: any): [string, string] => [
`${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: 'μ•ŒλΌλ”˜ λ„μ„œκ²€μƒ‰',
iconURL: 'https://image.aladin.co.kr/img/m/2018/shopping_app1.png',
})
.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(
Expand All @@ -128,7 +134,8 @@ export const itemSearch: SlashCommand = {
await interaction.followUp({
embeds: [embed],
})
} catch {
} catch (err) {
console.error(err)
const embed = new EmbedBuilder()
.setAuthor({
name: 'μ•ŒλΌλ”˜ λ„μ„œκ²€μƒ‰',
Expand All @@ -146,3 +153,7 @@ export const itemSearch: SlashCommand = {
}

// https://embed.dan.onl/

const availableCommands = [aladin]

export default availableCommands
4 changes: 0 additions & 4 deletions src/commands/index.ts

This file was deleted.

24 changes: 24 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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('&')
}

0 comments on commit 681af56

Please sign in to comment.