This repository has been archived by the owner on May 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
282 lines (270 loc) · 8.49 KB
/
setup.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
let inquirer = require("inquirer");
let fs = require("fs");
let chalk = require("chalk");
const ora = require("ora");
let mysql = require("mysql");
console.log(chalk.blueBright("Thanks for picking VukkyBot! Let's get started..."));
let sql;
async function isTokenValid(token) {
console.log(chalk.blueBright("\nValidating your token..."));
const spinner = ora("Connecting to the Discord API").start();
try {
const Discord = require("discord.js");
const client = new Discord.Client();
await client.login(token);
client.destroy();
spinner.succeed("Token is valid");
return true;
} catch {
spinner.fail("Token appears to be invalid");
return false;
}
}
let questions = [
{
type: "confirm",
name: "launch",
message: "Start VukkyBot after setup is complete?"
},
{
type: "input",
name: "prefix",
message: "What's the prefix for your VukkyBot going to be?",
default: "v!",
},
{
type: "checkbox",
name: "extrafeatures",
message: "What extra features would you like?",
choices: [
{ name: "MySQL features (MySQL database required)", value: "mysql" },
{ name: "Invalid command reminders", value: "invalidcmd" },
{ name: "Prefix reminders", value: "prefixremind" },
{ name: "Update checks", value: "updates" },
{ name: "Message reporting", value: "msgreporting" },
]
},
{
type: "checkbox",
name: "mysqlfeatures",
message: "What MySQL features would you like? Warns are always on.",
choices: [
{ name: "Counting", value: "counting" }
],
when: function (answers) {
return answers.extrafeatures !== undefined && answers.extrafeatures.includes("mysql");
},
},
{
type: "password",
name: "token",
message: "What's your bot token?",
validate: isTokenValid,
mask: true
},
{
type: "confirm",
name: "multipleowners",
message: "Should your VukkyBot have multiple owners?"
},
{
type: "input",
name: "discordid",
message: "What's the Discord ID of the person who'll be owning this VukkyBot?",
when: function (answers) {
return answers.multipleowners !== true;
}
},
{
type: "input",
name: "discordidmulti",
message: "What are the Discord IDs of the people who'll be owning this VukkyBot, split with commas (id1, id2, id3)?",
when: function (answers) {
return answers.multipleowners !== false;
}
},
{
type: "confirm",
name: "updateDms",
message: "Should your VukkyBot DM the owners when an update is found?",
when: function (answers) {
return answers.extrafeatures !== undefined && answers.extrafeatures.includes("updates");
}
},
{
type: "input",
name: "countingchannel",
message: "What is the name of the channel you would like VukkyBot to count in?",
default: "counting",
when: function (answers) {
return answers.mysqlfeatures !== undefined && answers.mysqlfeatures.includes("counting");
}
},
{
type: "confirm",
name: "msgReportingPosting",
message: "What's the name of the channel reports should be posted in?",
when: function (answers) {
return answers.extrafeatures !== undefined && answers.extrafeatures.includes("msgReporting");
}
},
{
type: "confirm",
name: "msgReportingRole",
message: "What's the name of the role that should be pinged when a message is reported?",
when: function (answers) {
return answers.extrafeatures !== undefined && answers.extrafeatures.includes("msgReporting");
}
},
{
type: "input",
name: "sqlhost",
message: "What's your SQL hostname?",
when: function (answers) {
return answers.extrafeatures !== undefined && answers.extrafeatures.includes("mysql");
},
},
{
type: "password",
name: "sqlpass",
message: "What's your SQL password?",
when: function (answers) {
return answers.extrafeatures !== undefined && answers.extrafeatures.includes("mysql");
},
},
{
type: "input",
name: "sqluser",
message: "What's your SQL username?",
when: function (answers) {
return answers.extrafeatures !== undefined && answers.extrafeatures.includes("mysql");
},
},
{
type: "input",
name: "sqldb",
message: "What's your SQL database name?",
when: function (answers) {
return answers.extrafeatures !== undefined && answers.extrafeatures.includes("mysql");
},
}
];
inquirer.prompt(questions).then((answers) => {
saveToEnv();
function saveToEnv() {
const spinner1 = ora("Saving credentials to .env").start();
try {
fs.writeFile(".env", `BOT_TOKEN=${answers.token}\nPREFIX=${answers.prefix}\nSQL_HOST=${answers.sqlhost}\nSQL_PASS=${answers.sqlpass}\nSQL_USER=${answers.sqluser}\nSQL_DB=${answers.sqldb}`, function (err) {
if (err) {
spinner1.fail("Saving credentials to .env failed");
console.log(err);
process.exit(1);
} else {
spinner1.succeed("Saved credentials to .env");
saveToConfig();
}
});
} catch (err) {
spinner1.fail("Saving credentials to .env failed");
console.log(err);
process.exit(1);
}
}
function saveToConfig() {
const spinner2 = ora("Saving configuration to config.json").start();
try {
const config = require("./config.json");
if (answers.multipleowners !== true) { config.misc.owner = [answers.discordid]; } else { config.misc.owner = answers.discordidmulti.split(", "); }
config.misc.invalidCmdReminder = answers.extrafeatures !== undefined && answers.extrafeatures.includes("invalidcmd");
config.misc.prefixReminder = answers.extrafeatures !== undefined && answers.extrafeatures.includes("prefixremind");
config.misc.mysql = answers.extrafeatures !== undefined && answers.extrafeatures.includes("mysql");
if (answers.mysqlfeatures !== undefined && answers.mysqlfeatures.includes("counting")) { config.counting.enabled = answers.mysqlfeatures.counting !== null; } else { config.counting.enabled = false; }
if (answers.mysqlfeatures !== undefined && answers.mysqlfeatures.includes("counting")) { config.counting.channelName = answers.countingchannel; }
if (answers.extrafeatures !== undefined && answers.extrafeatures.includes("updates")) {
config.updateChecker.enabled = true;
if (answers.updateDms == true) config.updateChecker.dmOwner = true;
} else {
config.updateChecker.enabled = false;
}
if (answers.extrafeatures !== undefined && answers.extrafeatures.includes("msgreporting")) {
config.reports.enabled = true;
config.reports.channelName = answers.msgReportingPosting;
config.reports.staffRoleName = answers.msgReportingRole;
} else {
config.reports.enabled = false;
}
fs.writeFile("config.json", JSON.stringify(config, null, 4), function (err) {
if (err) {
spinner2.fail("Saving configuration to config.json failed");
console.log(err);
process.exit(1);
} else {
spinner2.succeed("Saved configuration to config.json");
startMySQL();
}
});
} catch (err) {
spinner2.fail("Saving configuration to config.json failed");
console.log(err);
process.exit(1);
}
}
function startMySQL() {
if(answers.extrafeatures !== undefined && answers.extrafeatures.includes("mysql")) {
const spinner4 = ora("Connecting to the database").start();
let con = mysql.createConnection({
host: answers.sqlhost,
user: answers.sqluser,
password: answers.sqlpass,
database: answers.sqldb
});
con.connect(function (err) {
if (err) {
spinner4.fail("Failed to connect to the database");
console.log(err);
process.exit(1);
} else {
spinner4.succeed("Connected to the database");
const spinner5 = ora("Creating table in the database").start();
sql = "CREATE TABLE warnings (username VARCHAR(255), serverid VARCHAR(255), uid VARCHAR(255), reason VARCHAR(255), id INT AUTO_INCREMENT PRIMARY KEY)";
con.query(sql, function (err, result) {
if (err) {
if (err.code == "ER_TABLE_EXISTS_ERROR") {
spinner5.warn("Table already exists in the database");
} else {
spinner5.fail("Failed to create table in the database");
console.log(err);
}
} else {
spinner5.succeed("Created table in the database");
}
});
sql = "DROP TABLE counting";
con.query(sql, function (err, result) {
launchyBotty();
});
}
});
} else {
launchyBotty();
}
}
// eslint-disable-next-line no-inner-declarations
function launchyBotty() {
if (answers.launch) {
const spinner3 = ora("Starting VukkyBot").start();
try {
const npm = require("npm");
npm.load(() => {
npm.run("start");
});
spinner3.succeed("VukkyBot should start now");
} catch (err) {
spinner3.fail("Couldn't start VukkyBot");
console.log(err);
}
} else {
process.exit(0);
}
}
});