-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify.js
More file actions
151 lines (141 loc) · 4.34 KB
/
verify.js
File metadata and controls
151 lines (141 loc) · 4.34 KB
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
const { SlashCommandBuilder } = require('discord.js');
require('dotenv').config();
const { generateWarningEmbed, generateEmbed } = require('./tools');
const axios = require('axios');
async function verify(interaction, email, token) {
if (!token) return;
// Now that we have the token, we can use it to see if the email is registered
const verifyOptions = {
method: 'GET',
url:
'https://api.losaltoshacks.com/attendees/search?email=' +
encodeURIComponent(email),
headers: {
Authorization: 'Bearer ' + token,
},
};
axios(verifyOptions)
.then(function (response) {
if (response.data.length > 0) {
// exists
const data = response.data[0];
interaction.member.roles
.add(process.env.attendeeRole)
.catch(function (error) {
console.log(
'### This is very likely because you have tried running the command as a role higher than the bot!!! ###',
);
console.log(data);
console.error(error);
});
interaction.member
.setNickname(data.first_name + ' ' + data.last_name)
.catch(function (error) {
console.log(
'### This is very likely because you have tried running the command as a role higher than the bot!!! ###',
);
console.log(data);
console.error(error);
});
interaction.reply({
embeds: [
generateEmbed(
'Verified!',
"You are now verified! You'll gain access to the rest of the server soon.",
),
],
ephemeral: true,
});
} else {
interaction.reply({
embeds: [
generateWarningEmbed(
'Error!',
"Looks like you haven't registered yet. You can do so at www.losaltoshacks.com/registration! If you think this is a mistake, contact staff with `/staff`",
),
],
ephemeral: true,
});
}
})
.catch(function (error) {
console.log(error);
interaction.reply({
embeds: [
generateWarningEmbed(
'Error!',
'Oops, something went wrong. Please inform staff with `/staff`.',
),
],
ephemeral: true,
});
});
}
module.exports = {
data: new SlashCommandBuilder()
.setName('verify')
.setDescription(
'Verify your Discord account to gain access to the rest of the server.',
)
.addStringOption((option) =>
option
.setName('email')
.setDescription('Enter the email you registered with')
.setRequired(true),
),
async execute(client, interaction) {
if (interaction.channelId !== process.env.verifyChannel) {
interaction.reply({
embeds: [
generateWarningEmbed(
'Error!',
'You can only use the `/verify` command in the verification channel.',
),
],
ephemeral: true,
});
return;
}
const email = interaction.options.getString('email');
const emailRegex =
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (!emailRegex.test(email)) {
interaction.reply({
embeds: [generateWarningEmbed('Error!', 'Please enter a valid email!')],
ephemeral: true,
});
return;
}
// Below is taken from last year's bot
const options = {
method: 'POST',
url: 'https://api.losaltoshacks.com/auth/token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
data: new URLSearchParams({
username: process.env.apiUsername,
password: process.env.apiPassword,
}),
};
axios(options)
.then(function (response) {
const BACKEND_JWT_TOKEN = response.data.access_token;
verify(interaction, email, BACKEND_JWT_TOKEN);
})
.catch(function (error) {
if (error) {
console.log(error);
interaction.reply({
embeds: [
generateWarningEmbed(
'Error!',
'Oops, something went wrong. Please inform staff with `/staff`.',
),
],
ephemeral: true,
});
}
});
},
};