-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
221 lines (169 loc) · 7.51 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v10');
const { Client, GatewayIntentBits, Partials, Collection } = require('discord.js');
const R6 = require('r6s-stats-api');
const embeds = require('./src/embeds/embeds.js');
require('dotenv').config();
const config = require('./config.json');
const client = new Client({
intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildBans,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
partials: [Partials.Channel],
});
const TOKEN = process.env.TOKEN;
client.login(TOKEN);
client.on('ready', () => {
console.log(`-- Logged in as ${client.user.tag} --`);
console.log(`text command is ready`);
client.user.setActivity(`${config.PREFIX}r6 help`);
if (config.SLASH_COMMANDS) {
const CLIENT_ID = client.user.id;
const GUILD_ID = config.GUILD_ID;
// Loading commands from the commands folder
const commands = [];
const commandFiles = fs.readdirSync('./src/slash_commands').filter(file => file.endsWith('.js'));
// Creating a collection for commands in client
client.commands = new Collection();
for (const file of commandFiles) {
const command = require(`./src/slash_commands/${file}`);
commands.push(command.data.toJSON());
client.commands.set(command.data.name, command);
}
const rest = new REST({ version: '10' })
.setToken(TOKEN);
(async () => {
try {
if (config.LOAD_SLASH_GLOBAL) {
await rest.put(
Routes.applicationCommands(CLIENT_ID),
{ body: commands },
);
console.log('Successfully registered application commands globally');
}
else {
await rest.put(
Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID),
{ body: commands },
);
console.log('Successfully registered application commands for development guild');
}
} catch (error) {
if (error) console.error(error);
}
})();
console.log(`slash command is ready`);
}
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction, client);
} catch (error) {
if (error) console.error(error);
await interaction.reply({ content: 'There was an error while executing this command', ephemeral: true });
}
});
client.on("messageCreate", async message => {
if (message.author.bot) return; // if the message sender is bot
if (message.content.indexOf(config.PREFIX) !== 0) return; // if the config.PREFIX not in message.content[0]
let args = message.content.slice(config.PREFIX.length).trim().split(/ +/g);
let first_element = args.shift();
if (first_element.toUpperCase() === "R6") {
message.channel.sendTyping();
if (!args[0])
return message.channel.send({ embeds: [embeds.Help()] });
let option = args[0].toUpperCase();
if (option === "HELP") { // +R6 help
console.log("HELP");
return message.channel.send({ embeds: [embeds.Help()] });
}
if (option !== "PC" && option !== "XBOX" && option !== "PSN" && option !== "PS4" && option !== "PS5") {
if (config.ENABLE_DEFAULT_PLATFORM) { // enble default platform
args.splice(0, 0, config.DEFAULT_PLATFORM);
}
else {
console.log("PLATFORM_ERROR"); // if enter platform not pc/xbox/osn
return message.channel.send({ embeds: [embeds.Help_platform()] });
}
}
let input_platform = args[0];
let input_name = args[1];
let input_gamemode = args[2];
let input_operator = args[3];
//console.log("args:", args);
//console.log("input_platform:", input_platform);
//console.log("input_name:", input_name);
//console.log("input_gamemode:", input_gamemode);
//console.log("input_operator:", input_operator);
if (!input_gamemode) {
console.log("GENERAL");
let profile = await R6.general(input_platform, input_name);
//console.log(profile);
if (profile.header)
return message.channel.send({ embeds: [embeds.General(profile)] });
else
return message.channel.send({ embeds: [embeds.Help_Not_Found()] });
}
if (input_gamemode.toUpperCase() === "CASUAL") {
console.log("CASUAL");
let profile = await R6.casual(input_platform, input_name);
//console.log(profile);
if (profile.header)
return message.channel.send({ embeds: [embeds.Casual(profile)] });
else
return message.channel.send({ embeds: [embeds.Help_Not_Found()] });
}
else if (input_gamemode.toUpperCase() === "RANK") {
console.log("RANK");
let profile = await R6.rank(input_platform, input_name);
//console.log(profile);
if (profile.header)
return message.channel.send({ embeds: [embeds.Rank(profile)] });
else
return message.channel.send({ embeds: [embeds.Help_Not_Found()] });
}
/*
else if (input_gamemode.toUpperCase() === "UNRANK") {
console.log("UNRANK");
let profile = await R6.unrank(input_platform, input_name);
//console.log(profile);
if (profile.header)
return message.channel.send({ embeds: [embeds.Unrank(profile)] });
else
return message.channel.send({ embeds: [embeds.Help_Not_Found()] });
}
*/
else if (input_gamemode.toUpperCase() === "DEATHMATCH") {
console.log("DEATHMATCH");
let profile = await R6.deathmatch(input_platform, input_name);
//console.log(profile);
if (profile.header)
return message.channel.send({ embeds: [embeds.Deathmatch(profile)] });
else
return message.channel.send({ embeds: [embeds.Help_Not_Found()] });
}
else if (input_gamemode.toUpperCase() === "OPERATOR") {
console.log("OPERATOR");
if (!input_operator)
return message.channel.send({ embeds: [embeds.Help_operator()] });
let profile = await R6.operator(input_platform, input_name, input_operator);
//console.log(profile);
if (profile.header)
return message.channel.send({ embeds: [embeds.Operator(profile)] });
else if (profile === "OPERATOR_ERROR")
return message.channel.send({ embeds: [embeds.Help_operator(profile)] });
else
return message.channel.send({ embeds: [embeds.Help_Not_Found()] });
}
else
return message.channel.send({ embeds: [embeds.Help()] });
}
})