2024-09-02 18:32:03 +01:00
|
|
|
const { SlashCommandBuilder } = require("discord.js");
|
2024-09-05 03:50:51 +01:00
|
|
|
const VerificationCode = require("../../models/VerificationCode");
|
2024-09-02 18:32:03 +01:00
|
|
|
|
|
|
|
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) {
|
2024-09-02 23:05:25 +01:00
|
|
|
// Ensure command is only used in the specified verification channel
|
|
|
|
const verificationChannelName = process.env.VERIFICATION_CHANNEL_NAME;
|
|
|
|
if (interaction.channel.name !== verificationChannelName) {
|
|
|
|
return interaction.reply({
|
|
|
|
content: `This command can only be used in the #${verificationChannelName} channel.`,
|
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-09-02 18:32:03 +01:00
|
|
|
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(
|
2024-09-05 03:50:51 +01:00
|
|
|
process.env.LOG_CHANNEL_ID
|
2024-09-02 21:31:20 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
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 21:35:05 +01:00
|
|
|
content: `Congratulations <@${interaction.user.id}>, you have been verified!`,
|
2024-09-02 18:32:03 +01:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|