feat: get Word

This commit is contained in:
Siwoo Jeon 2024-07-15 23:17:40 +09:00
parent 84e1b93496
commit c1866bacef
Signed by: migan
GPG key ID: C4151385FFD2082A
2 changed files with 66 additions and 10 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "muffinbot", "name": "muffinbot",
"version": "3.0.0-cake.d240713a", "version": "3.0.0-cake.d240715a",
"main": "dist/index.js", "main": "dist/index.js",
"private": true, "private": true,
"dependencies": { "dependencies": {

View file

@ -2,17 +2,31 @@ import { container } from '@sapphire/framework'
import { type Message } from 'discord.js' import { type Message } from 'discord.js'
import { request } from 'undici' 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 { export class WordRelay {
private _url = 'https://opendict.korean.go.kr/api/search' private _url = 'https://opendict.korean.go.kr/api/search'
private _key = container.config.api.opendict
private _usedWords: string[] = [] private _usedWords: string[] = []
private _reqType = 'json'
public async validWord(word: string): Promise<boolean> { public async validWord(word: string): Promise<boolean> {
const res: any = await request(`${this._url}`, { const res: any = await request(`${this._url}`, {
query: { query: {
key: container.config.api.opendict, key: this._key,
req_type: 'json', req_type: this._reqType,
advanced: 'y', advanced: 'y',
type1: 'all', type1: 'word',
type3: 'general', type3: 'general',
q: word, q: word,
}, },
@ -46,12 +60,24 @@ export class WordRelay {
collector collector
.on('collect', async message => { .on('collect', async message => {
if (await this.validWord(message.content)) { if (message.content.length < 2) {
collector.stop(MAAWR_COLLECTED) await message.reply(
} else { '해당 단어는 너무 짧ㅇ아요. 다시 한번 입력해주세요.',
)
} else if (!(await this.validWord(message.content))) {
await message.reply( 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) => { .on('end', (collected, reason) => {
@ -59,14 +85,44 @@ export class WordRelay {
thread.send( thread.send(
`<@${userID}>님, 60초동안 시작단어를 입력하지 않아 자동ㅇ으로 게임이 종료되었어요.`, `<@${userID}>님, 60초동안 시작단어를 입력하지 않아 자동ㅇ으로 게임이 종료되었어요.`,
) )
} else if (reason === MAAWR_COLLECTED) { } else if (reason.startsWith(MAAWR_COLLECTED)) {
this._usedWords.push(
reason.slice(`${MAAWR_COLLECTED}: `.length),
)
} }
}) })
}) })
} catch (err) { } 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<APIResponse | null> {
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)
}
} }