From 36b5e30ce43718f147ad9940918ec679c3cdafb5 Mon Sep 17 00:00:00 2001 From: kdh8219 <65698239+kdh8219@users.noreply.github.com> Date: Sat, 10 Jun 2023 20:25:49 +0900 Subject: [PATCH] =?UTF-8?q?functions.ts=20=EC=AA=BC=EA=B0=9C=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/command/commands/add_nick.ts | 2 +- src/command/commands/add_nick_super.ts | 2 +- src/command/commands/del_nick_super.ts | 39 +++---- src/command/commands/del_user_super.ts | 34 +++--- src/command/commands/get_blacklist.ts | 2 +- src/command/commands/get_file.ts | 2 +- src/command/commands/get_members.ts | 30 +---- src/command/commands/get_raw_file.ts | 2 +- src/command/commands/mov_blacklist_super.ts | 37 +++--- src/functions.ts | 123 -------------------- src/functions/add_user.ts | 78 +++++++++++++ src/functions/dm_slice.ts | 24 ++++ src/functions/send_embed.ts | 11 ++ src/main.ts | 53 ++++++--- src/types.d.ts | 4 + 15 files changed, 212 insertions(+), 231 deletions(-) delete mode 100644 src/functions.ts create mode 100644 src/functions/add_user.ts create mode 100644 src/functions/dm_slice.ts create mode 100644 src/functions/send_embed.ts create mode 100644 src/types.d.ts diff --git a/src/command/commands/add_nick.ts b/src/command/commands/add_nick.ts index d61fb0d..853d74d 100755 --- a/src/command/commands/add_nick.ts +++ b/src/command/commands/add_nick.ts @@ -1,7 +1,7 @@ import { SlashCommandBuilder, ChatInputCommandInteraction } from "discord.js"; import mojangAPI from "../../wrapper/mojang-api.js"; import firebase from "../../wrapper/firebase.js"; -import { add_user } from "../../functions.js"; +import { add_user } from "../../functions/add_user.js"; export default { data: new SlashCommandBuilder() diff --git a/src/command/commands/add_nick_super.ts b/src/command/commands/add_nick_super.ts index 3261a03..5ef56b0 100755 --- a/src/command/commands/add_nick_super.ts +++ b/src/command/commands/add_nick_super.ts @@ -6,7 +6,7 @@ import { import mojangAPI from "../../wrapper/mojang-api.js"; import firebase from "../../wrapper/firebase.js"; -import { add_user } from "../../functions.js"; +import { add_user } from "../../functions/add_user.js"; export default { data: new SlashCommandBuilder() diff --git a/src/command/commands/del_nick_super.ts b/src/command/commands/del_nick_super.ts index 8225afb..39b9795 100755 --- a/src/command/commands/del_nick_super.ts +++ b/src/command/commands/del_nick_super.ts @@ -7,7 +7,7 @@ import { import mojangAPI from "../../wrapper/mojang-api.js"; import firebase from "../../wrapper/firebase.js"; -import { embed_to_channel } from "../../functions.js"; +import { send_embed } from "../../functions/send_embed.js"; export default { data: new SlashCommandBuilder() @@ -64,26 +64,23 @@ export default { content: `삭제 완료:${discord_tag}(${discord_id})에게서 ${minecraft_id}(${minecraft_uuid})를 제거했습니다.`, }); - const embed = new EmbedBuilder() - .setTitle("Nick deleted") - .setColor(0x0099ff) - .setFields([ - { - name: "Command sender", - value: `${interaction.user.tag}(${interaction.user.id})`, - }, - { name: " ", value: " " }, - { name: "Targrt Discord Id", value: discord_id }, - { name: " ", value: " " }, - { name: "Minecraft Id", value: minecraft_id }, - { name: "Minecraft Uuid", value: minecraft_uuid }, - ]) - .setTimestamp(interaction.createdAt); - await embed_to_channel( - interaction.client, - process.env.LOG_CHANNEL_ID, - embed - ); + await send_embed(interaction.client, process.env.LOG_CHANNEL_ID, [ + new EmbedBuilder() + .setTitle("Nick deleted") + .setColor(0x0099ff) + .setFields([ + { + name: "Command sender", + value: `${interaction.user.tag}(${interaction.user.id})`, + }, + { name: " ", value: " " }, + { name: "Targrt Discord Id", value: discord_id }, + { name: " ", value: " " }, + { name: "Minecraft Id", value: minecraft_id }, + { name: "Minecraft Uuid", value: minecraft_uuid }, + ]) + .setTimestamp(interaction.createdAt), + ]); } else { await interaction.editReply({ content: `\`에러\`: 해당 계정이 없습니다.`, diff --git a/src/command/commands/del_user_super.ts b/src/command/commands/del_user_super.ts index 689c4d6..74196fd 100755 --- a/src/command/commands/del_user_super.ts +++ b/src/command/commands/del_user_super.ts @@ -6,7 +6,7 @@ import { } from "discord.js"; import firebase from "../../wrapper/firebase.js"; -import { embed_to_channel } from "../../functions.js"; +import { send_embed } from "../../functions/send_embed.js"; export default { data: new SlashCommandBuilder() @@ -44,23 +44,21 @@ export default { await interaction.editReply({ content: `삭제 완료:${discord_tag}의 계정을 모두 제거했습니다.`, }); - const embed = new EmbedBuilder() - .setTitle("User deleted") - .setColor(0x0099ff) - .setFields([ - { - name: "Command sender", - value: `${interaction.user.tag}(${interaction.user.id})`, - }, - { name: " ", value: " " }, - { name: "Target Discord Id", value: discord_id }, - ]) - .setTimestamp(interaction.createdAt); - await embed_to_channel( - interaction.client, - process.env.LOG_CHANNEL_ID, - embed - ); + + await send_embed(interaction.client, process.env.LOG_CHANNEL_ID, [ + new EmbedBuilder() + .setTitle("User deleted") + .setColor(0x0099ff) + .setFields([ + { + name: "Command sender", + value: `${interaction.user.tag}(${interaction.user.id})`, + }, + { name: " ", value: " " }, + { name: "Target Discord Id", value: discord_id }, + ]) + .setTimestamp(interaction.createdAt), + ]); return; } else { await interaction.editReply({ diff --git a/src/command/commands/get_blacklist.ts b/src/command/commands/get_blacklist.ts index 18dc08e..dbb9b13 100755 --- a/src/command/commands/get_blacklist.ts +++ b/src/command/commands/get_blacklist.ts @@ -1,7 +1,7 @@ import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js"; import firebase from "../../wrapper/firebase.js"; -import { TUser } from "../../functions.js"; +import { TUser } from "../../types.js"; import mojangAPI from "../../wrapper/mojang-api.js"; export default { diff --git a/src/command/commands/get_file.ts b/src/command/commands/get_file.ts index e3d4d43..ac67ff0 100755 --- a/src/command/commands/get_file.ts +++ b/src/command/commands/get_file.ts @@ -1,7 +1,7 @@ import { SlashCommandBuilder, ChatInputCommandInteraction } from "discord.js"; import firebase from "../../wrapper/firebase.js"; -import { TUser } from "../../functions.js"; +import { TUser } from "../../types.js"; export default { data: new SlashCommandBuilder() diff --git a/src/command/commands/get_members.ts b/src/command/commands/get_members.ts index 928c60e..2aa7a4b 100755 --- a/src/command/commands/get_members.ts +++ b/src/command/commands/get_members.ts @@ -2,7 +2,8 @@ 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 } from "../../types.js"; +import { dm_slice } from "../../functions/dm_slice.js"; export default { data: new SlashCommandBuilder() @@ -64,35 +65,10 @@ export default { text = text.slice(0, -1); text += "\n\n"; } - const sliced = dmSlice(text); + const sliced = dm_slice(text); for (const chunk of sliced) { await interaction.user.send(chunk); } await interaction.editReply("dm을 확인해주세요."); }, }; - -function dmSlice(raw: string): string[] { - function slasher(txt: string): { front: string; end: string } { - let front = txt.slice(0, 2000); - let end = txt.slice(2000); - if (end) { - const IndexOfLastBlock = front.lastIndexOf("\n\n"); - end = "ㅤ" /* 공백문자 */ + front.slice(IndexOfLastBlock + 1) + end; - front = front.slice(0, IndexOfLastBlock); - } - return { front, end }; - } - - const output: string[] = []; - - let slashed = slasher(raw); - while (true) { - output.push(slashed.front); - if (slashed.end) { - slashed = slasher(slashed.end); - } else { - return output; - } - } -} diff --git a/src/command/commands/get_raw_file.ts b/src/command/commands/get_raw_file.ts index 99f6800..115a828 100755 --- a/src/command/commands/get_raw_file.ts +++ b/src/command/commands/get_raw_file.ts @@ -5,7 +5,7 @@ import { } from "discord.js"; import firebase from "../../wrapper/firebase.js"; -import { TUser } from "../../functions.js"; +import { TUser } from "../../types.js"; export default { data: new SlashCommandBuilder() diff --git a/src/command/commands/mov_blacklist_super.ts b/src/command/commands/mov_blacklist_super.ts index 9147edb..00b548f 100755 --- a/src/command/commands/mov_blacklist_super.ts +++ b/src/command/commands/mov_blacklist_super.ts @@ -6,7 +6,8 @@ import { } from "discord.js"; import firebase from "../../wrapper/firebase.js"; -import { TUser, embed_to_channel } from "../../functions.js"; +import { TUser } from "../../types.js"; +import { send_embed } from "../../functions/send_embed.js"; export default { data: new SlashCommandBuilder() @@ -54,23 +55,21 @@ export default { UUIDs += uuid; UUIDs += "\n"; }); - const embed = new EmbedBuilder() - .setTitle("User blacklisted") - .setColor(0x0099ff) - .setFields([ - { - name: "Command sender", - value: `${interaction.user.tag}(${interaction.user.id})`, - }, - { name: " ", value: " " }, - { name: "Target Discord Id", value: discord_id }, - { name: "UUIDs", value: UUIDs }, - ]) - .setTimestamp(interaction.createdAt); - await embed_to_channel( - interaction.client, - process.env.LOG_CHANNEL_ID, - embed - ); + + await send_embed(interaction.client, process.env.LOG_CHANNEL_ID, [ + new EmbedBuilder() + .setTitle("User blacklisted") + .setColor(0x0099ff) + .setFields([ + { + name: "Command sender", + value: `${interaction.user.tag}(${interaction.user.id})`, + }, + { name: " ", value: " " }, + { name: "Target Discord Id", value: discord_id }, + { name: "UUIDs", value: UUIDs }, + ]) + .setTimestamp(interaction.createdAt), + ]); }, }; diff --git a/src/functions.ts b/src/functions.ts deleted file mode 100644 index 425e304..0000000 --- a/src/functions.ts +++ /dev/null @@ -1,123 +0,0 @@ -import { - ChatInputCommandInteraction, - Client, - EmbedBuilder, - Guild, -} from "discord.js"; -import { Firestore } from "firebase-admin/firestore"; - -import { MojangAPI } from "./wrapper/mojang-api.js"; -import firebase from "./wrapper/firebase.js"; - -export type TUser = { - discord_id: string; // id - minecraft_uuid: string; //uuid -}; - -export async function add_user( - interaction: ChatInputCommandInteraction, - discord_id: string, - mcid: string, - mojangAPI: MojangAPI, - firebase: Firestore -) { - let mcuuid: string; - try { - mcuuid = await mojangAPI.getUUIDFromId(mcid); - } catch { - await interaction.editReply({ - content: - "`에러`: 마인크래프트 닉네임 검색을 실패했습니다. 마인크래프트 닉네임을 정확하게 입력했나요?", - }); - return; - } - - const members = firebase.collection("members"); - const blacklist = firebase.collection("blacklist"); - - if (!(await blacklist.where("discord_id", "==", discord_id).get()).empty) { - await interaction.editReply({ - content: "`에러`: 해당 디스코드 아이디는 블랙리스트 되었습니다.", - }); - return; - } - if (!(await blacklist.where("minecraft_uuid", "==", mcuuid).get()).empty) { - await interaction.editReply({ - content: "`에러`: 해당 마인크래프트 계정은 블랙리스트 되었습니다.", - }); - return; - } - if (!(await members.where("minecraft_uuid", "==", mcuuid).get()).empty) { - await interaction.editReply({ - content: "`에러`: 해당 마인크래프트 아이디는 이미 등록되었습니다.", - }); - return; - } - - if ((await members.where("discord_id", "==", discord_id).get()).empty) { - await interaction.editReply({ - content: `${mcid}(${mcuuid})님 2k2r에 오신것을 환영합니다!`, - }); - } else { - await interaction.editReply({ - content: `${mcid}(${mcuuid})이 성공적으로 부계정으로 등록되었습니다!`, - }); - } - await members.add({ - discord_id, - minecraft_uuid: mcuuid, - }); - - const embed = new EmbedBuilder() - .setTitle("User Added") - .setColor(0x0099ff) - .addFields([ - { - name: "Command sender", - value: `${interaction.user.tag}(${interaction.user.id})`, - }, - { name: " ", value: " " }, - { name: "Discord Id", value: discord_id }, - { name: " ", value: " " }, - { name: "Minecraft Id", value: mcid }, - { name: "Minecraft Uuid", value: mcuuid }, - ]) - .setTimestamp(interaction.createdAt); - await embed_to_channel(interaction.client, process.env.LOG_CHANNEL_ID, embed); -} - -export async function embed_to_channel( - client: Client, - channel_id: string, - embed: EmbedBuilder -) { - const channel = await client.channels.fetch(channel_id); - if (!channel.isTextBased()) return; - channel.send({ embeds: [embed] }); -} - -export async function delete_members_who_left( - client: Client, - guild: Guild -) { - const members_collection = firebase.collection("members"); - const members = await members_collection.get(); - for (const doc of members.docs) { - const data = doc.data() as TUser; - try { - await guild.members.fetch(data.discord_id); - } catch { - doc.ref.delete(); - - const embed = new EmbedBuilder() - .setTitle("물갈이") - .setColor(0x0099ff) - .setFields([ - { name: "Discord Id", value: data.discord_id }, - { name: "Minecraft Uuid", value: data.minecraft_uuid }, - ]) - .setTimestamp(new Date()); - await embed_to_channel(client, process.env.LOG_CHANNEL_ID, embed); - } - } -} diff --git a/src/functions/add_user.ts b/src/functions/add_user.ts new file mode 100644 index 0000000..9775c79 --- /dev/null +++ b/src/functions/add_user.ts @@ -0,0 +1,78 @@ +import { ChatInputCommandInteraction, EmbedBuilder } from "discord.js"; +import { Firestore } from "firebase-admin/firestore"; + +import { MojangAPI } from "./../wrapper/mojang-api.js"; +import { send_embed } from "./send_embed.js"; + +export async function add_user( + interaction: ChatInputCommandInteraction, + discord_id: string, + mcid: string, + mojangAPI: MojangAPI, + firebase: Firestore +) { + let mcuuid: string; + try { + mcuuid = await mojangAPI.getUUIDFromId(mcid); + } catch { + await interaction.editReply({ + content: + "`에러`: 마인크래프트 닉네임 검색을 실패했습니다. 마인크래프트 닉네임을 정확하게 입력했나요?", + }); + return; + } + + const members = firebase.collection("members"); + const blacklist = firebase.collection("blacklist"); + + if (!(await blacklist.where("discord_id", "==", discord_id).get()).empty) { + await interaction.editReply({ + content: "`에러`: 해당 디스코드 아이디는 블랙리스트 되었습니다.", + }); + return; + } + if (!(await blacklist.where("minecraft_uuid", "==", mcuuid).get()).empty) { + await interaction.editReply({ + content: "`에러`: 해당 마인크래프트 계정은 블랙리스트 되었습니다.", + }); + return; + } + if (!(await members.where("minecraft_uuid", "==", mcuuid).get()).empty) { + await interaction.editReply({ + content: "`에러`: 해당 마인크래프트 아이디는 이미 등록되었습니다.", + }); + return; + } + + if ((await members.where("discord_id", "==", discord_id).get()).empty) { + await interaction.editReply({ + content: `${mcid}(${mcuuid})님 2k2r에 오신것을 환영합니다!`, + }); + } else { + await interaction.editReply({ + content: `${mcid}(${mcuuid})이 성공적으로 부계정으로 등록되었습니다!`, + }); + } + await members.add({ + discord_id, + minecraft_uuid: mcuuid, + }); + + await send_embed(interaction.client, process.env.LOG_CHANNEL_ID, [ + new EmbedBuilder() + .setTitle("User Added") + .setColor(0x0099ff) + .addFields([ + { + name: "Command sender", + value: `${interaction.user.tag}(${interaction.user.id})`, + }, + { name: " ", value: " " }, + { name: "Discord Id", value: discord_id }, + { name: " ", value: " " }, + { name: "Minecraft Id", value: mcid }, + { name: "Minecraft Uuid", value: mcuuid }, + ]) + .setTimestamp(interaction.createdAt), + ]); +} diff --git a/src/functions/dm_slice.ts b/src/functions/dm_slice.ts new file mode 100644 index 0000000..ca755a7 --- /dev/null +++ b/src/functions/dm_slice.ts @@ -0,0 +1,24 @@ +export function dm_slice(raw: string): string[] { + function slasher(txt: string): { front: string; end: string } { + let front = txt.slice(0, 2000); + let end = txt.slice(2000); + if (end) { + const IndexOfLastBlock = front.lastIndexOf("\n\n"); + end = "ㅤ" /* 공백문자 */ + front.slice(IndexOfLastBlock + 1) + end; + front = front.slice(0, IndexOfLastBlock); + } + return { front, end }; + } + + const output: string[] = []; + + let slashed = slasher(raw); + while (true) { + output.push(slashed.front); + if (slashed.end) { + slashed = slasher(slashed.end); + } else { + return output; + } + } +} diff --git a/src/functions/send_embed.ts b/src/functions/send_embed.ts new file mode 100644 index 0000000..1ed1f89 --- /dev/null +++ b/src/functions/send_embed.ts @@ -0,0 +1,11 @@ +import { Client, EmbedBuilder } from "discord.js"; + +export async function send_embed( + client: Client, + channel_id: string, + embed: EmbedBuilder[] +) { + const channel = await client.channels.fetch(channel_id); + if (!channel.isTextBased()) return; + channel.send({ embeds: embed }); +} diff --git a/src/main.ts b/src/main.ts index e6d1867..05709db 100755 --- a/src/main.ts +++ b/src/main.ts @@ -4,12 +4,9 @@ config(); import { getCommands } from "./command/commands.js"; import mojangAPI from "./wrapper/mojang-api.js"; -import { - TUser, - delete_members_who_left, - embed_to_channel, -} from "./functions.js"; +import { TUser } from "./types.js"; import firebase from "./wrapper/firebase.js"; +import { send_embed } from "./functions/send_embed.js"; await mojangAPI.load_cache_from_firestore(); @@ -22,10 +19,29 @@ client.once(Events.ClientReady, async (event) => { console.log(`[DISCORD] Ready: discord client as ${event.user.tag}`); onTime = new Date(); - delete_members_who_left( - client, - await client.guilds.fetch(process.env.DISCORD_TARGET_GUILD_ID) - ); + const guild = await client.guilds.fetch(process.env.DISCORD_TARGET_GUILD_ID); + { + const members_collection = firebase.collection("members"); + const members = await members_collection.get(); + for (const doc of members.docs) { + const data = doc.data() as TUser; + try { + await guild.members.fetch(data.discord_id); + } catch { + doc.ref.delete(); + await send_embed(client, process.env.LOG_CHANNEL_ID, [ + new EmbedBuilder() + .setTitle("Auto Removed") + .setColor(0x0099ff) + .setFields([ + { name: "Discord Id", value: data.discord_id }, + { name: "Minecraft Uuid", value: data.minecraft_uuid }, + ]) + .setTimestamp(new Date()), + ]); + } + } + } }); client.on(Events.InteractionCreate, async (interaction) => { @@ -63,15 +79,16 @@ client.on(Events.GuildMemberRemove, async (interaction) => { for (const doc of exited_user.docs) { const data = doc.data() as TUser; doc.ref.delete(); - const embed = new EmbedBuilder() - .setTitle("물갈이") - .setColor(0x0099ff) - .setFields([ - { name: "Discord Id", value: data.discord_id }, - { name: "Minecraft Uuid", value: data.minecraft_uuid }, - ]) - .setTimestamp(new Date()); - await embed_to_channel(client, process.env.LOG_CHANNEL_ID, embed); + await send_embed(client, process.env.LOG_CHANNEL_ID, [ + new EmbedBuilder() + .setTitle("Auto Removed") + .setColor(0x0099ff) + .setFields([ + { name: "Discord Id", value: data.discord_id }, + { name: "Minecraft Uuid", value: data.minecraft_uuid }, + ]) + .setTimestamp(new Date()), + ]); } }); diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 0000000..b8e80b8 --- /dev/null +++ b/src/types.d.ts @@ -0,0 +1,4 @@ +export type TUser = { + discord_id: string; // id + minecraft_uuid: string; //uuid +};