-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
165 lines (160 loc) · 5.65 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
console.clear();
let djsVersion = require('./package.json')?.dependencies["discord.js"]
console.log(`
Bot name: ${require('./config.json').botName}
Node version: ${process.version}
Discord.js version: ${djsVersion ? djsVersion : "Missing package" /*doing this instead of the easy way because yes*/}
OS: ${process.platform}
`)
const {
Client,
Events,
GatewayIntentBits,
Collection,
EmbedBuilder,
} = require('discord.js')
const fs = require('node:fs')
const path = require('node:path')
// Create a new client instance
const log = require('./util/log')
const client = new Client({ intents: [GatewayIntentBits.Guilds] })
require('dotenv').config()
const token = process.env.token;
// When the client is ready, run this code (only once)
// We use 'c' for the event parameter to keep it separate from the already defined 'client'
try {
client.once(Events.ClientReady, async (c) => {
const ch = c?.channels?.cache?.get('1065859892335886437')
const emb = new EmbedBuilder()
.setTitle('Refresh')
.setDescription(`Successfully logged into ${c.user.tag}`)
.addFields({
name: 'Username - tag - ID',
value: `${c.user.username} - ${c.user.tag} - ${c.user.id}`,
inline: true,
})
.addFields({
name: 'Bot process',
value: `Platform: ${process.platform}\nNode version: ${process.version}`,
inline: true,
})
.addFields({
name: 'Login time',
value: `${Date.now()} - ${new Date()}`,
})
.setColor('#32a852')
.setTimestamp()
ch?.send({ content: '<@716491639857872928>', embeds: [emb] })
console.log('3: Client start\n')
log('CLIENT', `Logged into ${c.user.tag}. Sob!`)
log('CLIENT', 'Logs from commands or errors will be written below.')
log('CLIENT', 'Errors or logs will also be written in app.log, just in case...')
console.log('---------------------------------------')
log('CLIENT.back', `New login on ${c.user.tag}...`, false, true, true)
})
// Handle commands
client.commands = new Collection()
const commandsPath = path.join(__dirname, 'commands')
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith('.js'))
console.log('---------------------------------------')
console.log('1: Command loading\n')
console.log('Loading commands...')
console.log('Commands loaded:')
let cmdlen = []
for (const file of commandFiles) {
try {
const filePath = path.join(commandsPath, file)
const command = require(filePath)
cmdlen.push(command)
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
console.log(`/${command.data.name}`)
client.commands.set(command.data.name, command)
} else {
console.log(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`,
)
}
} catch (err) {
return log("CMD-EXEC", "Failed to execute command and recieved error:\n" + err);
}
}
console.log('Loaded ' + cmdlen.length + ' commands.')
console.log('---------------------------------------')
console.log('2. Command registering\n')
// register cmds
require('./slash')
// Handle events
const eventsPath = path.join(__dirname, 'events')
const eventFiles = fs
.readdirSync(eventsPath)
.filter((file) => file.endsWith('.js'))
for (const file of eventFiles) {
try {
const filePath = path.join(eventsPath, file)
const event = require(filePath)
if (event.once) {
client.once(event.name, (...args) => event.execute(...args))
} else {
client.on(event.name, (...args) => event.execute(...args))
}
} catch (err) {
return log("EVENT-LOG.back", "Failed to execute event. Node returned error:\n" + err, false, true);
}
}
client.login(token)
} catch (err) {
return log("CLIENT.back", "Error occured in client:" + err, false, true);
}
client.on('messageCreate', async (message, args) => {
if (
message.content.toLocaleLowerCase() === 'sob!kys' &&
message.author.id === '716491639857872928'
) {
const arg = args.join(' ')
if (args && args >= 0 && arg) {
await message.channel.send('Killing bot process with code ' + args)
return process.exit(arg)
} else {
return message.channel.send('Please provide an exit code.')
}
}
})
const exitEvents = ['exit', 'SIGINT', 'SIGUSR1', 'SIGUSR2', 'uncaughtException', 'SIGTERM', 'SIGKILL'];
exitEvents?.forEach((e) => {
process.on(e, async function () {
const c = client.channels.cache.get(require('./config.json').server_channels.acLogin);
const over = new EmbedBuilder()
.setTitle('Process Ended')
.setDescription(determine(e))
.addFields({
name: 'Exit time',
value: `${Date.now()} - ${new Date()}`,
})
.setColor('#32a852')
.setTimestamp();
await c.send({ embeds: [over] });
await log('CLIENT.back', `Process ended with event: ${e}.`, false, true)
return process.exit(1);
});
})
function determine(event) {
if (event === 'exit') {
return 'The bot process ended normally. (exit)'
}
if (event === 'SIGINT') {
return 'The bot process was manually ended. (SIGINT)';
}
if (event === 'SIGUSR1' || event === 'SIGUSR2' || event === 'SIGTERM') {
// idk what these are, but let's just use this
return 'The bot process was automatically ended. (' + event + ")";
}
if (event === 'uncaughtException') {
return 'The bot process was ended due to an error. (' + event + ")";
}
if (event === 'SIGKILL') {
return 'The bot process was ended unconditionally/forcefully. (SIGKILL)'
}
}