From 9cbdc72dbc66dac41defd4723f7abbe0d70e69a5 Mon Sep 17 00:00:00 2001 From: kdh8219 <65698239+kdh8219@users.noreply.github.com> Date: Wed, 26 Apr 2023 21:31:43 +0900 Subject: [PATCH] add_nick firestore and typescript --- src/command/commands.ts | 3 +- src/command/commands/add_nick.js | 90 -------------------------------- src/command/commands/add_nick.ts | 66 +++++++++++++++++++++++ 3 files changed, 68 insertions(+), 91 deletions(-) delete mode 100755 src/command/commands/add_nick.js create mode 100755 src/command/commands/add_nick.ts diff --git a/src/command/commands.ts b/src/command/commands.ts index 57899fe..752a151 100644 --- a/src/command/commands.ts +++ b/src/command/commands.ts @@ -1,6 +1,7 @@ import { TCommand } from "../functions.js"; import add_nick_super from "./commands/add_nick_super.js"; +import add_nick from "./commands/add_nick.js"; -const commands: TCommand[] = [add_nick_super]; +const commands: TCommand[] = [add_nick_super, add_nick]; export default commands; diff --git a/src/command/commands/add_nick.js b/src/command/commands/add_nick.js deleted file mode 100755 index 194afd9..0000000 --- a/src/command/commands/add_nick.js +++ /dev/null @@ -1,90 +0,0 @@ -const { SlashCommandBuilder } = require("discord.js"); -const mojangAPI = new (require("mojang-api-js"))(); - -const firebase_admin = require("firebase-admin"); -const { getDatabase } = require("firebase-admin/database"); -const serviceAccount = require("../firebase/conf.json"); -if (!firebase_admin.apps.length) { - firebase_admin.initializeApp({ - credential: firebase_admin.credential.cert(serviceAccount), - databaseURL: process.env.FIREBASE_URL, - databaseAuthVariableOverride: { - uid: process.env.FIREBASE_UID, - }, - }); -} -const firebase = getDatabase().ref("/"); - -module.exports = { - data: new SlashCommandBuilder() - .setName("add_nick") - .setDescription("Add your nickname on the list and share it to 2k2rs") - .addStringOption((option) => option.setName("minecraft_id").setDescription("id of the minecraft account").setRequired(true)) - .setDMPermission(false), - async execute(interaction) { - await interaction.deferReply({ ephemeral: true }); - let minecraft; - try { - minecraft = await mojangAPI.nameToUuid(interaction.options.getString("minecraft_id")); - } catch (err) { - if (err.message == "204 status code") { - await interaction.editReply({ - content: "`에러`: 닉네임 검색을 실패했어요(닉네임을 정확하게 입력했나요?)", - ephemeral: true, - }); - return; - } - } - - let data = (await firebase.get()).val(); - - for (let discord_search_point in data["blacklist"]) { - if (data["blacklist"][discord_search_point].discord == interaction.user.id) { - await interaction.editReply({ - content: "`에러`:해당 디스코드 아이디는 블랙리스팅 되었습니다! ~~2k2r에서 꺼져주세요!~~", - ephemeral: true, - }); - return; - } - for (let minecraft_search_point in data["blacklist"][discord_search_point].minecraft) { - if (data["blacklist"][discord_search_point].minecraft[minecraft_search_point] == minecraft.id) { - await interaction.editReply({ - content: "`에러`:해당 마인크래프트 아이디는 블랙리스팅 되었습니다!", - ephemeral: true, - }); - return; - } - } - } - - for (let discord_search_point in data["members"]) { - for (let minecraft_search_point in data["members"][discord_search_point].minecraft) { - if (data["members"][discord_search_point].minecraft[minecraft_search_point] == minecraft.id) { - await interaction.editReply({ - content: "`에러`:해당 마인크래프트 아이디는 이미 등록되었습니다!", - ephemeral: true, - }); - return; - } - } //신규 마크일 경우 - if (data["members"][discord_search_point].discord == interaction.user.id) { - await interaction.editReply({ - content: `${minecraft.name}(${minecraft.id})이 성공적으로 부계정으로 등록되었습니다!`, - ephemeral: true, - }); - data["members"][discord_search_point].minecraft.push(minecraft.id); - await firebase.set(data); - return; - } - } //신규 디코일 경우 - await interaction.editReply({ - content: `${minecraft.name}(${minecraft.id})님 2k2r에 오신것을 환영합니다!`, - ephemeral: true, - }); - data["members"].push({ - discord: interaction.user.id, - minecraft: [minecraft.id], - }); - await firebase.set(data); - }, -}; diff --git a/src/command/commands/add_nick.ts b/src/command/commands/add_nick.ts new file mode 100755 index 0000000..5bea1f1 --- /dev/null +++ b/src/command/commands/add_nick.ts @@ -0,0 +1,66 @@ +import { SlashCommandBuilder, ChatInputCommandInteraction } from "discord.js"; +import mojangAPI from "../../wrapper/mojang-api.js"; +import firebase from "../../wrapper/firebase.js"; + +export default { + data: new SlashCommandBuilder() + .setName("add_nick") + .setDescription("Add your nickname on the list and share it to 2k2rs") + .addStringOption((option) => + option + .setName("minecraft_id") + .setDescription("id of the minecraft account") + .setRequired(true) + ) + .setDMPermission(false), + async execute(interaction: ChatInputCommandInteraction) { + const discord_id = interaction.user.id; + const mcid = interaction.options.getString("minecraft_id") as string; + let mcuuid: string; + try { + mcuuid = await mojangAPI.getUUIDFromId(mcid); + } catch { + await interaction.editReply({ + content: + "`에러`: 마인크래프트 닉네임 검색을 실패했습니다. 마인크래프트 닉네임을 정확하게 입력했나요?", + }); + return; + } + + const members = firebase.collection("members"); + const blacklist = firebase.collection("blacklist"); + + if (!(await blacklist.where("discord_id", "==", discord_id).get()).empty) { + interaction.editReply({ + content: "`에러`: 해당 디스코드 아이디는 블랙리스트 되었습니다.", + }); + return; + } + if (!(await blacklist.where("minecraft_uuid", "==", mcuuid).get()).empty) { + interaction.editReply({ + content: "`에러`: 해당 마인크래프트 계정은 블랙리스트 되었습니다.", + }); + return; + } + if (!(await members.where("minecraft_uuid", "==", mcuuid).get()).empty) { + interaction.editReply({ + content: "`에러`: 해당 마인크래프트 아이디는 이미 등록되었습니다.", + }); + return; + } + + if (!(await members.where("discord_id", "==", discord_id).get()).empty) { + await interaction.editReply({ + content: `${mcid}(${mcuuid})님 2k2r에 오신것을 환영합니다!`, + }); + } else { + await interaction.editReply({ + content: `${mcid}(${mcuuid})이 성공적으로 부계정으로 등록되었습니다!`, + }); + } + await members.add({ + discord_id, + minecraft_uuid: mcuuid, + }); + }, +};