-
Notifications
You must be signed in to change notification settings - Fork 6
/
Bot.js
167 lines (139 loc) · 5.31 KB
/
Bot.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var awsCli = require('aws-cli-js');
var Options = awsCli.Options;
var Aws = awsCli.Aws;
const { ACCESSKEY } = require('../config.json');
const { SECRETKEY } = require('../config.json');
const { SESSIONTOKEN } = require('../config.json');
const { INSTANCE } = require('../config.json');
const { MESSAGELOGGING } = require('../config.json');
var verboseLog = (MESSAGELOGGING === 'T');
const StartCommand = 'ec2 start-instances --instance-ids ' + INSTANCE;
const StopCommand = 'ec2 stop-instances --instance-ids ' + INSTANCE;
const StatusCommand = 'ec2 describe-instances --instance-id ' + INSTANCE;
let options = new Options(
/* accessKey */ ACCESSKEY,
/* secretKey */ SECRETKEY,
/* sessionToken */ SESSIONTOKEN,
/* currentWorkingDirectory */ null
);
const aws = new Aws(options);
const eris = require('eris');
const { BOT_TOKEN } = require('../config.json');
const { ROLEID } = require('../config.json');
const { CHANNELID } = require('../config.json');
const PREFIX = '$aws';
const HelpDocs =
'\n Help Docs: \n `start`: Starts the server \n `stop`: Stops the server \n `status`: Returns the server status ';
// Create a Client instance with our bot token.
const bot = new eris.Client(BOT_TOKEN);
// When the bot is connected and ready, log to console.
bot.on('ready', () => {
console.log('Connected and ready.');
});
const commandHandlerForCommandName = {};
commandHandlerForCommandName['start'] = (msg, args) => {
startStop(true, msg);
};
commandHandlerForCommandName['stop'] = (msg, args) => {
startStop(false, msg);
};
commandHandlerForCommandName['help'] = (msg, args) => {
return msg.channel.createMessage(HelpDocs);
};
function startStop(isStart, msg){
var cmd = isStart ? StartCommand : StopCommand;
var word = isStart ? 'Starting' : 'Stopping';
console.warn(`${word} the server`);
try {
aws.command(cmd)
.then(function (data) {
console.warn('data = ', data);
return msg.channel.createMessage(`<@${msg.author.id}> ${word} the server`);
})
.catch(function (e) {
if (verboseLog) {
msg.channel.createMessage(`<@${msg.author.id}> Error ${word} the server, ${e}`);
} else {
msg.channel.createMessage(`<@${msg.author.id}> Error ${word} the server`);
}
});
} catch (err) {
msg.channel.createMessage(`<@${msg.author.id}> Error ${word} the server`);
return msg.channel.createMessage(err);
}
}
commandHandlerForCommandName['status'] = (msg, args) => {
console.warn("Getting server status");
try {
aws.command(StatusCommand)
.then(function (data) {
var reply = data.object.Reservations[0].Instances[0];
return msg.channel.createMessage(`<@${msg.author.id}> *Status: \n **Name**: ${reply.Tags[0].Value} \n **State**: ${reply.State.Name} \n **IP Address**: ${reply.PublicIpAddress} \n **Last Startup**: ${reply.LaunchTime}*`);
})
.catch(function (e) {
if (verboseLog) {
msg.channel.createMessage(`<@${msg.author.id}> Error getting status, ${e}`);
} else {
msg.channel.createMessage(`<@${msg.author.id}> Error getting status`);
}
console.warn(e);
});
} catch (err) {
console.warn(err);
msg.channel.createMessage(`<@${msg.author.id}> Error getting status`);
return msg.channel.createMessage(err);
}
};
// Every time a message is sent anywhere the bot is present,
// this event will fire and we will check if the bot was mentioned.
// If it was, the bot will attempt to respond with "Present".
bot.on('messageCreate', async (msg) => {
const content = msg.content;
const botWasMentioned = msg.mentions.find(
mentionedUser => mentionedUser.id === bot.user.id,
);
//make sure it's in the right channel
if (!(msg.channel.id === CHANNELID)) {
return;
}
//if the message is sent by a bot, ignore it
if (msg.author.bot) {
return;
}
if (!msg.member.roles.includes(ROLEID)) {
await msg.channel.createMessage(`<@${msg.author.id}> You do not have the required roles`);
return;
}
if (botWasMentioned) {
await msg.channel.createMessage('Brewing the coffee and ready to go!');
}
//ignore dms, guild messages only
if (!msg.channel.guild) {
console.warn('Received a dm, ignoring');
return;
}
// Ignore any message that doesn't start with the correct prefix.
if (!content.startsWith(PREFIX)) {
return;
}
// Extract the parts of the command and the command name
const commandName = content.split(PREFIX)[1].trim();
// Get the appropriate handler for the command, if there is one.
const commandHandler = commandHandlerForCommandName[commandName];
if (!commandHandler) {
await msg.channel.createMessage('Unkown command, try `$aws help` for a list of commands');
return;
}
// Separate the command arguments from the command prefix and command name.
try {
// Execute the command.
await commandHandler(msg, commandName);
} catch (err) {
console.warn('Error handling command');
console.warn(err);
}
});
bot.on('error', err => {
console.warn(err);
});
bot.connect();