/[sudobot]/trunk/src/services/StartupManager.ts
ViewVC logotype

Annotation of /trunk/src/services/StartupManager.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26