1 |
import { TextChannel } from "discord.js"; |
2 |
import { existsSync, readFile, rm } from "fs"; |
3 |
import { writeFile } from "fs/promises"; |
4 |
import path from "path"; |
5 |
import MessageEmbed from "../client/MessageEmbed"; |
6 |
import { fetchEmoji } from "../utils/Emoji"; |
7 |
import Service from "../utils/structures/Service"; |
8 |
import { yellow } from "../utils/util"; |
9 |
|
10 |
export interface RestartLockFileData { |
11 |
date: string; |
12 |
message_id: string; |
13 |
channel_id: string; |
14 |
guild_id: string; |
15 |
} |
16 |
|
17 |
export default class StartupManager extends Service { |
18 |
lockfile = path.join(process.env.SUDO_PREFIX ?? (__dirname + '/../../'), 'tmp/lock'); |
19 |
|
20 |
async createLockFile(data: RestartLockFileData) { |
21 |
await writeFile(this.lockfile, JSON.stringify(data)); |
22 |
} |
23 |
|
24 |
async boot() { |
25 |
if (existsSync(this.lockfile)) { |
26 |
readFile(this.lockfile, async (err, data) => { |
27 |
const { date, message_id, channel_id, guild_id } = <RestartLockFileData> await JSON.parse(data.toString()); |
28 |
|
29 |
console.warn(yellow('Lockfile detected - ' + new Date(date).toLocaleString())); |
30 |
|
31 |
await rm(this.lockfile, () => console.log('Lockfile removed')); |
32 |
|
33 |
try { |
34 |
const guild = await this.client.guilds.fetch(guild_id); |
35 |
const channel = <TextChannel> await guild.channels.fetch(channel_id); |
36 |
const message = await channel.messages.fetch(message_id); |
37 |
|
38 |
if (message) { |
39 |
await message.edit({ |
40 |
embeds: [ |
41 |
new MessageEmbed() |
42 |
.setTitle('System Restart') |
43 |
.setDescription(`${(await fetchEmoji('check'))?.toString()} Restart complete. (Took ${(Date.now() - new Date(date).getTime()) / 1000}s)`) |
44 |
], |
45 |
}); |
46 |
} |
47 |
} |
48 |
catch(e) { |
49 |
console.log(e); |
50 |
} |
51 |
}); |
52 |
} |
53 |
} |
54 |
} |