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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26