30 lines
1.5 KiB
JavaScript
30 lines
1.5 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 [user] = await pool.query('SELECT * FROM users WHERE discordId = ?', [discordID]);
|
|
if (user) return interaction.reply({ content: "This user is already linked to an account. Unlink before proceeding.", ephemeral: true });
|
|
|
|
const [row] = await pool.query('SELECT * FROM users WHERE robloxId = ?', [robloxId]);
|
|
if (!row) {
|
|
await pool.query('INSERT INTO users (robloxId, discordId) VALUES (?, ?)', [robloxId, discordID]);
|
|
return interaction.reply({ content: "Successfully linked accounts", ephemeral: true });
|
|
} else if (row.discordId && row.discordId !== discordID) {
|
|
return interaction.reply({ content: "This Roblox ID is already linked to another account", ephemeral: true });
|
|
} else {
|
|
await pool.query('UPDATE users SET discordId = ? WHERE robloxId = ?', [discordID, robloxId]);
|
|
}
|
|
} catch (error) {
|
|
log.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 } |