/[sudobot]/branches/5.x/src/commands/settings/RestartCommand.ts
ViewVC logotype

Contents of /branches/5.x/src/commands/settings/RestartCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 5302 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 /**
2 * This file is part of SudoBot.
3 *
4 * Copyright (C) 2021-2023 OSN Developers.
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 { ActionRowBuilder, ButtonBuilder, ButtonStyle, CacheType, Interaction, Snowflake } from "discord.js";
21 import { writeFile } from "fs/promises";
22 import path from "path";
23 import Command, { AnyCommandContext, CommandMessage, CommandReturn, ValidationRule } from "../../core/Command";
24 import { GatewayEventListener } from "../../decorators/GatewayEventListener";
25 import { HasEventListeners } from "../../types/HasEventListeners";
26 import { logError } from "../../utils/logger";
27 import { sudoPrefix } from "../../utils/utils";
28
29 export default class RestartCommand extends Command implements HasEventListeners {
30 public readonly name = "restart";
31 public readonly validationRules: ValidationRule[] = [];
32 public readonly aliases = ["reboot"];
33 public readonly systemAdminOnly = true;
34
35 public readonly description = "Restarts the bot.";
36
37 @GatewayEventListener("interactionCreate")
38 async onInteractionCreate(interaction: Interaction<CacheType>) {
39 if (!interaction.isButton()) {
40 return;
41 }
42
43 const { customId } = interaction;
44
45 if (!customId.startsWith("restart__yes__") && !customId.startsWith("restart__no__")) {
46 return;
47 }
48
49 const [, , guildId, channelId, userId] = customId.split("__");
50
51 if (!guildId || !channelId || !userId) {
52 return;
53 }
54
55 if (interaction.guildId !== guildId || interaction.channelId !== channelId) {
56 return;
57 }
58
59 if (interaction.user.id !== userId) {
60 await interaction
61 .reply({
62 ephemeral: true,
63 content: "That's not under your control!"
64 })
65 .catch(logError);
66 return;
67 }
68
69 if (customId.startsWith("restart__yes__")) {
70 const buttons = this.buildButtons(guildId, channelId, userId).map(button => button.setDisabled(true));
71
72 await interaction.update({
73 embeds: [
74 {
75 color: 0x007bff,
76 title: "System Restart",
77 description: `${this.emoji("loading")} Restarting...`
78 }
79 ],
80 components: [new ActionRowBuilder<ButtonBuilder>().addComponents(...buttons)]
81 });
82
83 const json = JSON.stringify(
84 {
85 guildId,
86 channelId,
87 messageId: interaction.message.id,
88 time: Date.now()
89 },
90 null,
91 4
92 );
93
94 await writeFile(path.join(sudoPrefix("tmp", true), "restart.json"), json);
95 process.exit(this.client.configManager.systemConfig.restart_exit_code);
96 }
97
98 if (customId.startsWith("restart__no__")) {
99 const buttons = this.buildButtons(guildId, channelId, userId).map(button => button.setDisabled(true));
100
101 await interaction.update({
102 embeds: [
103 {
104 color: 0xf14a60,
105 title: "System Restart",
106 description: `Operation cancelled.`
107 }
108 ],
109 components: [new ActionRowBuilder<ButtonBuilder>().addComponents(...buttons)]
110 });
111 }
112 }
113
114 async execute(message: CommandMessage, context: AnyCommandContext): Promise<CommandReturn> {
115 return {
116 __reply: true,
117 embeds: [
118 {
119 color: 0x007bff,
120 title: "System Restart",
121 description: "Are you sure you want to restart the entire system? The bot might go offline for some time."
122 }
123 ],
124 components: [
125 new ActionRowBuilder<ButtonBuilder>().addComponents(
126 ...this.buildButtons(message.guildId!, message.channelId!, message.member!.user.id)
127 )
128 ]
129 };
130 }
131
132 buildButtons(guildId: Snowflake, channelId: Snowflake, userId: Snowflake) {
133 return [
134 new ButtonBuilder()
135 .setCustomId(`restart__yes__${guildId}__${channelId}__${userId}`)
136 .setLabel("Restart")
137 .setStyle(ButtonStyle.Success),
138 new ButtonBuilder()
139 .setCustomId(`restart__no__${guildId}__${channelId}__${userId}`)
140 .setLabel("Cancel")
141 .setStyle(ButtonStyle.Danger)
142 ];
143 }
144 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26