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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26