/
index.js
562 lines (482 loc) · 16.2 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
const {
Client,
Collection,
GatewayIntentBits,
EmbedBuilder,
ActivityType,
ButtonBuilder,
ButtonStyle,
ActionRowBuilder,
ChannelType,
ModalBuilder,
TextInputBuilder,
TextInputStyle,
userMention
} = require("discord.js");
const fs = require("fs");
const path = require("path");
const TwitchApi = require("node-twitch").default;
require("dotenv").config();
const logger = require("./Logger/logger.js");
const axios = require("axios").default;
const CSGO = require('./Utils/cs-stuff.js');
const Valo = require('./Utils/valo-stuff.js');
const HenrikDevValorantAPI = require("unofficial-valorant-api");
////////////////////
/// DATABASE ///
////////////////////
const Sequelize = require('sequelize');
const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USERNAME, process.env.DB_PASSWORD, {
host: process.env.DB_IP,
dialect: 'mysql',
logging: false
});
const giveawayEntries = require("./Models/giveawayEntries")(sequelize, Sequelize.DataTypes);
const giveawayData = require("./Models/giveawayData")(sequelize, Sequelize.DataTypes);
const csgoRanks = require("./Models/csgoRanks")(sequelize, Sequelize.DataTypes);
////////////////////
/// DEFINE ///
////////////////////
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent,
],
partials: ["MESSAGE", "CHANNEL", "REACTION"],
});
const twitch = new TwitchApi({
client_id: process.env.TWITCH_ID,
client_secret: process.env.TWITCH_SECRET,
});
const vapi = new HenrikDevValorantAPI(process.env.VALO_KEY);
// const dataDirectory = path.join(__dirname, "Data");
// const weapons = JSON.parse(
// fs.readFileSync(path.join(dataDirectory, "weapons.json"))
// );
let isLive = false;
////////////////////
/// ADD COMMANDS ///
////////////////////
let files = fs.readdirSync("./"),
file;
for (file of files) {
if (file.startsWith("autoAdd")) {
require("./" + file);
}
}
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);
client.commands.set(command.data.name, command);
}
////////////////////
//// BOT EVENTS ////
////////////////////
client.on("ready", async () => {
logger.info(
`Username: ${client.user.tag} Servers: ${client.guilds.cache.size}`
);
client.user.setPresence({
activities: [
{
name: `in ${client.guilds.cache.size} servers`,
type: ActivityType.Streaming,
url: "https://twitch.tv/tansmh",
},
],
status: `dnd`,
});
let serverlist = "";
client.guilds.cache.forEach((guild) => {
serverlist = serverlist.concat(
"- " + guild.name + ": ID: " + guild.id + "\n"
);
});
const embed = new EmbedBuilder()
.setColor("Random")
.setTitle("Servers that have Y-Gaming Discord Bot", "")
.setDescription(serverlist);
client.users.fetch("132784173311197184").then((user) => {
try {
user.send({ embeds: [embed] });
} catch (err) {
console.log("err");
}
});
await dbInit();
// setInterval(() => {
// checkLive("tansmh");
// }, 10000);
});
client.on("interactionCreate", async (interaction) => {
if (interaction.isChatInputCommand() || interaction.isContextMenuCommand()) {
const command = interaction.client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction, client);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.editReply({
content: "There was an error while executing this command!",
});
} else await interaction.reply({
content: "There was an error while executing this command!",
});
}
} else if (interaction.isButton()) {
if (interaction.customId.startsWith("giveaway_")) {
await interaction.deferReply({ ephemeral: true });
const messageId = interaction.customId.split("_")[1];
const hasEntered = await giveawayEntries.findOne({ where: { discord_id: interaction.user.id, message_id: messageId } });
if (hasEntered) {
return await interaction.editReply({ content: `You have already entered this giveaway.`, ephemeral: true });
}
await giveawayEntries.create({
message_id: messageId,
channel_id: interaction.channel.id,
server_id: interaction.guild.id,
discord_id: interaction.user.id,
entries: 1,
});
const entries = await giveawayEntries.findAndCountAll({ where: { message_id: messageId } });
const giveawayButton = new ButtonBuilder()
.setCustomId("giveaway_" + messageId)
.setLabel("Join")
.setStyle(ButtonStyle.Success)
.setEmoji("🎉");
const disableButton = new ButtonBuilder()
.setCustomId("disabledGiveaway")
.setLabel((entries.count).toString())
.setStyle(ButtonStyle.Secondary)
.setDisabled(true)
.setEmoji("👤");
const row = new ActionRowBuilder().addComponents(giveawayButton, disableButton);
interaction.message.edit({ components: [row] });
await interaction.editReply({
content: "You have successfully entered the giveaway!",
ephemeral: true,
});
} else if (interaction.customId.startsWith("retry_")) {
await interaction.update({ content: 'Trying to fetch your store, please wait...', components: [] });
const data = interaction.customId.split('_', 3);
const userCreds = {
username: data[1],
password: data[2]
}
const retryButton = new ButtonBuilder()
.setLabel('Retry')
.setCustomId('retry_' + userCreds.username + '_' + userCreds.password)
.setStyle(ButtonStyle.Primary)
.setEmoji('🔁');
const row = new ActionRowBuilder().addComponents(retryButton);
const build = await Valo.getVersion();
const login = await Valo.authorize(build, userCreds.username, userCreds.password);
if (login.error) {
await interaction.message.edit({ content: 'Invalid login attempt. If you are sure your credentials were correct then please check if 2FA is enabled because the bot doesn\'t support 2FA as of yet. If you did everything correctly, then maybe the bot is malfunctioning.', components: [row] });
return;
}
login.build = build;
const playerStore = await Valo.getStoreFront(login);
let store;
await vapi
.getFeaturedItems({
version: "v2",
})
.then((response) => {
store = response;
})
.catch((error) => {
logger.error(error);
});
const embeds = [];
for (const bundle of store.data) {
const bundleUUID = bundle.bundle_uuid;
const bundleData = await axios.get(
`https://valorant-api.com/v1/bundles/${bundleUUID}`
);
const embed = new EmbedBuilder()
.setTitle(bundleData.data.data.displayName)
.setColor("FA4454")
.setImage(bundleData.data.data.displayIcon);
if (
bundleData.data.data.promoDescription !=
bundleData.data.data.extraDescription
) {
embed.setDescription(
bundleData.data.data.extraDescription +
"\n\n" +
bundleData.data.data.promoDescription
);
} else if (bundleData.data.data.extraDescription) {
embed.setDescription(bundleData.data.data.extraDescription);
}
embeds.push(embed);
}
if (!playerStore) {
return await interaction.message.edit({
content:
"Invalid login attempt. If you are sure your credentials were correct then please check if 2FA is enabled because the bot doesn't support 2FA as of yet.",
components: [row],
});
}
const skins = await fetchStoreSkins(playerStore);
for (const skin of skins) {
const skinEmbed = new EmbedBuilder()
.setColor("#2B2D31")
.setTitle(skin.name)
.setThumbnail(skin.icon)
.setDescription("<:VP:1231857629740138539> " + skin.cost);
embeds.push(skinEmbed);
}
await interaction.message.delete().catch(console.error);
await interaction.channel.send({ content: userMention(interaction.user.id), embeds: embeds });
} else if (interaction.customId === "toproll") {
const button = new ButtonBuilder()
.setCustomId('toprolled')
.setLabel('Roll')
.setStyle(ButtonStyle.Primary);
const row = new ActionRowBuilder().addComponents(button);
await interaction.reply({ content: 'Press the button below to start rolling!', components: [row], ephemeral: true });
} else if (interaction.customId === "toprolled") {
const randomNumber = Math.floor(Math.random() * 10) + 1;;
interaction.update({ content: randomNumber.toString() });
}
} else if (interaction.isAutocomplete()) {
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
logger.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.autocomplete(interaction);
} catch (error) {
console.error(error);
await interaction.editReply({
content: "There was an error while executing this command!",
});
}
}
});
client.on("messageCreate", async (message) => {
if (message.author.bot) return;
const msg = message.content;
if (!msg.includes("STEAM_")) return;
await message.channel.sendTyping();
try {
const embed = await CSGO.getStatusEmbed(message);
if (!embed.data.fields[0]) {
logger.error('Could not fetch rank status embed.');
return;
}
message.channel.send({ embeds: [embed] });
} catch(error) {
logger.error('Could not send rank status embed.');
console.log(error);
}
});
client.login(process.env.DISCORD_TOKEN);
async function checkLive(channel) {
const stream = await getChannel(channel);
if (stream && !isLive) {
// Stream went LIVE //
isLive = true;
const user = await getUser(stream.user_login);
if (!user) return console.log("User not found - " + stream.user_login);
const game = await getGame(parseInt(stream.game_id));
if (!game) return console.log("Game not found - " + stream.game_name);
await sendLiveEmbed(
stream,
user,
game,
"992356452368920607",
"770854206046470164"
);
} else if (!stream && isLive) {
// Stream went OFFLINE //
isLive = false;
const user = await getUser(channel);
if (!user) return console.log("User not found - " + channel);
const video = await getVideo(user.id);
if (!video) return console.log("Video not found - " + user.id);
await sendOfflineEmbed(user, video, "992356452368920607");
return;
} else if (stream && isLive) {
// Stream LIVE update //
return;
} else {
// Stream OFFLINE update //
return;
}
}
async function getChannel(channel) {
const stream = await twitch.getStreams({ channel: channel });
if (stream.data.length > 0) {
const stream_data = {
user_id: stream.data[0].user_id,
user_login: stream.data[0].user_login,
user_name: stream.data[0].user_name,
game_id: stream.data[0].game_id,
game_name: stream.data[0].game_name,
title: stream.data[0].title,
viewer_count: stream.data[0].viewer_count,
started_at: stream.data[0].started_at,
thumbnail_url: stream.data[0].thumbnail_url
.replace("{width}", "1280")
.replace("{height}", "720"),
tags: stream.data[0].tags,
};
return stream_data;
} else return false;
}
async function getUser(user_login) {
const user = await twitch.getUsers(user_login);
if (user.data.length > 0) {
const user_data = {
id: user.data[0].id,
broadcaster_type: user.data[0].broadcaster_type,
description: user.data[0].description,
profile_image_url: user.data[0].profile_image_url,
offline_image_url: user.data[0].offline_image_url,
view_count: user.data[0].view_count,
created_at: user.data[0].created_at,
};
return user_data;
} else return false;
}
async function getGame(game_id) {
const game = await twitch.getGames(game_id);
if (game.data.length > 0) {
const game_data = {
box_art_url: game.data[0].box_art_url
.replace("{width}", "300")
.replace("{height}", "300"),
igdb_id: game.data[0].igdb_id,
};
return game_data;
} else return false;
}
async function getVideo(user_id) {
const video = await twitch.getVideos({ user_id: user_id });
if (video.data.length > 0) {
const video_data = {
id: video.data[0].id,
url: video.data[0].url,
thumbnail_url: video.data[0].thumbnail_url
.replace("%{width}", "1280")
.replace("%{height}", "720"),
};
return video_data;
} else return false;
}
async function sendLiveEmbed(
stream,
user,
game,
channelId,
roleId = undefined
) {
const channel = client.channels.cache.get(channelId);
const tags = "`" + stream.tags.join("` `") + "`";
let message;
roleId
? (message = `<@&${roleId}> ${stream.user_name} is **Live**!`)
: (message = `${stream.user_name} is **Live**!`);
const button = new ButtonBuilder()
.setLabel("Watch Stream")
.setEmoji("<a:TanWiggle:961250315989049414>")
.setStyle(ButtonStyle.Link)
.setURL("https://twitch.tv/" + stream.user_name);
const row = new ActionRowBuilder().addComponents(button);
const embed = new EmbedBuilder()
.setColor("#6441A4")
.setAuthor({
name: stream.user_name,
iconURL: user.profile_image_url,
url: "https://twitch.tv/" + stream.user_name,
})
.setThumbnail(game.box_art_url)
.setTitle(stream.title)
.setURL("https://twitch.tv/" + stream.user_name)
.setDescription("**Game**\n" + stream.game_name + "\n\n" + tags)
.setImage(
stream.thumbnail_url.replace("{width}", "1280").replace("{height}", "720")
)
.setTimestamp()
.setFooter({
text: "Started streaming",
iconURL:
"https://www.tailorbrands.com/wp-content/uploads/2021/04/twitch-logo.png",
});
channel.send({
content: message,
embeds: [embed],
components: [row],
});
}
async function sendOfflineEmbed(user, video, channelId) {
const channel = client.channels.cache.get(channelId);
const messages = await channel.messages.fetch({ limit: 1 });
const lastMessage = messages.first();
if (lastMessage.author.id === client.user.id) {
const lastEmbed = lastMessage.embeds[0];
const embed = new EmbedBuilder()
.setColor(lastEmbed.color)
.setAuthor(lastEmbed.author)
.setThumbnail(lastEmbed.thumbnail.url)
.setTitle(lastEmbed.title)
.setURL(lastEmbed.url)
.setDescription(lastEmbed.description)
.setImage(user.offline_image_url)
.setTimestamp()
.setFooter({
text: "Last live",
iconURL:
"https://www.tailorbrands.com/wp-content/uploads/2021/04/twitch-logo.png",
});
const button = new ButtonBuilder()
.setLabel("Watch VOD")
.setEmoji("<a:TanWiggle:961250315989049414>")
.setStyle(ButtonStyle.Link)
.setURL(video.url);
const row = new ActionRowBuilder().addComponents(button);
lastMessage.edit({
content: `${lastEmbed.author.name} went offline!`,
embeds: [embed],
components: [row],
});
}
}
async function dbInit() {
const force = process.argv.includes('--force') || process.argv.includes('-f');
sequelize.sync({ force }).then(async () => {
logger.info('Database synced.');
}).catch(logger.error);
}
async function fetchStoreSkins(rawStore) {
const skins = [];
for (const record of rawStore) {
const skin = await axios.get(
"https://valorant-api.com/v1/weapons/skinlevels/" + record.OfferID
);
const costsArray = Object.values(record.Cost);
const cost = costsArray[0];
const skinData = {
name: skin.data.data.displayName,
icon: skin.data.data.displayIcon,
offerId: record.OfferID,
cost: cost.toString(),
};
skins.push(skinData);
}
return skins;
}