-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart.js
90 lines (79 loc) · 2.89 KB
/
start.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require('dotenv').config();
const compute = require('@google-cloud/compute');
const { Client, GatewayIntentBits, } = require('discord.js');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
const commands = [
{
name: 'startvm',
description: 'Starts the VM',
},
];
console.log('bot started')
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const requiredRoleId = '1199740171218866196';
const projectId = process.env.PROJECT_ID;
const zone = process.env.ZONE;
const instanceName = process.env.INSTANCE_NAME;
const instancesClient = new compute.InstancesClient();
const operationsClient = new compute.ZoneOperationsClient();
if (!interaction.member.roles.cache.has(requiredRoleId)) {
try {
await interaction.editReply({ content: 'You do not have the required role to use this command.' });
} catch (error) {
console.error(`Failed to send reply: ${error}`);
}
return;
}
if (interaction.commandName === 'startvm') {
await interaction.deferReply();
try {
const projectId = process.env.PROJECT_ID;
const zone = process.env.ZONE;
const instanceName = process.env.INSTANCE_NAME;
const instancesClient = new compute.InstancesClient();
const [response] = await instancesClient.start({
project: projectId,
zone,
instance: instanceName,
});
let operation = response.latestResponse;
const operationsClient = new compute.ZoneOperationsClient();
// Wait for the operation to complete.
while (operation.status !== 'DONE') {
[operation] = await operationsClient.wait({
operation: operation.name,
project: projectId,
zone: operation.zone.split('/').pop(),
});
}
await interaction.followUp('Instance started.');
} catch (error) {
console.error(error);
await interaction.followUp('Failed to start instance.');
}
}
});
client.login(process.env.TOKEN);
const rest = new REST({ version: '9' }).setToken(process.env.TOKEN);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();