-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
198 lines (185 loc) · 7.1 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
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
// Require the necessary discord.js classes
const fs = require('fs');
const { Client, Collection, Intents } = require('discord.js');
const keepAlive = require('./server');
const path = require('path');
const talkedRecently = new Set();
// const moment = require('moment');
// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.commands = new Collection();
const appCommandFiles = fs.readdirSync('./commands/application').filter(file => file.endsWith('.js'));
const guildCommandFiles = fs.readdirSync('./commands/guild').filter(file => file.endsWith('.js'));
for (const file of appCommandFiles) {
const command = require(`./commands/application/${file}`);
client.commands.set(command.data.name, command);
}
for (const file of guildCommandFiles) {
const command = require(`./commands/guild/${file}`);
client.commands.set(command.data.name, command);
}
let json = null
let characters = []
client.once('ready', () => {
json = fs.readFileSync("./assets/framedata.json", 'utf8');
json = JSON.parse(json);
Object.keys(json).forEach(function (key) {
characters.push(key);
})
console.log('Ready!');
});
client.on('interactionCreate', async autocomplete => {
if (!autocomplete.isAutocomplete()) return;
// console.log(autocomplete.commandName)
if (autocomplete.commandName === 'embed' || autocomplete.commandName === 'frames') {
let currentOption = autocomplete.options.getFocused(true);
let currentName = currentOption.name;
let currentValue = currentOption.value;
const options = [];
if (currentName === "character") {
characters.forEach((character) => {
if (character.toLowerCase().includes(currentValue.toLowerCase())) {
let charObj = {}
charObj["name"] = character;
charObj["value"] = character;
if (options.length < 25) {
options.push(charObj);
}
}
})
}
const character = autocomplete.options.getString('character')
// If move is focused.
if (currentName === "move" && character !== "") {
// currentValue = autocomplete.options.getFocused()
let moveObj = {}
if (json[character] === undefined) {
moveObj["name"] = 'Moves not found for specified character, try another character';
moveObj["value"] = 'Moves not found for specified character, try another character';
options.push(moveObj);
} else {
// Load move inputs.
let moves = []
Object.keys(json[character]).forEach(function (key) {
let name = ''
// Append move name if present.
if (json[character][key]['NAME'] !== undefined && json[character][key]['NAME'] !== null) {
name = ' - ' + json[character][key]['NAME'];
}
moves.push(key + name);
})
// Loop through moves and update when user types.
moves.forEach((move, index) => {
if (move.toLowerCase().includes(currentValue.toLowerCase())) {
moveObj = {}
// Trim move to only have input for object value.
let input = move
if (move.toLowerCase().includes('-')) {
input = move.split("-")
}
moveObj["name"] = move;
moveObj["value"] = (Array.isArray(input)) ? input[0].trim() : input;
if (options.length < 25) {
options.push(moveObj);
}
}
})
}
}
await autocomplete.respond(options);
}
// Calculator autocomplete.
if (autocomplete.commandName === 'calc' || autocomplete.commandName === 'punish') {
let currentOption = autocomplete.options.getFocused(true);
let currentName = currentOption.name;
let currentValue = currentOption.value;
const options = [];
if (currentName === "attacking_character" || currentName === "defending_character") {
characters.forEach((character) => {
if (character.toLowerCase().includes(currentValue.toLowerCase())) {
let charObj = {}
charObj["name"] = character;
charObj["value"] = character;
if (options.length < 25) {
options.push(charObj);
}
}
})
}
const character = autocomplete.options.getString('attacking_character')
// If move is focused.
if (currentName === "attacking_move" && character !== "") {
// currentValue = autocomplete.options.getFocused()
let moveObj = {}
if (json[character] === undefined) {
moveObj["name"] = 'Moves not found for specified character, try another character';
moveObj["value"] = 'Moves not found for specified character, try another character';
options.push(moveObj);
} else {
// Load move inputs.
let moves = []
Object.keys(json[character]).forEach(function (key) {
let name = ''
// Append move name if present.
if (json[character][key]['NAME'] !== undefined && json[character][key]['NAME'] !== null) {
name = ' - ' + json[character][key]['NAME'];
}
moves.push(key + name);
})
// Loop through moves and update when user types.
moves.forEach((move, index) => {
if (move.toLowerCase().includes(currentValue.toLowerCase())) {
moveObj = {}
// Trim move to only have input for object value.
let input = move
if (move.toLowerCase().includes('-')) {
input = move.split("-")
}
moveObj["name"] = move;
moveObj["value"] = (Array.isArray(input)) ? input[0].trim() : input;
if (options.length < 25) {
options.push(moveObj);
}
}
})
}
}
await autocomplete.respond(options);
}
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.on("ready", () => {
// if (client.shard.id == 0)
// console.log(`-- ${moment().utc().format('MMMM Do')}, ${moment().utc().format('hh:mm a')} --`);
// console.log(`Shard ${client.shard.id} ready!`);
console.log(`Hi, ${client.user.username} is now online and used in ${client.guilds.cache.size} servers.`);
client.user.setPresence({
status: "online",
activities: [{
name: 'YOLO? Use /frames or /help to get started. Now available on Android as "Frame of fighters"!'
}],
});
});
client.on("error", (e) => console.error(e));
client.on("warn", (e) => console.warn(e));
client.on("debug", (e) => console.info(e));
client.on('rateLimit', (info) => {
console.log(`Rate limit hit ${info.timeDifference ? info.timeDifference : info.timeout ? info.timeout: 'Unknown timeout '}`)
})
// Keep bot alive.
keepAlive();
// setInterval(keepAlive, 1000 * 60 * 60);
// setInterval(keepAlive, 5000);
// Login to Discord with your client's token.
const token = process.env['DISCORD_TOKEN']
client.login(token);