This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
102 lines (83 loc) · 4.08 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
const { Client, Intents } = require('discord.js')
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { getVoiceConnection } = require('@discordjs/voice');
const config = require('./config.json')
const fs = require('fs')
const logger = require('./lib/utils/logger.js')
const utils = require('./lib/utils/utils.js')
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES],
allowedMentions: { parse: [] },
});
const rest = new REST({ version: '9' }).setToken(config.token);
client.commands = new Map();
const slashCommands = [];
const cooldown = new Set();
const commandFiles = fs.readdirSync('./lib/commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./lib/commands/${file}`);
if (!command.data) console.log(command)
client.commands.set(command.data.name, command)
slashCommands.push(command.data.toJSON());
}
client.on('ready', async () => {
logger.info(`Connected to Discord (${client.user.tag})`);
client.user.setActivity("/help");
try {
logger.info('Refreshing Discord slash commands');
await rest.put(
Routes.applicationCommands(client.user.id),
{ body: slashCommands },
);
logger.info('Successfully reloaded Discord slash commands');
} catch (err) {
console.error(err);
}
});
client.on('guildCreate', async (guild) => {
const { voice, lang } = config.defaultValues
await utils.query(`INSERT INTO guilds (guild_id, voice, lang) VALUES (?, ?, ?)`, [guild.id, voice, lang])
logger.info(`Joined ${guild.name}`)
});
client.on('guildDelete', async (guild) => {
await utils.query(`DELETE FROM guilds WHERE guild_id=?`, [guild.id])
await utils.redis.del(`pt:guild:${guild.id}`)
logger.info(`Left ${guild.name}`)
});
client.on('voiceStateUpdate', (oldMember, newMember) => {
if (newMember.id === client.user.id && newMember.channel &&
!newMember.channel.members.filter(a => !a.user.bot).size) return getVoiceConnection(oldMember.guild.id)?.destroy()
if (oldMember.channel && oldMember.channelId === oldMember.guild.me.voice.channelId &&
!oldMember.channel.members.filter(a => !a.user.bot).size) return getVoiceConnection(oldMember.guild.id)?.destroy()
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;
const cacheData = await utils.redis.get(`pt:guild:${interaction.guild.id}`)
if (cacheData) interaction.query = JSON.parse(cacheData)
else {
const channelQuery = await utils.query(`SELECT voice, lang FROM guilds WHERE guild_id=? LIMIT 1`, [interaction.guild.id])
if (!channelQuery.length) {
await utils.query(`INSERT INTO guilds (guild_id, voice, lang) VALUES (?, ?, ?)`, [interaction.guild.id, voice, lang])
interaction.query = { voice, lang }
} else interaction.query = channelQuery[0]
await utils.redis.set(`pt:guild:${interaction.guild.id}`, JSON.stringify({ voice: interaction.query.voice, lang: interaction.query.lang }))
}
const command = client.commands.get(interaction.commandName)
if (!command) return interaction.reply({ content: 'Command not found', ephemeral: true });
if (cooldown.has(`${interaction.commandName} ${interaction.user.id}`)) return interaction.reply({ content: 'This command is on cooldown 🐌', ephemeral: true });
try {
command.execute(interaction);
if (command.cooldown) {
cooldown.add(`${interaction.commandName} ${interaction.user.id}`);
setTimeout(() => {
cooldown.delete(`${interaction.commandName} ${interaction.user.id}`);
}, command.cooldown * 1000);
}
logger.info(`${interaction.user.username} executed command ${interaction.commandName} in ${interaction.guild.name}`);
} catch (err) {
console.error(err);
interaction.reply({ content: "We're sorry, an unexpected error occurred 😕", ephemeral: true });
}
});
client.login(config.token);