From c1866bacefec59ef11b0398ab834d406370f3c0a Mon Sep 17 00:00:00 2001 From: Migan178 Date: Mon, 15 Jul 2024 23:17:40 +0900 Subject: [PATCH] feat: get Word --- package.json | 2 +- src/modules/wordRelay.ts | 74 +++++++++++++++++++++++++++++++++++----- 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index fa7926b..2b2cc85 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "muffinbot", - "version": "3.0.0-cake.d240713a", + "version": "3.0.0-cake.d240715a", "main": "dist/index.js", "private": true, "dependencies": { diff --git a/src/modules/wordRelay.ts b/src/modules/wordRelay.ts index 1133b99..3321cf3 100644 --- a/src/modules/wordRelay.ts +++ b/src/modules/wordRelay.ts @@ -2,17 +2,31 @@ import { container } from '@sapphire/framework' import { type Message } from 'discord.js' import { request } from 'undici' +interface APIResponse { + word: string + sense: { + target_code: number + sense_no: number + definition: string + pos: string + link: string + type: string + } +} + export class WordRelay { private _url = 'https://opendict.korean.go.kr/api/search' + private _key = container.config.api.opendict private _usedWords: string[] = [] + private _reqType = 'json' public async validWord(word: string): Promise { const res: any = await request(`${this._url}`, { query: { - key: container.config.api.opendict, - req_type: 'json', + key: this._key, + req_type: this._reqType, advanced: 'y', - type1: 'all', + type1: 'word', type3: 'general', q: word, }, @@ -46,12 +60,24 @@ export class WordRelay { collector .on('collect', async message => { - if (await this.validWord(message.content)) { - collector.stop(MAAWR_COLLECTED) - } else { + if (message.content.length < 2) { + await message.reply( + '해당 단어는 너무 짧ㅇ아요. 다시 한번 입력해주세요.', + ) + } else if (!(await this.validWord(message.content))) { await message.reply( '해당 단어는 일치하지 않아요. 다시 한ㅂ번 입력해주세요.', ) + } else if ( + !(await this.getWord( + message.content.slice(message.content.length - 1), + )) + ) { + await message.reply( + '시작단어가 한방단어면 안돼요. 다시 한번 입력ㅎ해주세요.', + ) + } else { + collector.stop(`${MAAWR_COLLECTED}: ${message.content}`) } }) .on('end', (collected, reason) => { @@ -59,14 +85,44 @@ export class WordRelay { thread.send( `<@${userID}>님, 60초동안 시작단어를 입력하지 않아 자동ㅇ으로 게임이 종료되었어요.`, ) - } else if (reason === MAAWR_COLLECTED) { + } else if (reason.startsWith(MAAWR_COLLECTED)) { + this._usedWords.push( + reason.slice(`${MAAWR_COLLECTED}: `.length), + ) } }) }) } catch (err) { - console.log(err) + console.error(err) } } - public getWord() {} + private _getrandomWord(wordList: APIResponse[] | null): APIResponse | null { + if (!wordList) return null + + const wordInfo = wordList[Math.floor(Math.random() * wordList.length)] + + if (this._usedWords.includes(wordInfo.word)) + return this._getrandomWord(wordList) + else return wordInfo + } + + public async getWord(lastWord: string): Promise { + const res: any = await request(this._url, { + query: { + key: this._key, + req_type: this._reqType, + advanced: 'y', + type1: 'word', + type3: 'general', + method: 'start', + q: lastWord, + letter_s: 2, + num: 100, + pos: 1, + }, + }).then(res => res.body.json()) + + return this._getrandomWord(res.channel.item) + } }