NotParcel/commands/forcelink.js

34 lines
1.4 KiB
JavaScript

const client = global.discord_client
const pool = global.db_pool;
const execute = async (interaction) => {
const robloxId = interaction.options.getNumber("roblox-id");
const discordID = interaction.options.getUser("discord-id")?.id;
if (!discordID) return interaction.reply({ content: "You must provide a Discord User", ephemeral: true });
if (!robloxId) return interaction.reply({ content: "You must provide a Roblox ID", ephemeral: true });
try {
const connection = await pool.getConnection();
const [rows] = await connection.query("SELECT * FROM users WHERE robloxId = ?", [robloxId]);
if (rows.length === 0) {
await connection.query("INSERT INTO users (robloxId, discordId) VALUES (?, ?)", [robloxId, discordID]);
} else {
const user = rows[0];
if (!user.discordId) {
await connection.query("UPDATE users SET discordId = ? WHERE robloxId = ?", [discordID, robloxId]);
} else if (user.discordId !== discordID) {
await connection.query("UPDATE users SET discordId = NULL WHERE discordId = ?", [discordID]);
await connection.query("UPDATE users SET discordId = ? WHERE robloxId = ?", [discordID, robloxId]);
}
}
connection.release();
} catch (error) {
console.error(error);
return interaction.reply({ content: "An error occurred while linking your account", ephemeral: true });
}
return interaction.reply({ content: "Successfully linked your account", ephemeral: true });
}
module.exports = { execute }