forked from ZandBraxton/dustbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
244 lines (217 loc) · 7.16 KB
/
bot.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
require("dotenv").config();
const fs = require("node:fs");
const path = require("node:path");
const { games } = require("./data/games.json");
const { Client, Collection, Intents } = require("discord.js");
const { getCharacters, getMoveset } = require("./database/queries");
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
"GUILDS",
],
});
client.on("ready", () => {
console.log("ready!");
});
client.commands = new Collection();
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection
// With the key as the command name and the value as the exported module
client.commands.set(command.data.name, command);
}
client.on("interactionCreate", async (interaction) => {
if (interaction.isAutocomplete()) {
if (
interaction.commandName === "character" ||
interaction.commandName === "game-roster" ||
interaction.commandName === "move"
) {
const focusedOption = interaction.options.getFocused(true);
let choices = [];
let result;
if (focusedOption.name === "game") {
for (let i = 0; i < games.length; i++) {
choices.push({ name: games[i].title, value: games[i].path });
}
}
//pulls character data and displays it as options
if (focusedOption.name === "character-name") {
let characters;
try {
const result = await getCharacters(
interaction.options._hoistedOptions[0].value
);
if (focusedOption.value !== "") {
const characterFilter = result.filter((character) =>
character.name
.toLocaleLowerCase()
.match(focusedOption.value.toLocaleLowerCase())
);
characters = characterFilter;
} else {
characters = result;
}
if (characters.length >= 25) {
for (let i = 0; i < 24; i++) {
choices.push(characters[i]);
}
choices.push({ name: "Type to view more", path: "" });
} else {
characters.map((ch) => choices.push(ch));
}
return await interaction.respond(
choices.map((character) => ({
name: character.name,
value: character.name,
}))
);
} catch (error) {
console.log(error);
}
}
if (focusedOption.name === "move-type") {
try {
result = await getMoveset(
interaction.options._hoistedOptions[0].value,
interaction.options._hoistedOptions[1].value
);
if (focusedOption.value !== "") {
const filter = result.filter((element) =>
element.moveType
.toLocaleLowerCase()
.match(focusedOption.value.toLocaleLowerCase())
);
return await interaction.respond(
filter.map((element) => ({
name: element.moveType,
value: element.moveType,
}))
);
}
result.map((el) => choices.push(el.moveType));
return await interaction.respond(
choices.map((element) => ({
name: element,
value: element,
}))
);
} catch (error) {
console.log(error);
}
}
if (focusedOption.name === "move") {
try {
const result = await getMoveset(
interaction.options._hoistedOptions[0].value,
interaction.options._hoistedOptions[1].value
);
const moveSetResult = result.find(
(list) =>
list.moveType === interaction.options._hoistedOptions[2].value
);
let moveSet;
if (focusedOption.value !== "") {
const filter = moveSetResult.moveList.filter((move) =>
move.name
? move.name
.toLocaleLowerCase()
.match(focusedOption.value.toLocaleLowerCase())
: move.input
.toLocaleLowerCase()
.match(focusedOption.value.toLocaleLowerCase())
);
moveSet = filter;
} else {
moveSet = moveSetResult.moveList;
}
if (moveSet.length >= 25) {
for (let i = 0; i < 24; i++) {
if (moveSet[i].name && moveSet[i].input) {
if (moveSet[i].name === moveSet[i].input) {
choices.push(moveSet[i].input);
} else {
choices.push(`${moveSet[i].name} [${moveSet[i].input}]`);
}
} else if (!moveSet[i].name && moveSet[i].input) {
choices.push(moveSet[i].input);
} else if (moveSet[i].name && !moveSet[i].input) {
choices.push(moveSet[i].name);
} else {
choices.push("N/A");
}
}
choices.push("Type to view more");
} else {
moveSet.map((move) => {
if (move.name && move.input) {
if (move.name === move.input) {
choices.push(move.input);
} else {
choices.push(`${move.name} [${move.input}]`);
}
} else if (!move.name && move.input) {
choices.push(move.input);
} else if (move.name && !move.input) {
choices.push(move.name);
} else {
choices.push("N/A");
}
});
}
return await interaction.respond(
choices.map((move) => ({ name: move, value: move }))
);
} catch (error) {
console.log(error);
}
}
try {
const filtered = choices.filter((choice) =>
choice.name
.toLocaleLowerCase()
.match(focusedOption.value.toLocaleLowerCase())
);
await interaction.respond(
filtered.map((choice) => ({ name: choice.name, value: choice.value }))
);
} catch (error) {
console.log(error);
}
}
}
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
const channel = interaction.member.guild.channels.cache.get(
interaction.channelId
);
if (channel.type !== "GUILD_VOICE") {
await command.execute(interaction, client);
} else {
await interaction.reply({
content: "Dustbot commands cannot be called in voice channels.",
ephemeral: true,
});
}
} catch (error) {
console.error(error);
try {
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
} catch (error) {
console.log(error);
}
}
});
client.login(process.env.BOT_TOKEN);