2024-09-02 18:32:03 +01:00
|
|
|
const { SlashCommandBuilder } = require("discord.js");
|
|
|
|
const VerificationCode = require("../models/VerificationCode");
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
data: new SlashCommandBuilder()
|
|
|
|
.setName("code")
|
|
|
|
.setDescription("Verify your account with a verification code")
|
|
|
|
.addStringOption((option) =>
|
|
|
|
option
|
|
|
|
.setName("code")
|
|
|
|
.setDescription("Your verification code")
|
|
|
|
.setRequired(true)
|
|
|
|
),
|
|
|
|
|
|
|
|
async execute(interaction, client) {
|
|
|
|
const code = interaction.options.getString("code");
|
|
|
|
|
|
|
|
if (!code) {
|
|
|
|
return interaction.reply({
|
2024-09-02 18:51:45 +01:00
|
|
|
content:
|
2024-09-02 20:33:11 +01:00
|
|
|
"Please provide the verification code sent to your email address.",
|
2024-09-02 18:32:03 +01:00
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const verificationEntry = await VerificationCode.findOne({
|
|
|
|
userId: interaction.user.id,
|
|
|
|
code,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!verificationEntry) {
|
|
|
|
return interaction.reply({
|
|
|
|
content: "Invalid or expired verification code. Please try again.",
|
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const guild = client.guilds.cache.get(process.env.GUILD_ID);
|
|
|
|
|
|
|
|
if (!guild) {
|
|
|
|
console.error("Guild not found.");
|
|
|
|
return interaction.reply({
|
2024-09-02 20:33:11 +01:00
|
|
|
content: "The guild could not be found.",
|
2024-09-02 18:32:03 +01:00
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const member = guild.members.cache.get(interaction.user.id);
|
|
|
|
|
|
|
|
if (!member) {
|
|
|
|
console.error("Member not found in the guild.");
|
|
|
|
return interaction.reply({
|
2024-09-02 20:33:11 +01:00
|
|
|
content: "You are not a member of the guild.",
|
2024-09-02 18:32:03 +01:00
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const role = guild.roles.cache.find(
|
|
|
|
(r) => r.name === process.env.VERIFIED_ROLE_NAME
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!role) {
|
|
|
|
console.error(`Role "${process.env.VERIFIED_ROLE_NAME}" not found.`);
|
|
|
|
return interaction.reply({
|
2024-09-02 20:33:11 +01:00
|
|
|
content: `The role "${process.env.VERIFIED_ROLE_NAME}" could not be found.`,
|
2024-09-02 18:32:03 +01:00
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (member.roles.cache.has(role.id)) {
|
|
|
|
return interaction.reply({
|
|
|
|
content: "You are already verified!",
|
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await member.roles.add(role);
|
2024-09-02 20:33:11 +01:00
|
|
|
await VerificationCode.deleteOne({ userId: interaction.user.id, code });
|
|
|
|
|
2024-09-02 21:31:20 +01:00
|
|
|
// Get the admin log channel
|
|
|
|
const adminLogChannel = client.channels.cache.get(
|
|
|
|
process.env.ADMIN_LOG_CHANNEL_ID
|
|
|
|
);
|
|
|
|
|
|
|
|
if (adminLogChannel) {
|
|
|
|
// Send the log message
|
|
|
|
await adminLogChannel.send({
|
|
|
|
content: `🎉 **Verification Success**\nUser: <@${interaction.user.id}> (${interaction.user.tag})\nRole: ${role.name}`,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.error("Admin log channel not found.");
|
|
|
|
}
|
|
|
|
|
2024-09-02 20:33:11 +01:00
|
|
|
return interaction.reply({
|
2024-09-02 18:32:03 +01:00
|
|
|
content: `Congratulations ${interaction.user.username}, you have been verified!`,
|
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error processing verification code:", err);
|
2024-09-02 20:33:11 +01:00
|
|
|
return interaction.reply({
|
2024-09-02 18:32:03 +01:00
|
|
|
content:
|
|
|
|
"There was an error processing your verification. Please try again later.",
|
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|