-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
169 lines (148 loc) · 4.68 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
159
160
161
162
163
164
165
166
167
168
169
const { Client, Intents, Collection } = require('discord.js')
const client = new Client({
partials: ['CHANNEL', 'USER', 'GUILD_MEMBER', 'MESSAGE'],
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.DIRECT_MESSAGES],
})
const config = require('./util/config.json')
const express = require('express');
const app = express();
const axios = require('axios')
const discord = require('discord.js')
const schema = require('./util/schema.js')
require('dotenv').config()
client.commands = new Collection()
client.buttons = new Collection()
client.selectMenus = new Collection()
client.slashCommands = new Collection()
client.cooldowns = new Collection();
['commandHandler.js', 'interactionHandler.js', 'eventHandler.js'].forEach((handler) => {
require(`./handlers/${handler}`)(client)
})
process.on('unhandledRejection', error => {
console.error('Unhandled promise rejection:', error);
});
// webserver
app.listen(config.port, () => {
console.log(`Listening on port ${config.port}`)
})
app.get('/', (req, res) => {
res.send('Welcome to the Roblox Verification API! This is not a website, but an API.')
})
app.get('/discord/:discordid', async (req, res) => {
// use db to find user
const discordid = req.params.discordid
const data = await schema.find({ discordID: discordid }).exec();
if (data.length === 0) {
res.send({
verified: false
})
} else {
res.status(200);
res.send({
robloxID: data[0].robloxID,
robloxUsername: data[0].robloxUsername,
verified: data[0].verified
})
}
})
app.get('/roblox/:robloxid', async (req, res) => {
// use db to find user
const robloxid = req.params.robloxid
const data = await schema.find({ robloxID: robloxid }).exec();
if (data.length === 0) {
res.send({
verified: false
})
} else {
res.status(200);
res.send({
discordID: data[0].discordID,
robloxUsername: data[0].robloxUsername,
verified: data[0].verified
})
}
})
app.get('/redirect', async ( req, res ) => {
// make code into string
const code = req.query.code
const state = req.query.state
try {
const data = await schema.find({ statecode: state }).exec();
if (data.length === 0) {
res.send('An error occurred');
res.status(404);
res.end();
} else {
const user = await client.users.fetch(data[0].discordID);
if (!user) {
res.send('An error occurred');
res.status(404);
res.end();
} else {
res.status(200);
res.send('You may close this tab now');
const params = new URLSearchParams();
params.append("client_id", config.robloxclientID);
params.append("client_secret", config.robloxtoken);
params.append("grant_type", "authorization_code");
params.append("code", code);
const response = await axios.post(`https://apis.roblox.com/oauth/v1/token`, params, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
if (response.status === 200) {
const access_token = response.data.access_token;
const userInfoResponse = await fetch(`https://apis.roblox.com/oauth/v1/userinfo`, {
headers: {
Authorization: `Bearer ${access_token}`
}
});
if (userInfoResponse.status === 200) {
const userInfo = await userInfoResponse.json();
data[0].verified = true;
data[0].robloxID = userInfo.sub;
data[0].robloxUsername = userInfo.preferred_username;
data[0].statecode = null;
await data[0].save();
// change nickname to roblox username
const guild = client.guilds.cache.get(config.guildID)
const username = userInfo.preferred_username
const member = await guild.members.fetch(user.id)
if (member.id === guild.ownerId) {
}
else {
member.setNickname(username)
}
const embed = new discord.MessageEmbed()
.setColor('GREEN')
.setDescription(`Successfully verified ${user.tag}!`)
.setTimestamp()
.setFooter({ text: 'Success!' });
client.channels.cache.get(config.verificationChannel).send({ embeds: [embed] });
} else {
const embed = new discord.MessageEmbed()
.setColor('RED')
.setDescription('An error occurred while verifying your account!')
.setTimestamp()
.setFooter({ text: 'Error!' });
client.channels.cache.get(config.verificationChannel).send({ embeds: [embed] });
}
} else {
const embed = new discord.MessageEmbed()
.setColor('RED')
.setDescription('An error occurred while verifying your account!')
.setTimestamp()
.setFooter({ text: 'Error!' });
client.channels.cache.get(config.verificationChannel).send({ embeds: [embed] });
}
}
}
} catch (err) {
console.log(err);
res.send('An error occurred');
res.status(500);
res.end();
}
})
client.login(process.env.BOT_TOKEN)