-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathAkinator.js
105 lines (85 loc) · 2.72 KB
/
Akinator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } from 'discord.js'
import { Aki } from 'aki-api'
const emojis = ['👍', '👎', '❔', '🤔', '🙄', '❌']
class Akinator {
constructor(region = 'en') {
this.api = new Aki({ region })
}
get answers() {
return this.api.answers
}
get question() {
return this.api.question
}
get score() {
return this.api.currentStep
}
get ended() {
return this.api.progress >= 70 || this.api.currentStep >= 78
}
start() {
return this.api.start()
}
stop() {
return this.api.win()
}
/**
*
* @param {import("discord.js").TextChannel} channel
*/
ask(channel, filter) {
return new Promise((resolve, reject) => {
const collector = channel.createMessageComponentCollector({ filter, time: 30_000 })
collector
.on('collect', async (ctx) => {
await ctx.deferUpdate()
const answer = Number(ctx.customId)
await this.api.step(answer)
collector.stop()
})
.on('end', (_, reason) => {
if (reason === 'time') {
reject()
} else {
resolve()
}
})
})
}
get embed() {
if (this.ended) {
const someone = this.answers[0]
return new EmbedBuilder()
.setTitle('Is this your character?')
.setDescription(`**${someone.name}**\n${someone.description}\nRanking as **#${someone.ranking}**`)
.setImage(someone.absolute_picture_path)
.setColor('Random')
}
return new EmbedBuilder()
.setTitle(`${this.score + 1}. ${this.question}`)
.setColor('Random')
.setFooter({ text: 'You have 30 seconds to answer.' })
}
get component() {
const row = new ActionRowBuilder()
if (this.ended) row.addComponents(
new ButtonBuilder()
.setLabel('Yes')
.setStyle(ButtonStyle.Primary)
.setCustomId('yes'),
new ButtonBuilder()
.setLabel('No')
.setStyle(ButtonStyle.Danger)
.setCustomId('no')
)
else row.addComponents(this.answers.map((answer, index) => {
return new ButtonBuilder()
.setEmoji(emojis[index])
.setLabel(answer)
.setStyle(ButtonStyle.Primary)
.setCustomId(index.toString())
}))
return row
}
}
export default Akinator