-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (95 loc) · 3.78 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
const {
Client,
Events,
GatewayIntentBits,
ActivityType,
Collection
} = require('discord.js');
const fs = require('node:fs')
require('dotenv').config();
let servers = [];
const getLocaleFile = require('./locale.js');
const package = require('./package.json');
const bot = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences
]
})
const commands = [];
bot.commands = new Collection();
const commandFolders = fs.readdirSync('./commands');
for (const folder of commandFolders) {
const files = fs.readdirSync('./commands/' + folder).filter(file => file.endsWith('.js'));
for (const file of files) {
const command = require(`./commands/${folder}/${file}`);
if ('data' in command && 'execute' in command) {
bot.commands.set(command.data.name, command);
commands.push(command.data.toJSON());
}
}
}
bot.buttons = new Collection();
const buttonFolders = fs.readdirSync('./buttons').filter(file => file.endsWith('.js'));
for (const file of buttonFolders) {
const button = require('./buttons/' + file);
if ('data' in button && 'execute' in button) bot.buttons.set(button.data.data.custom_id, button);
}
bot.once(Events.ClientReady, async (ctx) => {
const commandExecutes = await bot.application?.commands.fetch();
commandExecutes.map(com => {
if (!commands.some(cmd => cmd.name == com.name)) bot.application?.commands.delete(com);
})
for (let i = 0; i < commands.length; i++) {
setActivity('dnd', 'Command: ' + i + '/' + commands.length + ' registered', ActivityType.Playing);
await bot.application.commands.create(commands[i]);
}
getListGuilds();
console.log(ctx.user.username + " started\n" + "Count guilds: " + servers.length);
})
bot.on(Events.GuildCreate, () => getListGuilds());
bot.on(Events.GuildDelete, () => getListGuilds());
bot.on(Events.Error, (error) => console.error(error));
bot.on(Events.Warn, (warning) => console.warn(warning));
bot.on(Events.InteractionCreate, async interaction => {
const lang = await getLocaleFile(interaction.guildLocale || interaction.locale);
if (!interaction.guildId) return await interaction.reply({ content: lang.cmdNotWorkInDM, ephemeral: true });
if (!interaction.isChatInputCommand()) {
const button = bot.buttons.get(interaction.customId);
if (!button) await interaction.reply({ content: lang.btnNotFound, ephemeral: true });
try {
await button.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: lang.btnNotWork, ephemeral: true });
}
} else {
const command = bot.commands.get(interaction.commandName);
if (!command) return await interaction.reply({ content: lang.cmdNotFound, ephemeral: true });
try {
await command.execute(interaction);
} catch (error) {
if (interaction.replied || interaction.deferred)
await interaction.reply({ content: lang.cmdNotWork, ephemeral: true });
else
await interaction.reply({ content: lang.cmdNotFound, ephemeral: true });
console.error(error);
}
}
})
function getListGuilds() {
servers = bot.guilds.cache.map(guild => guild.id);
setActivity('idle', `/help | ${servers.length} servers | v${package.version}`, ActivityType.Competing);
}
function setActivity(status, nameActivity, typeActivity) {
bot.user.setPresence({
status: status,
activities: [{ name: nameActivity, type: typeActivity }],
afk: false
})
}
bot.login(process.env.token);