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; +}