From 79f2042536f630918efbc937feda19b8e5f3edc3 Mon Sep 17 00:00:00 2001 From: kdh8219 <65698239+kdh8219@users.noreply.github.com> Date: Fri, 28 Apr 2023 21:16:38 +0900 Subject: [PATCH] get_blacklist --- src/command/commands.ts | 2 + src/command/commands/get_blacklist.js | 50 ---------------------- src/command/commands/get_blacklist.ts | 61 +++++++++++++++++++++++++++ src/functions.ts | 4 +- 4 files changed, 65 insertions(+), 52 deletions(-) delete mode 100755 src/command/commands/get_blacklist.js create mode 100755 src/command/commands/get_blacklist.ts diff --git a/src/command/commands.ts b/src/command/commands.ts index 4de0f6b..7c4cc3e 100644 --- a/src/command/commands.ts +++ b/src/command/commands.ts @@ -4,6 +4,7 @@ import add_nick_super from "./commands/add_nick_super.js"; 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 ping from "./commands/ping.js"; const commands: TCommand[] = [ @@ -11,6 +12,7 @@ const commands: TCommand[] = [ add_nick, del_nick_super, del_user_super, + get_blacklist, ping, ]; export default commands; diff --git a/src/command/commands/get_blacklist.js b/src/command/commands/get_blacklist.js deleted file mode 100755 index 1dfb250..0000000 --- a/src/command/commands/get_blacklist.js +++ /dev/null @@ -1,50 +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("get_blacklist").setDescription("show blicklist").setDMPermission(false), - async execute(interaction) { - await interaction.deferReply({ ephemeral: true }); - const data = (await firebase.get()).val(); - for (let discord_search_point in data["members"]) { - if (data["members"][discord_search_point].discord == interaction.user.id) { - //이미 등록된 디코일경우 - let message = ""; - for (let discord_say_point in data["blacklist"]) { - let discord_tag; - try { - const discord = await client.users.fetch(data["blacklist"][discord_say_point].discord); - discord_tag = `${discord.username}#${discord.discriminator}`; - } catch (e) { - discord_tag = `Deleted User#0000`; - } - - message = `${message}${discord_tag}(${data["blacklist"][discord_say_point].discord}):`; - for (let minecraft_say_point in data["blacklist"][discord_say_point].minecraft) { - let minecraft = await mojangAPI.uuidToName(data["blacklist"][discord_say_point].minecraft[minecraft_say_point]); - message = `${message}${minecraft.name},`; - } - message = `${message.slice(0, -1)}\n`; - } - const userInfoFile = { attachment: Buffer.from(message), name: "data.txt" }; - await interaction.editReply({ files: [userInfoFile] }); - return; - } - } //신규 디코일 경우 - await interaction.editReply({ content: `\`에러\`:하나 이상의 아이디를 등록해야만 합니다. \`/add_nick\`을 살펴보세요.` }); - }, -}; diff --git a/src/command/commands/get_blacklist.ts b/src/command/commands/get_blacklist.ts new file mode 100755 index 0000000..8e33403 --- /dev/null +++ b/src/command/commands/get_blacklist.ts @@ -0,0 +1,61 @@ +import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js"; + +import firebase from "../../wrapper/firebase.js"; +import { TUser } from "../../functions.js"; +import mojangAPI from "../../wrapper/mojang-api.js"; + +export default { + data: new SlashCommandBuilder() + .setName("get_blacklist") + .setDescription("show blicklist") + .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 blacklist_data: Map = new Map(); + (await blacklist.get()).forEach((doc) => { + const user = doc.data() as TUser; + const data_get = blacklist_data.get(user.discord_id); + if (!data_get) { + blacklist_data.set(user.discord_id, [user.minecraft_uuid]); + } else { + data_get.push(user.minecraft_uuid); + blacklist_data.set(user.discord_id, data_get); + } + }); + + let text = ""; + blacklist_data.forEach(async (minecraft_uuids, discord_id) => { + let discord_tag: string; + try { + discord_tag = (await interaction.client.users.fetch(discord_id)).tag; + } catch (e) { + discord_tag = `Deleted User#0000`; + } + text += discord_tag; + + text += " : "; + minecraft_uuids.forEach(async (minecraft_uuid) => { + text += await mojangAPI.getIdFromUUID(minecraft_uuid); + text += ", "; + }); + text = text.slice(0, text.length - 2); + }); + + const buffer_file = { + attachment: Buffer.from(text), + name: "blacklist.txt", + }; + await interaction.editReply({ files: [buffer_file] }); + }, +}; diff --git a/src/functions.ts b/src/functions.ts index 7cd2b51..3f67454 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -15,8 +15,8 @@ export function getCommands(): Collection { } export type TUser = { - discord: string; // id - minecraft: string[]; //uuid + discord_id: string; // id + minecraft_uuid: string; //uuid }; // export type TData = {