From d6351fbc3091e2d80292f3f9ef631ea6044bf26a Mon Sep 17 00:00:00 2001 From: kdh8219 <65698239+kdh8219@users.noreply.github.com> Date: Sat, 29 Apr 2023 16:04:07 +0900 Subject: [PATCH] search --- src/command/commands.ts | 2 + src/command/commands/search.js | 110 --------------------------------- src/command/commands/search.ts | 90 +++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 110 deletions(-) delete mode 100755 src/command/commands/search.js create mode 100755 src/command/commands/search.ts diff --git a/src/command/commands.ts b/src/command/commands.ts index e228ed6..5b47303 100644 --- a/src/command/commands.ts +++ b/src/command/commands.ts @@ -10,6 +10,7 @@ import get_members from "./commands/get_members.js"; import get_raw_file from "./commands/get_raw_file.js"; import mov_blacklist_super from "./commands/mov_blacklist_super.js"; import ping from "./commands/ping.js"; +import search from "./commands/search.js"; const commands: TCommand[] = [ add_nick_super, @@ -22,5 +23,6 @@ const commands: TCommand[] = [ get_raw_file, mov_blacklist_super, ping, + search, ]; export default commands; diff --git a/src/command/commands/search.js b/src/command/commands/search.js deleted file mode 100755 index 2d5ab08..0000000 --- a/src/command/commands/search.js +++ /dev/null @@ -1,110 +0,0 @@ -const { SlashCommandBuilder, Client, GatewayIntentBits } = require("discord.js"); -const client = new Client({ intents: [GatewayIntentBits.Guilds] }); -client.login(process.env.DISCORD_TOKEN); - -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("search") - .setDescription("search info") - .addSubcommand((subcommand) => - subcommand - .setName("by_discord") - .setDescription("search info by discord") - .addUserOption((option) => option.setName("discord").setDescription("discord account").setRequired(true)) - ) - .addSubcommand((subcommand) => - subcommand - .setName("by_minecraft") - .setDescription("search info by minecraft") - .addStringOption((option) => option.setName("minecraft").setDescription("minecraft id").setRequired(true)) - ) - .setDMPermission(false), - async execute(interaction) { - await interaction.deferReply({ ephemeral: true }); - const data = (await firebase.get()).val(); - let is_2k2r = false; - let found = false; - if (interaction.options.getSubcommand() === "by_discord") { - let result = ""; - const searching_discord_id = interaction.options.getUser("discord").id; - const requester_discord_id = interaction.user.id; - for (let discord_search_point in data["members"]) { - const this_discord = data["members"][discord_search_point].discord; - if (this_discord == requester_discord_id) { - is_2k2r = true; - } - if (this_discord == searching_discord_id) { - found = true; - result = data["members"][discord_search_point].minecraft; - } - } - if (is_2k2r) { - if (!found) { - interaction.editReply({ content: `\`에러\`: 해당 디스코드 계정은 등록되지 않았어요!` }); - return; - } - let mcid = ""; - for (let mcuuid in result) { - mcid = mcid + (await mojangAPI.uuidToName(result[mcuuid])).name + ","; - } - interaction.editReply({ content: `${interaction.options.getUser("discord")}:${mcid.slice(0, -1)}` }); - } else { - interaction.editReply({ content: `\`에러\`:하나 이상의 아이디를 등록해야만 합니다. \`/add_nick\`을 살펴보세요.` }); - } - } else { - let minecraft; - try { - minecraft = await mojangAPI.nameToUuid(interaction.options.getString("minecraft")); - } catch (err) { - if (err.message == "204 status code") { - await interaction.editReply({ content: "`에러`: 닉네임 검색을 실패했어요(닉네임을 정확하게 입력했나요?)" }); - return; - } - } - const requester_discord_id = interaction.user.id; - let searched_discord; - let searched_minecraft; - 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) { - searched_minecraft = data["members"][discord_search_point].minecraft; - searched_discord = data["members"][discord_search_point].discord; - found = true; - } - } - if (data["members"][discord_search_point].discord == requester_discord_id) { - is_2k2r = true; - } - } - if (is_2k2r) { - if (!found) { - interaction.editReply({ content: `\`에러\`: 해당 마인크래프트 아이디는 등록되지 않았어요!` }); - return; - } - let mcid = ""; - for (let mcuuid in searched_minecraft) { - mcid = mcid + (await mojangAPI.uuidToName(searched_minecraft[mcuuid])).name + ","; - } - interaction.editReply({ content: `<@${searched_discord}>:${mcid.slice(0, -1)}` }); - } else { - interaction.editReply({ content: `\`에러\`:하나 이상의 아이디를 등록해야만 합니다. \`/add_nick\`을 살펴보세요.` }); - } - } - }, -}; diff --git a/src/command/commands/search.ts b/src/command/commands/search.ts new file mode 100755 index 0000000..73c5190 --- /dev/null +++ b/src/command/commands/search.ts @@ -0,0 +1,90 @@ +import { SlashCommandBuilder, ChatInputCommandInteraction } from "discord.js"; + +import firebase from "../../wrapper/firebase.js"; +import mojangAPI from "../../wrapper/mojang-api.js"; + +export default { + data: new SlashCommandBuilder() + .setName("search") + .setDescription("search info") + .addSubcommand((subcommand) => + subcommand + .setName("by_discord") + .setDescription("search info by discord") + .addUserOption((option) => + option + .setName("discord") + .setDescription("discord account") + .setRequired(true) + ) + ) + .addSubcommand((subcommand) => + subcommand + .setName("by_minecraft") + .setDescription("search info by minecraft") + .addStringOption((option) => + option + .setName("minecraft") + .setDescription("minecraft id") + .setRequired(true) + ) + ) + .setDMPermission(false), + async execute(interaction: ChatInputCommandInteraction) { + const members = firebase.collection("members"); + if ( + (await members.where("discord_id", "==", interaction.user.id).get()).empty + ) { + interaction.editReply({ + content: `\`에러\`:하나 이상의 아이디를 등록해야만 합니다.`, + }); + return; + } + let discord_id; + if (interaction.options.getSubcommand() === "by_minecraft") { + const minecraft_id = interaction.options.getString("minecraft"); + let minecraft_uuid; + try { + minecraft_uuid = await mojangAPI.getUUIDFromId(minecraft_id); + } catch { + await interaction.editReply({ + content: + "`에러`: 마인크래프트 닉네임 검색을 실패했습니다. 마인크래프트 닉네임을 정확하게 입력했나요?", + }); + return; + } + const member = await members + .where("minecraft_uuid", "==", minecraft_uuid) + .get(); + if (member.empty) { + interaction.editReply({ + content: `\`에러\`: 해당 마인크래프트 아이디는 등록되지 않았어요!`, + }); + return; + } + discord_id = member.docs[0].data()["discord_id"]; + } else { + discord_id = interaction.options.getUser("discord").id; + } + + const the_datas = await members.where("discord_id", "==", discord_id).get(); + if (the_datas.empty) { + interaction.editReply({ + content: "`에러`: 해당 멤버는 등록되지 않았어요!", + }); + } + + let text = ""; + text += interaction.guild.members.cache.get(discord_id).nickname; + text += ": "; + for (const user of the_datas.docs) { + const minecraft_uuid = user["minecraft_uuid"]; + text += mojangAPI.getIdFromUUID(minecraft_uuid); + text += ", "; + } + text = text.slice(0, -1); + interaction.editReply({ + content: text, + }); + }, +};