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-25 23:07:02 +01:00
|
|
|
const ServerSettings = require("../../models/ServerSettings");
|
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-25 23:07:02 +01:00
|
|
|
// Fetch server settings from the database
|
|
|
|
const serverSettings = await ServerSettings.findOne({
|
|
|
|
guildId: interaction.guild.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!serverSettings) {
|
|
|
|
return interaction.reply({
|
|
|
|
content:
|
|
|
|
"Server settings have not been configured yet. Please contact an administrator.",
|
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-09-02 23:05:25 +01:00
|
|
|
// Ensure command is only used in the specified verification channel
|
2024-09-25 23:07:02 +01:00
|
|
|
const verificationChannelId = serverSettings.verificationChannelId;
|
|
|
|
if (interaction.channel.id !== verificationChannelId) {
|
2024-09-02 23:05:25 +01:00
|
|
|
return interaction.reply({
|
2024-09-25 23:07:02 +01:00
|
|
|
content: `This command can only be used in <#${verificationChannelId}> channel.`,
|
2024-09-02 23:05:25 +01:00
|
|
|
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 {
|
2024-09-25 23:07:02 +01:00
|
|
|
// Find the verification code in the database
|
2024-09-02 18:32:03 +01:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-09-25 23:07:02 +01:00
|
|
|
const guild = client.guilds.cache.get(interaction.guild.id);
|
2024-09-02 18:32:03 +01:00
|
|
|
|
|
|
|
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(
|
2024-09-25 23:07:02 +01:00
|
|
|
(r) => r.name === serverSettings.verifiedRoleName
|
2024-09-02 18:32:03 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!role) {
|
2024-09-25 23:07:02 +01:00
|
|
|
console.error(`Role "${serverSettings.verifiedRoleName}" not found.`);
|
2024-09-02 18:32:03 +01:00
|
|
|
return interaction.reply({
|
2024-09-25 23:07:02 +01:00
|
|
|
content: `The role "${serverSettings.verifiedRoleName}" 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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-09-09 15:24:10 +01:00
|
|
|
// Add the verified role to the member
|
2024-09-02 18:32:03 +01:00
|
|
|
await member.roles.add(role);
|
2024-09-09 15:24:10 +01:00
|
|
|
// Delete the verification code entry
|
2024-09-02 20:33:11 +01:00
|
|
|
await VerificationCode.deleteOne({ userId: interaction.user.id, code });
|
|
|
|
|
2024-09-25 23:07:02 +01:00
|
|
|
// Get the log channel and send a log message
|
2024-09-02 21:31:20 +01:00
|
|
|
const adminLogChannel = client.channels.cache.get(
|
2024-09-25 23:07:02 +01:00
|
|
|
serverSettings.logChannelId
|
2024-09-02 21:31:20 +01:00
|
|
|
);
|
|
|
|
if (adminLogChannel) {
|
|
|
|
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-09 15:24:10 +01:00
|
|
|
// Get the general channel and send a welcome message
|
|
|
|
const generalChannel = client.channels.cache.get(
|
2024-09-25 23:07:02 +01:00
|
|
|
serverSettings.generalChannelId
|
2024-09-09 15:24:10 +01:00
|
|
|
);
|
|
|
|
if (generalChannel) {
|
|
|
|
await generalChannel.send({
|
|
|
|
content: `Welcome <@${interaction.user.id}> to the server! 🎉`,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.error("General 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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|