From f73ce8254d2668e8c0b9de3d46cb7a7be37f7979 Mon Sep 17 00:00:00 2001 From: kdh8219 <65698239+kdh8219@users.noreply.github.com> Date: Sat, 10 Jun 2023 21:00:42 +0900 Subject: [PATCH] =?UTF-8?q?Discord=20new=20username=20=EB=8C=80=EC=9D=91?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80=EC=A0=81=EC=9C=BC=EB=A1=9C=20get=5Fmember?= =?UTF-8?q?s=EC=97=90=EC=84=9C=20=EC=93=B0=EB=8D=98=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=EC=9D=84=20=ED=95=A8=EC=88=98=EB=A1=9C=20=EB=B9=BC=20search?= =?UTF-8?q?=EC=97=90=EC=84=9C=EB=8F=84=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/command/commands/get_members.ts | 32 +++------------------- src/command/commands/search.ts | 16 ++++------- src/functions/data_to_string.ts | 41 +++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 39 deletions(-) create mode 100644 src/functions/data_to_string.ts diff --git a/src/command/commands/get_members.ts b/src/command/commands/get_members.ts index 2aa7a4b..26d0dc4 100755 --- a/src/command/commands/get_members.ts +++ b/src/command/commands/get_members.ts @@ -4,6 +4,7 @@ import firebase from "../../wrapper/firebase.js"; import mojangAPI from "../../wrapper/mojang-api.js"; import { TUser } from "../../types.js"; import { dm_slice } from "../../functions/dm_slice.js"; +import { data_to_string } from "../../functions/data_to_string.js"; export default { data: new SlashCommandBuilder() @@ -35,36 +36,11 @@ export default { }); let text = ""; - for (const member of member_data) { - let nickname: string; - try { - nickname = - (await interaction.guild.members.fetch(member[0])).nickname || - (await interaction.client.users.fetch(member[0])).username; - } catch (e) { - nickname = ` `; - } - let tag: string; - try { - tag = (await interaction.client.users.fetch(member[0])).tag; - } catch (e) { - tag = `Deleted User#0000`; - } - text += "- "; - text += `\`${nickname}\``; - text += `【\`${tag}\`】`; - text += `[${member[0]}]`; - - text += "\n"; - for (const minecraft_uuid of member[1]) { - text += " - "; - text += `\`${await mojangAPI.getIdFromUUID(minecraft_uuid)}\``; - text += `[${minecraft_uuid}]`; - text += "\n"; - } - text = text.slice(0, -1); + for (const [discord_id, minecraft_uuids] of member_data) { + text += await data_to_string(interaction, discord_id, minecraft_uuids); text += "\n\n"; } + const sliced = dm_slice(text); for (const chunk of sliced) { await interaction.user.send(chunk); diff --git a/src/command/commands/search.ts b/src/command/commands/search.ts index 13a3f3c..54e3a1f 100755 --- a/src/command/commands/search.ts +++ b/src/command/commands/search.ts @@ -2,6 +2,7 @@ import { SlashCommandBuilder, ChatInputCommandInteraction } from "discord.js"; import firebase from "../../wrapper/firebase.js"; import mojangAPI from "../../wrapper/mojang-api.js"; +import { data_to_string } from "../../functions/data_to_string.js"; export default { data: new SlashCommandBuilder() @@ -71,21 +72,14 @@ export default { if (the_data.empty) { await interaction.editReply({ content: "`에러`: 해당 멤버는 등록되지 않았어요!", - }); + }); return; } - let text = ""; - text += - (await interaction.guild.members.fetch(discord_id)).nickname || - (await interaction.client.users.fetch(discord_id)).username; - text += ": "; + const minecraft_uuids: Array = []; for (const user of the_data.docs) { - const minecraft_uuid = user.data()["minecraft_uuid"]; - text += await mojangAPI.getIdFromUUID(minecraft_uuid); - text += ` [${minecraft_uuid}]`; - text += ", "; + minecraft_uuids.push(user.data()["minecraft_uuid"]); } - text = text.slice(0, -2); + const text = await data_to_string(interaction, discord_id, minecraft_uuids); await interaction.editReply({ content: text, }); diff --git a/src/functions/data_to_string.ts b/src/functions/data_to_string.ts new file mode 100644 index 0000000..3966174 --- /dev/null +++ b/src/functions/data_to_string.ts @@ -0,0 +1,41 @@ +import { ChatInputCommandInteraction } from "discord.js"; + +import mojangAPI from "../wrapper/mojang-api"; + +export async function data_to_string( + interaction: ChatInputCommandInteraction, + discord_id: string, + minecraft_uuids: string[] +) { + let text = ""; + let nickname: string; + let tag: string; + try { + const guild_user = await interaction.guild.members.fetch(discord_id); + if (guild_user) { + } + const client_user = await interaction.client.users.fetch(discord_id); + tag = + client_user.tag.split("#")[1] != "0" + ? client_user.tag + : "@" + client_user.tag.split("#")[0]; + nickname = guild_user.nickname || client_user.username; + } catch (e) { + nickname = ` `; + tag = `Deleted User#0000`; + } + text += "- "; + text += `\`${nickname}\``; + text += `【\`${tag}\`】`; + text += `[${discord_id}}]`; + + text += "\n"; + for (const minecraft_uuid of minecraft_uuids) { + text += " - "; + text += `\`${await mojangAPI.getIdFromUUID(minecraft_uuid)}\``; + text += `[${minecraft_uuid}]`; + text += "\n"; + } + text = text.slice(0, -1); + return text; +}