/[sudobot]/branches/4.x/src/services/StartupManager.ts
ViewVC logotype

Annotation of /branches/4.x/src/services/StartupManager.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 2786 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /**
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2022 OSN Inc.
5     *
6     * SudoBot is free software; you can redistribute it and/or modify it
7     * under the terms of the GNU Affero General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * SudoBot is distributed in the hope that it will be useful, but
12     * WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU Affero General Public License for more details.
15     *
16     * You should have received a copy of the GNU Affero General Public License
17     * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18     */
19    
20     import { TextChannel } from "discord.js";
21     import { existsSync, readFile, rm } from "fs";
22     import { writeFile } from "fs/promises";
23     import path from "path";
24     import MessageEmbed from "../client/MessageEmbed";
25     import { fetchEmoji } from "../utils/Emoji";
26     import Service from "../utils/structures/Service";
27     import { yellow } from "../utils/util";
28    
29     export interface RestartLockFileData {
30     date: string;
31     message_id: string;
32     channel_id: string;
33     guild_id: string;
34     }
35    
36     export default class StartupManager extends Service {
37     lockfile = path.join(process.env.SUDO_PREFIX ?? (__dirname + '/../../'), 'tmp/lock');
38    
39     async createLockFile(data: RestartLockFileData) {
40     await writeFile(this.lockfile, JSON.stringify(data));
41     }
42    
43     async boot() {
44     if (existsSync(this.lockfile)) {
45     readFile(this.lockfile, async (err, data) => {
46     const { date, message_id, channel_id, guild_id } = <RestartLockFileData> await JSON.parse(data.toString());
47    
48     console.warn(yellow('Lockfile detected - ' + new Date(date).toLocaleString()));
49    
50     await rm(this.lockfile, () => console.log('Lockfile removed'));
51    
52     try {
53     const guild = await this.client.guilds.fetch(guild_id);
54     const channel = <TextChannel> await guild.channels.fetch(channel_id);
55     const message = await channel.messages.fetch(message_id);
56    
57     if (message) {
58     await message.edit({
59     embeds: [
60     new MessageEmbed()
61     .setTitle('System Restart')
62     .setDescription(`${(await fetchEmoji('check'))?.toString()} Restart complete. (Took ${(Date.now() - new Date(date).getTime()) / 1000}s)`)
63     ],
64     });
65     }
66     }
67     catch(e) {
68     console.log(e);
69     }
70     });
71     }
72     }
73     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26