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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 376 - (hide annotations)
Mon Jul 29 17:29:51 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 2062 byte(s)
refactor(boot): use SUDO_PREFIX env variable
1 rakin 58 import { TextChannel } from "discord.js";
2     import { existsSync, readFile, rm } from "fs";
3     import { writeFile } from "fs/promises";
4 rakin 376 import path from "path";
5 rakin 58 import MessageEmbed from "../client/MessageEmbed";
6     import { fetchEmoji } from "../utils/Emoji";
7 rakin 226 import Service from "../utils/structures/Service";
8 rakin 344 import { yellow } from "../utils/util";
9 rakin 58
10     export interface RestartLockFileData {
11     date: string;
12     message_id: string;
13     channel_id: string;
14     guild_id: string;
15     }
16    
17 rakin 226 export default class StartupManager extends Service {
18 rakin 376 lockfile = path.join(process.env.SUDO_PREFIX ?? (__dirname + '/../../'), 'tmp/lock');
19    
20 rakin 58 async createLockFile(data: RestartLockFileData) {
21 rakin 376 await writeFile(this.lockfile, JSON.stringify(data));
22 rakin 58 }
23    
24     async boot() {
25 rakin 376 if (existsSync(this.lockfile)) {
26     readFile(this.lockfile, async (err, data) => {
27 rakin 58 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 rakin 376 await rm(this.lockfile, () => console.log('Lockfile removed'));
32 rakin 58
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     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26