Skip to content
This repository was archived by the owner on May 25, 2023. It is now read-only.

Commit fc066a7

Browse files
committed
i hate javascript
1 parent c0e4f39 commit fc066a7

File tree

3 files changed

+123
-70
lines changed

3 files changed

+123
-70
lines changed

index.js

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1-
const Discord = require('discord.js');
1+
const Discord = require("discord.js");
22
const client = new Discord.Client();
3-
const prefix = '!imp';
4-
const mongoose = require('mongoose');
5-
require('dotenv').config();
3+
const prefix = "!imp";
4+
const mongoose = require("mongoose");
5+
require("dotenv").config();
66

77
// Modules
8-
const postCode = require('./modules/postCode');
9-
const saveMessages = require('./modules/saveMessages');
10-
const getCode = require('./modules/getCode');
11-
const setApiToken = require('./modules/setApiToken');
12-
const help = require('./modules/help');
8+
const postCode = require("./modules/postCode");
9+
const saveMessages = require("./modules/saveMessages");
10+
const getCode = require("./modules/getCode");
11+
const setApiToken = require("./modules/setApiToken");
12+
const help = require("./modules/help");
1313

1414
// Utilities
15-
const throwError = require('./utils/throwError');
15+
const throwError = require("./utils/throwError");
1616

17-
client.on('ready', () => {
18-
client.user.setActivity('!imp help | https://imperialb.in/', { type: 'PLAYING' });
19-
console.log('READY');
20-
mongoose.connect(`mongodb+srv://${process.env.DB_NAME}:${process.env.DB_PASS}@discordusers.hy2f3.mongodb.net/users?retryWrites=true&w=majority`, { useNewUrlParser: true, useUnifiedTopology: true }, (err) => {
21-
if (err) return console.log(err);
22-
console.log('CONNECTED DATABASE');
23-
})
24-
})
17+
client.on("ready", () => {
18+
client.user.setActivity("!imp help | https://imperialb.in/", {
19+
type: "PLAYING",
20+
});
21+
console.log("READY");
22+
mongoose.connect(
23+
process.env.DB_URI,
24+
{ useNewUrlParser: true, useUnifiedTopology: true },
25+
(err) => {
26+
if (err) return console.log(err);
27+
console.log("CONNECTED DATABASE");
28+
}
29+
);
30+
});
2531

26-
client.on('message', async msg => {
27-
if (msg.channel.type == 'dm' && !msg.author.bot) setApiToken(msg, client);
32+
client.on("message", async (msg) => {
33+
if (msg.channel.type == "dm" && !msg.author.bot) setApiToken(msg, client);
2834
if (msg.author.bot) return;
2935
if (msg.content.indexOf(prefix) !== 0) return;
3036
const command = msg.content
@@ -34,32 +40,43 @@ client.on('message', async msg => {
3440
.shift()
3541
.toLowerCase();
3642
switch (true) {
37-
case (command === 'help' || command === 'h'):
43+
case command === "help" || command === "h":
3844
help(msg);
3945
break;
40-
case (command === 'paste' || command === 'postcode' || command === 'post' || command === 'p'):
46+
case command === "paste" ||
47+
command === "postcode" ||
48+
command === "post" ||
49+
command === "p":
4150
postCode(msg);
4251
break;
43-
case (command === 'save' || command === 'savemessages' || command === 'savemessages' || command === 'sm'):
52+
case command === "save" ||
53+
command === "savemessages" ||
54+
command === "savemessages" ||
55+
command === "sm":
4456
saveMessages(msg);
4557
break;
46-
case (command === 'getcode' || command === 'get' || command === 'code' || command === 'g'):
58+
case command === "getcode" ||
59+
command === "get" ||
60+
command === "code" ||
61+
command === "g":
4762
getCode(msg);
4863
break;
49-
case (command === 'setapi' || command === 'setapitoken' || command === 'api' || command === 'setup'):
64+
case command === "setapi" ||
65+
command === "setapitoken" ||
66+
command === "api" ||
67+
command === "setup":
5068
setApiToken(msg);
5169
break;
5270
default:
53-
throwError(msg, 'Unknown command!')
71+
throwError(msg, "Unknown command!");
5472
break;
5573
}
56-
})
74+
});
5775

58-
59-
if (process.env.ENVIRONMENT === 'DEVELOPMENT') {
60-
client.login(process.env.BOT_TOKEN_DEV)
61-
console.log('DEV TOKEN BEING USED!')
76+
if (process.env.ENVIRONMENT === "DEVELOPMENT") {
77+
client.login(process.env.BOT_TOKEN_DEV);
78+
console.log("DEV TOKEN BEING USED!");
6279
} else {
63-
client.login(process.env.BOT_TOKEN)
64-
console.log('PROD TOKEN BEING USED!')
65-
}
80+
client.login(process.env.BOT_TOKEN);
81+
console.log("PROD TOKEN BEING USED!");
82+
}

modules/getCode.js

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,46 @@
1-
const Users = require('../models/User');
2-
const Imperial = require('imperial-node').Imperial;
1+
const Users = require("../models/User");
2+
const Imperial = require("imperial-node").Imperial;
33

44
// Utilities
5-
const throwError = require('../utils/throwError');
5+
const throwError = require("../utils/throwError");
66

7-
module.exports = msg => {
7+
module.exports = (msg) => {
88
Users.findOne({ userId: msg.author.id }, (err, user) => {
9-
if (err) return throwError(msg, 'An internal server error occurred! Please contact an admin!');
9+
if (err)
10+
return throwError(
11+
msg,
12+
"An internal server error occurred! Please contact an admin!"
13+
);
1014
if (user) {
1115
const api = new Imperial(user.apiToken);
12-
const documentId = msg.content.split(' ')[2]
13-
api.getCode(documentId)
14-
.then(paste => {
15-
if (paste.success) {
16+
const documentId = msg.content.split(" ")[2];
17+
api
18+
.getDocument(documentId)
19+
.then((document) => {
20+
if (document) {
1621
msg.channel
17-
.send(`\`\`\`${paste.document}\`\`\``)
18-
.catch(() => throwError(msg, `Code is too long! Here is a URL, https://imperialb.in/p/${documentId}`));
22+
.send(`\`\`\`${document.content}\`\`\``)
23+
.catch(() =>
24+
throwError(
25+
msg,
26+
`Code is too long! Here is a URL, https://imperialb.in/p/${documentId}`
27+
)
28+
);
1929
} else {
20-
throwError(msg, 'Sorry, but we couldn\'t find the paste you were looking for!')
30+
throwError(
31+
msg,
32+
"Sorry, but we couldn't find the paste you were looking for!"
33+
);
2134
}
22-
}).catch(() => throwError(msg, 'There was an error getting your paste!'))
35+
})
36+
.catch(() =>
37+
throwError(msg, "There was an error getting your document!")
38+
);
2339
} else {
24-
throwError(msg, 'Please link your IMPERIAL account by doing `!imp api` before trying to get code!')
40+
throwError(
41+
msg,
42+
"Please link your IMPERIAL account by doing `!imp api` before trying to get code!"
43+
);
2544
}
26-
})
27-
}
45+
});
46+
};

modules/postCode.js

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,54 @@
1-
const Users = require('../models/User');
2-
const Imperial = require('imperial-node').Imperial;
1+
const Users = require("../models/User");
2+
const Imperial = require("imperial-node").Imperial;
33

44
// Utilities
5-
const throwError = require('../utils/throwError');
5+
const throwError = require("../utils/throwError");
66
const options = {
77
max: 1,
8-
time: 30000
9-
}
8+
time: 30000,
9+
};
1010

11-
module.exports = msg => {
11+
module.exports = (msg) => {
1212
Users.findOne({ userId: msg.author.id }, (err, user) => {
1313
if (user) {
14-
msg.channel.send('Go ahead and paste what you want to save in the chat, or type \'CANCEL\' in all caps to cancel!')
15-
msg.channel.awaitMessages(awaitingMessage => awaitingMessage.author.id == msg.author.id, options)
16-
.then(authorsMessage => {
17-
const code = authorsMessage.first()
14+
msg.channel.send(
15+
"Go ahead and paste what you want to save in the chat, or type 'CANCEL' in all caps to cancel!"
16+
);
17+
msg.channel
18+
.awaitMessages(
19+
(awaitingMessage) => awaitingMessage.author.id == msg.author.id,
20+
options
21+
)
22+
.then((authorsMessage) => {
23+
const code = authorsMessage.first();
1824
if (code) {
19-
if (code.content !== 'CANCEL') {
20-
const api = new Imperial(user.apiToken)
21-
api.postCode(code.content)
22-
.then(paste => {
25+
if (code.content !== "CANCEL") {
26+
const api = new Imperial(user.apiToken);
27+
api
28+
.createDocument(code.content)
29+
.then((paste) => {
2330
msg.reply(paste.formattedLink);
2431
code.delete();
2532
})
33+
.catch((_err) => {
34+
throwError(msg, "Your API token may be invalid!");
35+
});
2636
} else {
27-
throwError(msg, 'The operation has been cancelled')
37+
throwError(msg, "The operation has been cancelled");
2838
}
2939
} else {
30-
throwError(msg, 'You didn\'t respond in 30 seconds! The operation has been cancelled')
40+
throwError(
41+
msg,
42+
"You didn't respond in 30 seconds! The operation has been cancelled"
43+
);
3144
}
32-
}).catch(() => throwError(msg, 'An internal server error occurred!'))
45+
})
46+
.catch(() => throwError(msg, "An internal server error occurred!"));
3347
} else {
34-
throwError(msg, 'Please link your IMPERIAL account by doing `!imp api` before trying to post code!')
48+
throwError(
49+
msg,
50+
"Please link your IMPERIAL account by doing `!imp api` before trying to post code!"
51+
);
3552
}
36-
})
37-
}
53+
});
54+
};

0 commit comments

Comments
 (0)