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

Annotation of /branches/4.x/src/commands/settings/RestartCommand.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: 7249 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 { CommandInteraction, InteractionCollector, Message, MessageActionRow, MessageButton } from 'discord.js';
21     import BaseCommand from '../../utils/structures/BaseCommand';
22     import DiscordClient from '../../client/Client';
23     import InteractionOptions from '../../types/InteractionOptions';
24     import MessageEmbed from '../../client/MessageEmbed';
25     import { MessageButtonStyles } from 'discord.js/typings/enums';
26     import { fetchEmoji } from '../../utils/Emoji';
27    
28     export default class RestartCommand extends BaseCommand {
29     supportsInteractions: boolean = true;
30     supportsLegacy = false;
31     ownerOnly = true;
32    
33     constructor() {
34     super('restart', 'settings', []);
35     }
36    
37     async run(client: DiscordClient, interaction: CommandInteraction, options: InteractionOptions) {
38     const row = new MessageActionRow();
39    
40     row.addComponents([
41     new MessageButton()
42     .setCustomId('restart:true')
43     .setLabel('Yes')
44     .setStyle(MessageButtonStyles.SUCCESS),
45     new MessageButton()
46     .setCustomId('restart:false')
47     .setLabel('No')
48     .setStyle(MessageButtonStyles.DANGER)
49     ]);
50    
51     const disabledRow = new MessageActionRow();
52    
53     await disabledRow.addComponents([
54     new MessageButton()
55     .setCustomId('restart:true')
56     .setLabel('Restart')
57     .setStyle(MessageButtonStyles.SUCCESS)
58     .setDisabled(true),
59     new MessageButton()
60     .setCustomId('restart:false')
61     .setLabel('Cancel')
62     .setStyle(MessageButtonStyles.DANGER)
63     .setDisabled(true)
64     ]);
65    
66     await interaction.reply({
67     embeds: [
68     new MessageEmbed()
69     .setTitle('System Restart')
70     .setDescription('Are you sure to restart the system? This will restart the whole bot system including the backend API and might take up to a minute.')
71     ],
72     components: [row]
73     });
74    
75     const reply = <Message> await interaction.fetchReply();
76    
77     const collector = new InteractionCollector(client, {
78     channel: reply.channel,
79     message: reply,
80     componentType: 'BUTTON',
81     interactionType: 'MESSAGE_COMPONENT',
82     filter(i) {
83     return i.isButton() && i.customId.startsWith('restart');
84     },
85     time: 15000
86     });
87    
88     collector.on('collect', async i => {
89     if (!i.isButton())
90     return;
91    
92     if (i.member!.user.id !== interaction.member!.user.id) {
93     await i.reply({
94     content: 'That\'s not your button.',
95     ephemeral: true
96     });
97    
98     return;
99     }
100    
101     if (i.customId === 'restart:true') {
102     await i.update({
103     embeds: [
104     new MessageEmbed()
105     .setColor('#007bff')
106     .setTitle('System Restart')
107     .setDescription((await fetchEmoji('loading'))!.toString() + ' Restarting...')
108     ],
109     components: [disabledRow]
110     });
111    
112     await client.startupManager.createLockFile({
113     date: new Date().toISOString(),
114     guild_id: i.guild!.id,
115     channel_id: i.channel!.id,
116     message_id: reply.id
117     });
118    
119     await process.exit(0);
120     }
121     else {
122     await i.update({
123     embeds: [
124     new MessageEmbed()
125     .setColor('GREY')
126     .setTitle('System Restart')
127     .setDescription('This action has been canceled.')
128     ],
129     components: [disabledRow]
130     });
131     }
132     });
133    
134     collector.on('end', async i => {
135     if (reply.embeds[0].hexColor === '#007bff') {
136     await reply.edit({
137     embeds: [
138     new MessageEmbed()
139     .setColor('GREY')
140     .setTitle('System Restart')
141     .setDescription('This action has been canceled due to inactivity.')
142     ],
143     components: [disabledRow]
144     });
145     }
146     });
147    
148     // reply.awaitMessageComponent({
149     // componentType: 'BUTTON',
150     // filter(i) {
151     // return i.customId.startsWith('restart') && i.member!.user.id === interaction.member!.user.id;
152     // },
153     // time: 15000
154     // })
155     // .then(async i => {
156     // if (i.customId === 'restart:true') {
157     // await i.update({
158     // embeds: [
159     // new MessageEmbed()
160     // .setColor('#007bff')
161     // .setTitle('System Restart')
162     // .setDescription((await fetchEmoji('loading'))!.toString() + ' Restarting...')
163     // ],
164     // components: [disabledRow]
165     // });
166    
167     // await client.startupManager.createLockFile({
168     // date: new Date().toISOString(),
169     // guild_id: i.guild!.id,
170     // channel_id: i.channel!.id,
171     // message_id: reply.id
172     // });
173    
174     // await process.exit(0);
175     // }
176     // else {
177     // await i.update({
178     // embeds: [
179     // new MessageEmbed()
180     // .setColor('GREY')
181     // .setTitle('System Restart')
182     // .setDescription('This action has been canceled.')
183     // ],
184     // components: [disabledRow]
185     // });
186     // }
187     // })
188     // .catch(async e => {
189     // console.log(e);
190    
191     // await reply.edit({
192     // embeds: [
193     // new MessageEmbed()
194     // .setColor('GREY')
195     // .setTitle('System Restart')
196     // .setDescription('This action has been canceled due to inactivity.')
197     // ],
198     // components: [disabledRow]
199     // });
200     // });
201     }
202     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26