-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
136 lines (113 loc) · 3.9 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import 'dotenv/config' // Load environment variables.
import { createServer } from 'http'
import { Client, GatewayIntentBits as Intents, SlashCommandBuilder, SlashCommandStringOption, ActivityType, EmbedBuilder } from 'discord.js'
import Akinator from './Akinator.js'
if (!process.env.DISCORD_TOKEN) {
console.error('"DISCORD_TOKEN" is required to run the bot.')
process.exit()
}
if (process.env.REPL_ID) { // Repl.it requires the app to listen for incoming requests to keep running!
createServer((_, res) => res.end('Pong')).listen(process.env.PORT || '0.0.0.0')
}
const client = new Client({
intents: [Intents.Guilds]
})
// Login to Discord API
client.login(process.env.DISCORD_TOKEN)
client.on('ready', async (client) => {
console.log('Connected to Discord API!')
console.log(client.user.tag)
// Set bot activity.
client.user.setActivity({
name: '/akinator',
type: ActivityType.Watching
})
const commands = [new SlashCommandBuilder()
.setName('akinator')
.setDescription('Akinator Game! 🧞')
.addStringOption(new SlashCommandStringOption()
.setName('language')
.setDescription('Select the language you prefer. (default: English)')
.setChoices({
name: 'English',
value: 'en'
}, {
name: 'Arabic',
value: 'ar'
}, {
name: 'Spanish',
value: 'es'
}, {
name: 'French',
value: 'fr'
}, {
name: 'Italian',
value: 'it'
}, {
name: 'Japanese',
value: 'jp'
}, {
name: 'Russian',
value: 'ru'
}, {
name: 'Portuguese',
value: 'pt'
}, {
name: 'Turkish',
value: 'tr'
}, {
name: 'Chinese',
value: 'cn'
})
.setRequired(false)
)]
// Deploy Global /slash commands
await client.application.commands.set(commands)
})
client.on('interactionCreate', async (ctx) => {
if (!ctx.isCommand()) return
if (ctx.commandName !== 'akinator') return
await ctx.deferReply()
const language = ctx.options.getString('language', false) || 'en'
const game = new Akinator(language)
await game.start()
await ctx.editReply({
components: [game.component],
embeds: [game.embed]
})
// To Ignore non-playing users.
const filter = i => i.user.id === ctx.user.id && i.channelId === ctx.channelId
const channel = await client.channels.fetch(ctx.channelId, { force: false })
while (!game.ended) try {
await game.ask(channel, filter) // will throw an error if did not reply within 30 seconds
if (!game.ended) await ctx.editReply({ embeds: [game.embed], components: [game.component] })
} catch (err) {
if (err instanceof Error) console.error(err)
return await ctx.editReply({
components: [],
embeds: [],
content: 'Timeout.'
})
}
await game.stop()
const msg = await ctx.editReply({ components: [game.component], embeds: [game.embed] })
const embed = new EmbedBuilder(msg.embeds[0].toJSON())
try {
const response = await msg.awaitMessageComponent({
filter: i => ['yes', 'no'].includes(i.customId) && i.user.id === ctx.user.id,
time: 30_000
})
const title = response.customId === 'yes'
? 'Awesome! Thanks for playing'
: 'GG!'
await msg.edit({
components: [],
embeds: [embed.setTitle(title)]
})
} catch { // probably a timeout
await msg.edit({
components: [],
embeds: [embed.setTitle(null)]
}).catch(() => null)
}
})