-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.js
236 lines (211 loc) · 9.76 KB
/
verify.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//
//Coded by Trizzey (Discord: .trizzey)
//
const { Client, MessageEmbed, MessageButton, MessageActionRow, MessageAttachment, DiscordAPIError } = require("discord.js");
const config = require("./config");
const { Captcha } = require("captcha-canvas");
const rateLimit = {
lastRequest: 0,
timeout: 5000 // Zeit in ms, um die nächste Anfrage zu ermöglichen
};
async function makeRequest(func, interaction) {
const now = Date.now();
if (now - rateLimit.lastRequest < rateLimit.timeout) {
const waitTime = rateLimit.timeout - (now - rateLimit.lastRequest);
console.log(`Rate limit hit. Waiting for ${waitTime} ms`);
try {
await new Promise(resolve => setTimeout(resolve, waitTime));
await func(); // Versuche es erneut nach der Wartezeit
} catch (error) {
console.error('Error making API request:', error);
if (interaction) {
await interaction.reply({
content: 'Due to high server load, your request is being delayed. Please try again later.',
ephemeral: true
});
}
}
} else {
rateLimit.lastRequest = Date.now();
try {
await func(); // Führe die API-Anfrage aus
} catch (error) {
console.error('Error making API request:', error);
if (interaction) {
await interaction.reply({
content: 'An unexpected error occurred. Please try again later.',
ephemeral: true
});
}
}
}
}
module.exports = async (client) => {
client.once("ready", () => {
console.log(`Bot is ready as ${client.user.tag}`);
});
client.on("interactionCreate", async (interaction) => {
if (interaction.isCommand()) {
switch (interaction.commandName) {
case "ping":
return interaction.reply({
content: `Pong :: ${client.ws.ping}ms`,
ephemeral: true
});
case "setup":
if (!interaction.member.permissions.has("MANAGE_ROLES")) {
return interaction.reply({
content: "You don't have permissions to run this command.",
ephemeral: true
});
}
const verifyChannel = interaction.guild.channels.cache.get(config.verifyChannel);
const verifyRole = interaction.guild.roles.cache.get(config.verifyRole);
if (!verifyChannel || !verifyRole) {
return interaction.reply({
content: "Verify channel or verify role is not found. Please check the configuration.",
ephemeral: true
});
}
const messages = await verifyChannel.messages.fetch({ limit: 100 });
const existingSetup = messages.find(msg => msg.embeds[0] && msg.embeds[0].title === `Verification System of ${interaction.guild.name}`);
if (existingSetup) {
return interaction.reply({
content: "The verification system has already been set up in this channel.",
ephemeral: true
});
}
const embed = new MessageEmbed()
.setColor("#ffee00")
.setTitle(`Verification System of ${interaction.guild.name}`)
.setDescription("**Click the button below to verify yourself through a captcha and gain the Role <@&1278943711681576990> to access the full server! Make sure you're enabled Direct Messages from others and server members!**")
.setFooter({ text: 'The Lux Club Security System' });
const btnrow = new MessageActionRow().addComponents(
new MessageButton()
.setCustomId("v_verify")
.setLabel("Verify")
.setStyle("SUCCESS")
.setEmoji("📑")
);
await makeRequest(async () => {
await verifyChannel.send({
embeds: [embed],
components: [btnrow]
});
}, interaction);
return interaction.reply({
content: `Verification system set up in ${verifyChannel}. Verify role is ${verifyRole}.`,
ephemeral: true
});
default:
await interaction.reply({
content: `The command ${interaction.commandName} is not valid.`,
ephemeral: true
});
break;
}
}
if (interaction.isButton() && interaction.customId === "v_verify") {
const verifyRole = interaction.guild.roles.cache.get(config.verifyRole);
if (!verifyRole) {
return interaction.reply({
content: "Verification role not found. Please contact an administrator.",
ephemeral: true
});
}
if (interaction.member.roles.cache.has(verifyRole.id)) {
return interaction.reply({
content: "You are already verified.",
ephemeral: true
});
}
if (!interaction.guild.members.me.permissions.has("MANAGE_ROLES")) {
return interaction.reply({
content: "I don't have permission to manage roles.",
ephemeral: true
});
}
const captcha = new Captcha();
captcha.async = true;
captcha.addDecoy();
captcha.drawTrace();
captcha.drawCaptcha();
const captchaImage = new MessageAttachment(await captcha.png, "captcha.png");
try {
const dmChannel = await interaction.user.createDM();
const cmsg = await dmChannel.send({
embeds: [
new MessageEmbed()
.setColor("#ffee00")
.setTitle("Captcha Verification")
.setImage('attachment://captcha.png')
.setTimestamp()
.setFooter({ text: 'The Lux Club Security System' })
],
files: [captchaImage]
});
await interaction.reply({
content: "Check your DMs for the captcha.",
ephemeral: true
});
const filter = m => m.author.id === interaction.user.id;
const collected = await dmChannel.awaitMessages({
filter,
max: 1,
time: 20000,
errors: ['time']
});
if (collected.first().content.trim().toLowerCase() === captcha.text.trim().toLowerCase()) {
try {
await interaction.member.roles.add(verifyRole);
await interaction.user.send({
content: "You are now verified and have been granted the role! 🎉"
});
} catch (error) {
if (error instanceof DiscordAPIError && error.code === 50013) {
console.log('Verification failed because of a permissions error.');
await interaction.user.send({
content: "Verification failed due to insufficient permissions. Please contact a server admin."
});
} else {
console.error('Unexpected error in verification:', error);
await interaction.user.send({
content: "An unexpected error occurred. Please try again later."
});
}
}
} else {
await interaction.user.send({
content: "Incorrect captcha. Please try again."
});
}
} catch (error) {
console.error('Error in captcha verification:', error);
let errorMessage = 'An unexpected error occurred. Please try again later.';
if (error.message) {
if (error.message.includes('Cannot send messages to this user')) {
errorMessage = "I couldn't send you a DM. Please check your privacy settings.";
} else if (error.message.includes('Missing Access')) {
errorMessage = "I don't have permission to send you a DM.";
}
}
if (!interaction.replied && !interaction.deferred) {
await interaction.reply({
content: errorMessage,
ephemeral: true
});
}
if (error instanceof DiscordAPIError && error.code === 50013) {
// Handle the permissions error specifically in the catch block above
} else {
await interaction.user.send({
content: errorMessage
});
}
}
}
});
};
//
//Coded by Trizzey (Discord: .trizzey)
//