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