From c099eccbf8b0596037f1bbab5395aebcdb909733 Mon Sep 17 00:00:00 2001 From: kdh8219 <65698239+kdh8219@users.noreply.github.com> Date: Sat, 29 Apr 2023 18:24:27 +0900 Subject: [PATCH] edit message form --- src/command/commands/get_members.ts | 39 ++++++++++++++++++----------- src/functions.ts | 30 ++++++++++++++++++++++ 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/command/commands/get_members.ts b/src/command/commands/get_members.ts index 7161117..b713337 100755 --- a/src/command/commands/get_members.ts +++ b/src/command/commands/get_members.ts @@ -2,7 +2,7 @@ import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js"; import firebase from "../../wrapper/firebase.js"; import mojangAPI from "../../wrapper/mojang-api.js"; -import { TUser } from "../../functions.js"; +import { TUser, dmSlice } from "../../functions.js"; export default { data: new SlashCommandBuilder() @@ -37,25 +37,34 @@ export default { for (const member of member_data) { let nickname: string; try { - nickname = interaction.guild.members.cache.get(discord_id).nickname; + nickname = (await interaction.guild.members.fetch(member[0])).nickname; } catch (e) { - nickname = `Deleted User`; + nickname = ``; } - text += nickname; - text += `(${discord_id})`; + let tag: string; + try { + tag = (await interaction.client.users.fetch(member[0])).tag; + } catch (e) { + tag = `Deleted User#0000`; + } + text += `\`${nickname}\``; + text += `【\`${tag}\`】`; + text += `[${discord_id}]`; - text += " : "; + text += "\n"; for (const minecraft_uuid of member[1]) { - text += await mojangAPI.getIdFromUUID(minecraft_uuid); - text += ", "; + text += " - "; + text += `\`${await mojangAPI.getIdFromUUID(minecraft_uuid)}\``; + text += `[${minecraft_uuid}]`; + text += "\n"; } - text = text.slice(0, text.length - 2); + text = text.slice(0, -1); + text += "\n\n"; } - - const buffer_file = { - attachment: Buffer.from(text), - name: "members.txt", - }; - await interaction.editReply({ files: [buffer_file] }); + const sliced = dmSlice(text); + for (const chunc of sliced) { + await interaction.user.send(chunc); + } + await interaction.editReply("dm을 확인해주세요."); }, }; diff --git a/src/functions.ts b/src/functions.ts index 8a83e81..f05f56c 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -22,3 +22,33 @@ export type TUser = { discord_id: string; // id minecraft_uuid: string; //uuid }; + +export function dmSlice(raw: string): string[] { + const output: string[] = []; + function slasher(txt: string): { front: string; end: string } { + if (txt.length <= 2000) { + return { + front: txt, + end: "", + }; + } + let front = txt.slice(0, 2000); + let end = txt.slice(2000, txt.length); + if (end) { + let tolast = front.slice(front.lastIndexOf("\n"), -1); + front = front.slice(0, front.lastIndexOf("\n")); + end = tolast + end; + } + return { front, end }; + } + + let slashed = slasher(raw); + while (true) { + output.push(slashed.front); + if (slashed.end) { + slashed = slasher(slashed.end); + } else { + return output; + } + } +}