-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.js
158 lines (146 loc) · 4.47 KB
/
main.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
require("dotenv").config();
const Discord = require("discord.js");
const { MessageActionRow, MessageButton } = require("discord.js");
const express = require("express");
const axios = require("axios");
const link = require("linkvertise.js");
const ejs = require("ejs");
const userId = process.env.LINKVERTISE_USER_ID;
const linkvertise = new link(userId);
const client = new Discord.Client({ intents: 32767 });
const app = express();
const port = 300;
const data = {};
const appURL = process.env.APP_URL;
const cooldownTime = parseInt(process.env.COOLDOWN) * 60 * 1000;
const cooldowns = new Map();
app.set("view engine", "ejs");
app.set("views", "views");
app.use(express.static("assets"));
function generateToken() {
var length = 50,
charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890",
token = "";
for (var i = 0, n = charset.length; i < length; ++i) {
token += charset.charAt(Math.floor(Math.random() * n));
}
return token;
}
client.on("messageCreate", (message) => {
if (message.content.startsWith("!earn")) {
const id = message.author.id;
const options = {
method: "GET",
url: process.env.DASH_URL + "/api/userinfo/",
params: { id: id },
headers: { Authorization: `Bearer ${process.env.DASH_API}` },
};
axios.request(options).then(async function (response) {
RESdata = response.data;
if (RESdata.status == "invalid id") {
message.reply(
`Your are not registered! Register on: ${process.env.DASH_URL}/login`
);
} else {
if (cooldowns.has(message.author.id)) {
const expirationTime = cooldowns.get(message.author.id) + cooldownTime;
if (Date.now() < expirationTime) {
let seconds = (expirationTime - Date.now()) / 1000;
let minutes = Math.floor(seconds / 60);
let extraSeconds = seconds % 60;
extraSeconds =
extraSeconds < 10 ? "0" + extraSeconds : extraSeconds;
let embed = new Discord.MessageEmbed()
.setTitle("Cooldown!")
.setDescription(
"Please wait `" +
`${minutes}` +
" minutes` and `" +
`${Math.floor(extraSeconds)} ` +
"seconds` before generating the link again."
)
.setColor("#0099ff");
message.reply({
content: `<@${message.author.id}>`,
embeds: [embed],
});
return;
}
}
cooldowns.set(message.author.id, Date.now());
let user = message.author.id;
let otp = generateToken();
data[user] = generateToken();
let url = process.env.APP_URL + "/earn/" + user + "/" + otp;
const row = new MessageActionRow().addComponents(
new MessageButton()
.setURL(url)
.setLabel("Earn Coins")
.setStyle("LINK")
);
let embed = new Discord.MessageEmbed()
.setTitle("Earn Coins")
.setDescription(
"**Success!** Link Generated for user: `" +
user +
"` \n```Hint: Use below buttons to get the link.```"
)
.setColor("#0099ff")
.setFooter({
text: `Requested By ${message.author.tag} | Made with ❤️ by NicoRuizDev`,
iconURL: `${process.env.APP_ICON}`,
});
message.reply({
content: `<@${message.author.id}>`,
embeds: [embed],
components: [row],
});
}
});
}
});
app.get("/earn/:user/:otp", (req, res) => {
const user = req.params.user;
const otp = req.params.otp;
let url = `/redirect/${user}/${otp}`;
res.render("start", { url: url });
});
app.get("/redirect/:user/:otp", (req, res) => {
const user = req.params.user;
const otp = req.params.otp;
const token = data[user];
let url = `${appURL}/verify/${user}/${otp}/${token}`;
let shrink = linkvertise.shrink(url);
res.redirect(shrink);
});
app.get("/verify/:user/:otp/:token", (req, res) => {
const user = req.params.user;
const otp = req.params.otp;
const token = data[user];
const coins = parseInt(process.env.COINS_TO_GIVE);
if (data[user] == undefined) {
res.redirect("/404");
} else if (data[user] == token) {
const dataRES = { id: user, coins: coins };
axios
.post(process.env.DASH_URL + "/api/addcoins", dataRES, {
headers: {
Authorization: `Bearer ${process.env.DASH_API}`,
},
})
.then(() => {
delete data[user];
res.render("success", { coins: coins, user: user });
});
} else {
res.redirect("/404");
}
});
app.get("/404", (req, res) => {
res.render("404");
});
app.listen(port, () => console.log(`Application listening on port ${port}!`));
client.once("ready", (c) => {
console.log(`Ready! Logged in as ${c.user.tag}`);
});
client.login(process.env.DISCORD_TOKEN);