-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
230 lines (203 loc) · 5.76 KB
/
app.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
const path = require("path")
const fs = require("fs").promises
const {REST} = require("@discordjs/rest")
const {Routes} = require("discord-api-types/v10")
const {Client, IntentsBitField: Intents, PermissionsBitField: Permissions, Collection} = require("discord.js")
const {createPool} = require("mysql2/promise")
const pkg = require("./package")
const util = require("util")
const Intent = Intents.Flags
const Permission = Permissions.Flags
const OWNER = (process.env.OWNER || "239031520587808769").split(",").map(str => str.trim())
const {
DISCORD_GUILD: GUILD,
DISCORD_TOKEN: TOKEN,
DISCORD_CLIENT_ID: CLIENT,
MYSQL_HOST: HOST,
MYSQL_USER: USER,
MYSQL_PASS: PASS,
MYSQL_DB: DB,
MYSQL_PORT: PORT
} = process.env
/** @var {Pool} pool */
let pool = createPool({
connectionLimit: 1,
host: HOST,
user: USER,
password: PASS,
database: DB,
port: PORT,
supportBigNumbers: true,
bigNumberStrings: true
})
const client = new Client({
intents: [Intent.DirectMessages, Intent.Guilds]
})
client.pool = pool
client.commands = new Collection()
async function setupDatabase(){
Promise.all([
pool.query(`
CREATE TABLE IF NOT EXISTS authors (
sid BIGINT UNSIGNED,
name VARCHAR(500),
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (sid)
);
`),
pool.query(`
CREATE TABLE IF NOT EXISTS addons (
wsid BIGINT UNSIGNED,
name VARCHAR(500),
author BIGINT UNSIGNED,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (wsid),
FOREIGN KEY (author) REFERENCES authors(sid) ON DELETE CASCADE ON UPDATE CASCADE
);
`),
pool.query(`
CREATE TABLE IF NOT EXISTS files (
path VARCHAR(500),
wsid BIGINT UNSIGNED,
PRIMARY KEY (wsid, path),
FOREIGN KEY (wsid) REFERENCES addons(wsid) ON DELETE CASCADE ON UPDATE CASCADE
);
`),
pool.query(`
CREATE TABLE IF NOT EXISTS components (
component VARCHAR(100),
wsid BIGINT UNSIGNED,
PRIMARY KEY (wsid, component),
FOREIGN KEY (wsid) REFERENCES addons(wsid) ON DELETE CASCADE ON UPDATE CASCADE
);
`),
pool.query(`
CREATE TABLE IF NOT EXISTS vehicles (
vehicle VARCHAR(100),
wsid BIGINT UNSIGNED,
PRIMARY KEY (wsid, vehicle),
FOREIGN KEY (wsid) REFERENCES addons(wsid) ON DELETE CASCADE ON UPDATE CASCADE
);
`),
pool.query(`
CREATE TABLE IF NOT EXISTS errors (
path VARCHAR(500),
wsid BIGINT UNSIGNED,
error TEXT,
PRIMARY KEY (wsid, path),
FOREIGN KEY (wsid, path) REFERENCES files(wsid, path) ON DELETE CASCADE ON UPDATE CASCADE
);
`),
pool.query(`
CREATE TABLE IF NOT EXISTS jobs (
id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT,
status enum("new", "locked", "done") NOT NULL DEFAULT "new",
type VARCHAR(500) NULL DEFAULT "",
data TEXT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
`)]
)
}
async function setupCommands(){
const files = (await fs.readdir(path.join(__dirname, "commands"))).filter(file => file.endsWith(".js"))
for (const file of files){
console.log(`registering ${file}`)
const cmd = require(`./commands/${file}`)
client.commands.set(cmd.data.name, cmd)
}
}
async function registerCommands(client){
const rest = (new REST({version: 9})).setToken(TOKEN)
let cmds = []
for (let cmd of client.commands.values()){
cmds.push(cmd.data.toJSON())
}
console.log("updating guild commands")
try {
await rest.put(
Routes.applicationGuildCommands(CLIENT, GUILD),
{body: cmds},
)
} catch (e){
console.error(e);
}
}
client.on('ready', async () => {
console.log("login complete")
let invite = await client.generateInvite({
scopes: ["applications.commands", "bot"],
permissions: Permission.SEND_MESSAGES
})
console.log(invite)
client.user.setPresence({
status: "online",
afk: false,
game: {
name: "with ions.",
url: "https://photon.lighting",
type: "PLAYING"
}
})
})
client.on('interactionCreate', async (interaction) => {
if (interaction.isButton()){
client.emit('interactionButtonClicked', interaction)
return
}
if (!interaction.isChatInputCommand()){
return;
}
const {commandName, options: opts} = interaction
if (!client.commands.has(commandName)){
return
}
try {
let cmd = client.commands.get(commandName)
const callbacks = cmd.callbacks
let subGroup = opts.getSubcommandGroup(false)
let subCmd = opts.getSubcommand(false)
let callback = undefined
if (subGroup !== null && subCmd !== null){
try {
callback = callbacks[subGroup][subCmd]
} catch (e){}
} else if (subCmd !== null){
try {
callback = callbacks[subCmd]
} catch (e){}
} else {
callback = cmd.execute
}
if (callback === undefined){
let cmdName = [commandName, subGroup, subCmd].filter(Boolean).join(".")
return interaction.reply({content: `${cmdName} is missing a callback.`, ephemeral: true})
} else {
return callback(interaction, client)
}
} catch (e){
console.error(e)
return interaction.reply({content: 'There was an error while executing this command!', ephemeral: true})
}
})
client.on("commandError", (cmd, err, msg) => {
console.error(err)
})
async function main(){
await setupDatabase()
await setupCommands()
await client.login(TOKEN)
await registerCommands(client)
console.log("started")
}
process.on('uncaughtException', (e) => {console.error(e); process.exit(1)})
process.on('unhandledRejection', (e) => {console.error(e)})
process.on('SIGTERM', async () => {
console.log("Recieved SIGTERM, hanging up.")
await client.destroy()
process.exit(0)
})
main()