From 745f533384d6e7f79951c81ea2263529553ae42f Mon Sep 17 00:00:00 2001 From: Moonded Date: Tue, 27 Aug 2024 16:04:40 +0200 Subject: [PATCH] update link command --- package.json | 1 + pnpm-lock.yaml | 8 ++++++ src/commands/team/index.ts | 2 +- src/commands/team/link.ts | 53 +++++++++++++++++++++++++++++++++++--- 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index e9af11c..397a3f6 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "license": "ISC", "dependencies": { "@prisma/client": "5.17.0", + "country-code-lookup": "^0.1.3", "discord.js": "^14.15.3", "tesseract.js": "^5.1.0", "tsx": "^3.14.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0ace19f..5e7e989 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ importers: '@prisma/client': specifier: 5.17.0 version: 5.17.0(prisma@5.18.0) + country-code-lookup: + specifier: ^0.1.3 + version: 0.1.3 discord.js: specifier: ^14.15.3 version: 14.15.3 @@ -242,6 +245,9 @@ packages: buffer-from@1.1.2: resolution: {integrity: sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==} + country-code-lookup@0.1.3: + resolution: {integrity: sha512-gLu+AQKHUnkSQNTxShKgi/4tYd0vEEait3JMrLNZgYlmIZ9DJLkHUjzXE9qcs7dy3xY/kUx2/nOxZ0Z3D9JE+A==} + discord-api-types@0.37.83: resolution: {integrity: sha512-urGGYeWtWNYMKnYlZnOnDHm8fVRffQs3U0SpE8RHeiuLKb/u92APS8HoQnPTFbnXmY1vVnXjXO4dOxcAn3J+DA==} @@ -537,6 +543,8 @@ snapshots: buffer-from@1.1.2: {} + country-code-lookup@0.1.3: {} + discord-api-types@0.37.83: {} discord.js@14.15.3: diff --git a/src/commands/team/index.ts b/src/commands/team/index.ts index 44fb5bb..9ad4a3a 100644 --- a/src/commands/team/index.ts +++ b/src/commands/team/index.ts @@ -6,4 +6,4 @@ import update from "./update"; import pass from "./pass"; import sync from "./sync"; -export default category("Team", [who, link, create, update, pass, sync]); +export default category("Team", [who, link, create, pass, sync]); diff --git a/src/commands/team/link.ts b/src/commands/team/link.ts index 2cf2ec2..8071a57 100644 --- a/src/commands/team/link.ts +++ b/src/commands/team/link.ts @@ -1,5 +1,6 @@ import { PermissionFlagsBits, SlashCommandBuilder } from "discord.js"; import { command } from "utils"; +import lookup from "country-code-lookup"; const meta = new SlashCommandBuilder() .setName("link") @@ -46,21 +47,35 @@ const meta = new SlashCommandBuilder() ) .setDescription("Choose your username style") .setRequired(false) + ) + .addStringOption((option) => + option.setName("country").setDescription("Your country").setRequired(false) ); export default command(meta, async ({ interaction }) => { + const country = + interaction.options.getString("country")!.charAt(0).toUpperCase() + + interaction.options.getString("country")!.slice(1); + + const look = + lookup.byCountry(country) ?? + lookup.byInternet(country) ?? + lookup.byIso(country); + const info = { User: interaction.user.globalName, Id: interaction.user.id, NexusMods: interaction.options.getString("nexusmods") ?? "None", - GitHub: interaction.options.getString("github") ?? "None", + Github: interaction.options.getString("github") ?? "None", Theme: interaction.options.getString("theme") ?? "default", Description: interaction.options.getString("description") ?? "None", Style: interaction.options.getString("username") ?? "uppercase", + Timezone: "None", + Country: look?.internet ?? "None", }; - const data = fetch(process.env.API_ENDPOINT + "/bot/commands/user", { - method: "PUT", + const data = await fetch(process.env.API_ENDPOINT + "/bot/commands/user", { + method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${process.env.BOT_TOKEN}`, @@ -68,5 +83,35 @@ export default command(meta, async ({ interaction }) => { body: JSON.stringify(info), }); - return interaction.reply({ content: JSON.stringify(info, null, 2) }); + if (!data.ok) { + return interaction.reply({ + content: "Failed to link your account.", + ephemeral: true, + }); + } + + if (!look) { + return interaction.reply({ + embeds: [ + { + title: "Account Linked", + description: + "Your account has been successfully linked/Updated.\nBut we couldn't find your country.\nTry using an ISO Country Code.", + color: 0x00ff00, + }, + ], + ephemeral: true, + }); + } + + return interaction.reply({ + embeds: [ + { + title: "Account Linked", + description: "Your account has been successfully linked/Updated.", + color: 0x00ff00, + }, + ], + ephemeral: true, + }); });