From 438136b43afde660bc574a28a234f24a29e0a128 Mon Sep 17 00:00:00 2001 From: kdh8219 <65698239+kdh8219@users.noreply.github.com> Date: Fri, 28 Apr 2023 22:13:06 +0900 Subject: [PATCH] get_file --- src/command/commands.ts | 2 ++ src/command/commands/get_file.js | 49 -------------------------------- src/command/commands/get_file.ts | 46 ++++++++++++++++++++++++++++++ src/wrapper/firebase.ts | 3 ++ 4 files changed, 51 insertions(+), 49 deletions(-) delete mode 100755 src/command/commands/get_file.js create mode 100755 src/command/commands/get_file.ts diff --git a/src/command/commands.ts b/src/command/commands.ts index 7c4cc3e..f737249 100644 --- a/src/command/commands.ts +++ b/src/command/commands.ts @@ -5,6 +5,7 @@ import add_nick from "./commands/add_nick.js"; import del_nick_super from "./commands/del_nick_super.js"; import del_user_super from "./commands/del_user_super.js"; import get_blacklist from "./commands/get_blacklist.js"; +import get_file from "./commands/get_file.js"; import ping from "./commands/ping.js"; const commands: TCommand[] = [ @@ -13,6 +14,7 @@ const commands: TCommand[] = [ del_nick_super, del_user_super, get_blacklist, + get_file, ping, ]; export default commands; diff --git a/src/command/commands/get_file.js b/src/command/commands/get_file.js deleted file mode 100755 index f92310c..0000000 --- a/src/command/commands/get_file.js +++ /dev/null @@ -1,49 +0,0 @@ -const { SlashCommandBuilder, AttachmentBuilder } = require("discord.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("get_file").setDescription("get the file").setDMPermission(false), - async execute(interaction) { - await interaction.deferReply({ ephemeral: true }); - const data = (await firebase.get()).val(); - let is_registed = false; - - text = "{members:["; - for (let discord_search_point in data["members"]) { - for (let minecraft_search_point in data["members"][discord_search_point].minecraft) { - text = text + "'" + data["members"][discord_search_point].minecraft[minecraft_search_point] + "',"; - } - if (data["members"][discord_search_point].discord == interaction.user.id) { - is_registed = true; - } - } - text = text.slice(0, -1) + "], blacklist:["; - - for (let discord_search_point in data["blacklist"]) { - for (let minecraft_search_point in data["blacklist"][discord_search_point].minecraft) { - text = text + "'" + data["blacklist"][discord_search_point].minecraft[minecraft_search_point] + "',"; - } - } - text = text.slice(0, -1) + "]}"; - - if (is_registed) { - const userInfoFile = { attachment: Buffer.from(text), name: "data.json" }; - await interaction.editReply({ content: `*** 절대 이 파일을 공유하지 마세요***`, files: [userInfoFile] }); - } else { - await interaction.editReply({ content: `\`에러\`:하나 이상의 아이디를 등록해야만 합니다. \`/add_nick\`을 살펴보세요.` }); - } - }, -}; diff --git a/src/command/commands/get_file.ts b/src/command/commands/get_file.ts new file mode 100755 index 0000000..092bf19 --- /dev/null +++ b/src/command/commands/get_file.ts @@ -0,0 +1,46 @@ +import { SlashCommandBuilder, ChatInputCommandInteraction } from "discord.js"; + +import firebase from "../../wrapper/firebase.js"; +import { TUser } from "../../functions.js"; + +export default { + data: new SlashCommandBuilder() + .setName("get_file") + .setDescription("get the file") + .setDMPermission(false), + async execute(interaction: ChatInputCommandInteraction) { + const discord_id = interaction.user.id; + + const members = firebase.collection("members"); + const blacklist = firebase.collection("blacklist"); + + if ((await members.where("discord_id", "==", discord_id).get()).empty) { + interaction.editReply({ + content: "`에러`: 하나 이상의 아이디를 등록해야 합니다.", + }); + return; + } + + let data: { + blacklist: string[]; + members: string[]; + } = { blacklist: [], members: [] }; + (await blacklist.get()).forEach((doc) => { + const user = doc.data() as TUser; + data.blacklist.push(user.minecraft_uuid); + }); + (await members.get()).forEach((doc) => { + const user = doc.data() as TUser; + data.members.push(user.minecraft_uuid); + }); + + const buffered_file = { + attachment: Buffer.from(JSON.stringify(data)), + name: "data.json", + }; + await interaction.editReply({ + content: `*** 절대 이 파일을 공유하지 마세요***`, + files: [buffered_file], + }); + }, +}; diff --git a/src/wrapper/firebase.ts b/src/wrapper/firebase.ts index 3c3125c..f2932bc 100644 --- a/src/wrapper/firebase.ts +++ b/src/wrapper/firebase.ts @@ -9,6 +9,9 @@ initializeApp({ clientEmail: process.env.FIREBASE_CERT_CLIENT_EMAIL, privateKey: process.env.FIREBASE_CERT_PRIVATE_KEY?.replace(/\\n/g, "\n"), }), + databaseAuthVariableOverride: { + uid: process.env.FIREBASE_UID, + }, }); const db = getFirestore();