-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
107 lines (94 loc) · 3.2 KB
/
server.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
const express = require("express");
const request = require("request");
const app = express();
const bodyParser = require("body-parser");
const { Client, Intents } = require("discord.js");
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const discord = require("discord.js");
const config = require("./config.json");
var discordid;
app.use(bodyParser.urlencoded({ extended: true }));
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
app.get("/", (req, res) => {
var code = req.query.code;
var options = {
method: "POST",
url: "https://discord.com/api/oauth2/token",
headers: { "content-type": "application/x-www-form-urlencoded" },
form: {
client_id: config.client_id,
client_secret: config.client_secret,
grant_type: "authorization_code",
code: code,
redirect_uri: config.redirect_uri,
scope: "identify",
},
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
var parsed = JSON.parse(body);
var options = {
method: "GET",
url: "https://discord.com/api/users/@me",
headers: { authorization: `Bearer ${parsed.access_token}` },
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
if (response.statusCode != 200) {
res.redirect(
`https://discord.com/oauth2/authorize?client_id=${config.client_id}&redirect_uri=${config.redirect_uri}&response_type=code&scope=identify`
);
return;
}
var parsed = JSON.parse(body);
discordid = parsed.id;
res.sendFile("/verify.html", { root: __dirname });
});
});
});
app.post("/", (req, res) => {
var options = {
method: "POST",
url: "https://hcaptcha.com/siteverify",
headers: {
"content-type":
"multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
},
formData: {
secret: config.hcaptcha_secret,
response: req.body["g-recaptcha-response"],
},
};
request(options, async function (error, response, body) {
if (error) throw new Error(error);
const parsed = JSON.parse(body);
if (parsed.success) {
res.sendFile("/verified.html", { root: __dirname });
const guild = await client.guilds.fetch(config.guild_id);
const member = await guild.members.fetch(discordid);
member.roles.add(config.role_id, `user is verified`);
const user = client.users.cache.get(discordid);
const embed = new discord.MessageEmbed()
.setTitle("Verification")
.setDescription(
"You are now verified!"
)
.setColor("GREEN")
.setTimestamp()
.setFooter("Discord-hcaptcha");
member.send({ embeds: [embed] });
} else {
res.redirect("/");
}
});
});
app.use(express.static("public"));
app.get("/", function (request, response) {
response.sendFile(__dirname + "/verify.html");
});
var listener = app.listen(process.env.PORT || 8081, function() {
console.log('Your app is listening on port ' + listener.address().port);
});
client.login(config.bot_token);