/[sudobot]/trunk/src/commands/settings/RestartCommand.ts
ViewVC logotype

Contents of /trunk/src/commands/settings/RestartCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 344 - (show annotations)
Mon Jul 29 17:29:40 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 6525 byte(s)
chore: eslint autofix
1 import { CommandInteraction, InteractionCollector, Message, MessageActionRow, MessageButton } from 'discord.js';
2 import BaseCommand from '../../utils/structures/BaseCommand';
3 import DiscordClient from '../../client/Client';
4 import InteractionOptions from '../../types/InteractionOptions';
5 import MessageEmbed from '../../client/MessageEmbed';
6 import { MessageButtonStyles } from 'discord.js/typings/enums';
7 import { fetchEmoji } from '../../utils/Emoji';
8
9 export default class RestartCommand extends BaseCommand {
10 supportsInteractions: boolean = true;
11 supportsLegacy = false;
12 ownerOnly = true;
13
14 constructor() {
15 super('restart', 'settings', []);
16 }
17
18 async run(client: DiscordClient, interaction: CommandInteraction, options: InteractionOptions) {
19 const row = new MessageActionRow();
20
21 row.addComponents([
22 new MessageButton()
23 .setCustomId('restart:true')
24 .setLabel('Yes')
25 .setStyle(MessageButtonStyles.SUCCESS),
26 new MessageButton()
27 .setCustomId('restart:false')
28 .setLabel('No')
29 .setStyle(MessageButtonStyles.DANGER)
30 ]);
31
32 const disabledRow = new MessageActionRow();
33
34 await disabledRow.addComponents([
35 new MessageButton()
36 .setCustomId('restart:true')
37 .setLabel('Restart')
38 .setStyle(MessageButtonStyles.SUCCESS)
39 .setDisabled(true),
40 new MessageButton()
41 .setCustomId('restart:false')
42 .setLabel('Cancel')
43 .setStyle(MessageButtonStyles.DANGER)
44 .setDisabled(true)
45 ]);
46
47 await interaction.reply({
48 embeds: [
49 new MessageEmbed()
50 .setTitle('System Restart')
51 .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.')
52 ],
53 components: [row]
54 });
55
56 const reply = <Message> await interaction.fetchReply();
57
58 const collector = new InteractionCollector(client, {
59 channel: reply.channel,
60 message: reply,
61 componentType: 'BUTTON',
62 interactionType: 'MESSAGE_COMPONENT',
63 filter(i) {
64 return i.isButton() && i.customId.startsWith('restart');
65 },
66 time: 15000
67 });
68
69 collector.on('collect', async i => {
70 if (!i.isButton())
71 return;
72
73 if (i.member!.user.id !== interaction.member!.user.id) {
74 await i.reply({
75 content: 'That\'s not your button.',
76 ephemeral: true
77 });
78
79 return;
80 }
81
82 if (i.customId === 'restart:true') {
83 await i.update({
84 embeds: [
85 new MessageEmbed()
86 .setColor('#007bff')
87 .setTitle('System Restart')
88 .setDescription((await fetchEmoji('loading'))!.toString() + ' Restarting...')
89 ],
90 components: [disabledRow]
91 });
92
93 await client.startupManager.createLockFile({
94 date: new Date().toISOString(),
95 guild_id: i.guild!.id,
96 channel_id: i.channel!.id,
97 message_id: reply.id
98 });
99
100 await process.exit(0);
101 }
102 else {
103 await i.update({
104 embeds: [
105 new MessageEmbed()
106 .setColor('GREY')
107 .setTitle('System Restart')
108 .setDescription('This action has been canceled.')
109 ],
110 components: [disabledRow]
111 });
112 }
113 });
114
115 collector.on('end', async i => {
116 if (reply.embeds[0].hexColor === '#007bff') {
117 await reply.edit({
118 embeds: [
119 new MessageEmbed()
120 .setColor('GREY')
121 .setTitle('System Restart')
122 .setDescription('This action has been canceled due to inactivity.')
123 ],
124 components: [disabledRow]
125 });
126 }
127 });
128
129 // reply.awaitMessageComponent({
130 // componentType: 'BUTTON',
131 // filter(i) {
132 // return i.customId.startsWith('restart') && i.member!.user.id === interaction.member!.user.id;
133 // },
134 // time: 15000
135 // })
136 // .then(async i => {
137 // if (i.customId === 'restart:true') {
138 // await i.update({
139 // embeds: [
140 // new MessageEmbed()
141 // .setColor('#007bff')
142 // .setTitle('System Restart')
143 // .setDescription((await fetchEmoji('loading'))!.toString() + ' Restarting...')
144 // ],
145 // components: [disabledRow]
146 // });
147
148 // await client.startupManager.createLockFile({
149 // date: new Date().toISOString(),
150 // guild_id: i.guild!.id,
151 // channel_id: i.channel!.id,
152 // message_id: reply.id
153 // });
154
155 // await process.exit(0);
156 // }
157 // else {
158 // await i.update({
159 // embeds: [
160 // new MessageEmbed()
161 // .setColor('GREY')
162 // .setTitle('System Restart')
163 // .setDescription('This action has been canceled.')
164 // ],
165 // components: [disabledRow]
166 // });
167 // }
168 // })
169 // .catch(async e => {
170 // console.log(e);
171
172 // await reply.edit({
173 // embeds: [
174 // new MessageEmbed()
175 // .setColor('GREY')
176 // .setTitle('System Restart')
177 // .setDescription('This action has been canceled due to inactivity.')
178 // ],
179 // components: [disabledRow]
180 // });
181 // });
182 }
183 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26