feat: get Word
This commit is contained in:
parent
84e1b93496
commit
c1866bacef
2 changed files with 66 additions and 10 deletions
|
@ -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": {
|
||||
|
|
|
@ -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<boolean> {
|
||||
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<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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue