-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
184 lines (160 loc) · 7.81 KB
/
Copy pathindex.js
File metadata and controls
184 lines (160 loc) · 7.81 KB
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
require('dotenv').config();
const startTime = Date.now();
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
process.removeAllListeners('warning');
const { GatewayIntentBits, ActivityType, version: djsVersion, SlashCommandBuilder, ActionRowBuilder, Partials } = require('discord.js');
const { SapphireClient, LogLevel, version: sapphireVersion } = require('@sapphire/framework');
const figlet = require('figlet');
const gradient = require('gradient-string');
const { bold, blue, cyan, gray, red, yellow, green, white, dim } = require('colorette');
const fxGradient = gradient(['#1055bc', '#4d8fea', '#1055bc']);
const fs = require('fs');
const path = require('path');
const { templates } = require('./utils/templates');
const { startApi } = require('./utils/api');
const originalFetch = global.fetch;
function formatCentered(text) {
const cols = process.stdout.columns || 80;
return text.split('\n').map(line => {
const pad = Math.max(0, Math.floor((cols - line.length) / 2));
return ' '.repeat(pad) + line;
}).join('\n');
}
function getTimestamp() {
return dim(gray(new Date().toLocaleTimeString('en-GB')));
}
const logger = {
info: (...args) => console.log(`${getTimestamp()} ${bold(fxGradient('INFO '))} ${white(args.join(' '))}`),
warn: (...args) => console.log(`${getTimestamp()} ${bold(yellow('WARN '))} ${white(args.join(' '))}`),
error: (...args) => console.log(`${getTimestamp()} ${bold(red('ERROR '))} ${white(args.join(' '))}`),
fatal: (...args) => console.log(`${getTimestamp()} ${bold(red('FATAL '))} ${white(args.join(' '))}`),
debug: (...args) => console.log(`${getTimestamp()} ${bold(fxGradient('DEBUG '))} ${white(args.join(' '))}`),
trace: (...args) => console.log(`${getTimestamp()} ${bold(gray('TRACE '))} ${white(args.join(' '))}`),
};
const requiredKeys = ['CLIENT_TOKEN', 'AUDIOSCROBBLER'];
const missingKeys = requiredKeys.filter(key => !process.env[key]);
if (missingKeys.length > 0) {
logger.fatal(`Missing required environment variables: ${bold(red(missingKeys.join(', ')))}`);
process.exit(1);
}
const client = new SapphireClient({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions,
GatewayIntentBits.DirectMessageTyping,
GatewayIntentBits.MessageContent,
],
partials: [Partials.Channel, Partials.Message, Partials.Reaction],
rest: { timeout: 30000 },
baseUserDirectory: __dirname,
logger: { level: LogLevel.None },
loadMessageCommandListeners: true,
});
client.once('clientReady', async (c) => {
const banner = figlet.textSync('\nfxmbed', { font: 'ANSI Shadow' });
console.log(fxGradient(formatCentered(banner)));
console.log(fxGradient(formatCentered('fxmbed is a discord all-in-one app · beta\n\n')));
const guildCount = c.guilds.cache.size;
const userCount = c.guilds.cache.reduce((acc, guild) => acc + (guild.memberCount || 0), 0);
let commandCount = 0;
const commandStore = client.stores.get('commands');
if (commandStore) {
commandStore.forEach(cmd => {
try {
const registries = cmd.applicationCommandRegistry.chatInputCommands;
if (registries && registries.size > 0) {
for (const entry of registries) {
let json;
if (typeof entry === 'function') {
const builder = new SlashCommandBuilder().setName(cmd.name).setDescription(cmd.description || 'No description');
entry(builder);
json = builder.toJSON();
} else {
json = entry.toJSON ? entry.toJSON() : entry;
}
if (json.options && json.options.some(o => o.type === 1 || o.type === 2)) {
commandCount += json.options.filter(o => o.type === 1 || o.type === 2).length;
} else {
commandCount += 1;
}
}
} else {
commandCount += 1;
}
} catch (e) {
commandCount += 1;
}
});
}
const startupDuration = ((Date.now() - startTime) / 1000).toFixed(2);
startApi(c);
c.user.setPresence({
status: 'online',
activities: [{
name: 'v.gd/sysmd | /help',
type: ActivityType.Custom,
emoji: { id: '1486150485982445579', name: 'logo' }
}]
});
console.log("\n");
logger.info(`Logged in as ${bold(cyan(c.user.tag))}`);
logger.info(`Serving ${bold(white(guildCount))} guilds · ${bold(white(userCount))} users · ${bold(white((process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2)))} MB`);
logger.info(`Loaded ${bold(white(commandCount))} commands in ${bold(white(startupDuration))}s`);
logger.info(`Running Node ${bold(white(process.version))} · d.js ${bold(white(djsVersion))} · sapphire ${bold(white(sapphireVersion))}`);
});
client.on('interactionCreate', async (interaction) => {
if (interaction.isChatInputCommand()) return;
if (interaction.isButton()) {
if (interaction.customId === 'paginator:cancel') {
try {
if (!interaction.deferred && !interaction.replied) await interaction.deferUpdate().catch(() => {});
await interaction.message.delete().catch(async () => {
await interaction.webhook.deleteMessage(interaction.message.id).catch(() => {});
});
if (interaction.guild) await interaction.channel.messages.delete(interaction.message.id).catch(() => {});
} catch (e) {}
return;
}
if (interaction.customId.startsWith('paginator:')) {
if (interaction.replied || interaction.deferred) return;
try {
const rows = interaction.message.components.map(row => {
const newRow = ActionRowBuilder.from(row.toJSON());
newRow.components.forEach(c => {
if (c.data.custom_id?.startsWith('paginator:')) c.data.disabled = true;
});
return newRow;
});
await interaction.message.edit({ components: rows });
} catch (e) {}
return interaction.reply({
...templates.error({ message: 'This pagination session has expired (the bot may have restarted). Please run the command again.' }),
ephemeral: true
});
}
}
});
process.on('uncaughtException', (err) => {
if (err.code === 'MODULE_NOT_FOUND') {
const moduleName = err.message.split('\n')[0].replace('Cannot find module ', '');
logger.fatal(`Missing dependency: ${bold(red(moduleName))}`);
process.exit(1);
}
logger.error('Uncaught exception:', err?.stack || err);
process.exit(1);
});
process.on('SIGINT', async () => {
logger.warn('Shutting down...');
await client.destroy();
process.exit(0);
});
module.exports = { logger };
client.login(process.env.CLIENT_TOKEN).catch((err) => {
logger.fatal('Failed to login:', err);
process.exit(1);
});