From f166d4b5e312c44348015b350ee9815e54ad7881 Mon Sep 17 00:00:00 2001 From: kdh8219 <65698239+kdh8219@users.noreply.github.com> Date: Wed, 3 May 2023 21:24:23 +0900 Subject: [PATCH] done #2 --- .env.example | 4 +- src/command/commands.ts | 18 +++- src/command/commands/add_nick.ts | 48 +-------- src/command/commands/add_nick_super.ts | 48 +-------- src/command/commands/del_nick_super.ts | 31 +++++- src/command/commands/del_user_super.ts | 19 ++++ src/command/commands/mov_blacklist_super.ts | 31 +++++- src/deploy.ts | 2 +- src/functions.ts | 104 ++++++++++++++++---- src/main.ts | 2 +- src/wrapper/mojang-api.ts | 2 +- 11 files changed, 188 insertions(+), 121 deletions(-) diff --git a/.env.example b/.env.example index 4b4df7e..08a3b98 100644 --- a/.env.example +++ b/.env.example @@ -14,4 +14,6 @@ DISCORD_TARGET_GUILD_ID = "" # ETC # server name -RUNNER_NAME="" +RUNNER_NAME = "" +# log channel id +LOG_CHANNEL_ID = diff --git a/src/command/commands.ts b/src/command/commands.ts index 5b47303..c0c8307 100644 --- a/src/command/commands.ts +++ b/src/command/commands.ts @@ -1,4 +1,8 @@ -import { TCommand } from "../functions.js"; +import { + SlashCommandBuilder, + ChatInputCommandInteraction, + Collection, +} from "discord.js"; import add_nick_super from "./commands/add_nick_super.js"; import add_nick from "./commands/add_nick.js"; @@ -26,3 +30,15 @@ const commands: TCommand[] = [ search, ]; export default commands; + +export type TCommand = { + data: SlashCommandBuilder; + execute: (interaction: ChatInputCommandInteraction) => Promise; +}; +export function getCommands(): Collection { + const commandCollection = new Collection(); + for (const command of commands) { + commandCollection.set(command.data.name, command); + } + return commandCollection; +} diff --git a/src/command/commands/add_nick.ts b/src/command/commands/add_nick.ts index c39e46e..d61fb0d 100755 --- a/src/command/commands/add_nick.ts +++ b/src/command/commands/add_nick.ts @@ -1,6 +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"; export default { data: new SlashCommandBuilder() @@ -16,51 +17,6 @@ export default { async execute(interaction: ChatInputCommandInteraction) { const discord_id = interaction.user.id; const mcid = interaction.options.getString("minecraft_id") as string; - 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, - }); + add_user(interaction, discord_id, mcid, mojangAPI, firebase); }, }; diff --git a/src/command/commands/add_nick_super.ts b/src/command/commands/add_nick_super.ts index dbf6d98..3261a03 100755 --- a/src/command/commands/add_nick_super.ts +++ b/src/command/commands/add_nick_super.ts @@ -6,6 +6,7 @@ import { import mojangAPI from "../../wrapper/mojang-api.js"; import firebase from "../../wrapper/firebase.js"; +import { add_user } from "../../functions.js"; export default { data: new SlashCommandBuilder() @@ -28,51 +29,6 @@ export default { async execute(interaction: ChatInputCommandInteraction) { const discord_id = interaction.options.getUser("discord")?.id as string; const mcid = interaction.options.getString("minecraft_id") as string; - 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, - }); + add_user(interaction, discord_id, mcid, mojangAPI, firebase); }, }; diff --git a/src/command/commands/del_nick_super.ts b/src/command/commands/del_nick_super.ts index c34984f..1e5e062 100755 --- a/src/command/commands/del_nick_super.ts +++ b/src/command/commands/del_nick_super.ts @@ -2,10 +2,12 @@ import { SlashCommandBuilder, PermissionFlagsBits, ChatInputCommandInteraction, + EmbedBuilder, } from "discord.js"; import mojangAPI from "../../wrapper/mojang-api.js"; import firebase from "../../wrapper/firebase.js"; +import { embed_to_channel } from "../../functions.js"; export default { data: new SlashCommandBuilder() @@ -28,7 +30,9 @@ export default { async execute(interaction: ChatInputCommandInteraction) { const discord_id = interaction.options.getUser("discord")?.id as string; - const minecraft_id = interaction.options.getString("minecraft_id") as string; + const minecraft_id = interaction.options.getString( + "minecraft_id" + ) as string; let minecraft_uuid: string; try { minecraft_uuid = await mojangAPI.getUUIDFromId(minecraft_id); @@ -49,7 +53,7 @@ export default { members.doc(member.id).delete(); }); - let discord_tag; + let discord_tag: string; try { const discord_user = await interaction.client.users.fetch(discord_id); discord_tag = discord_user.tag; @@ -59,12 +63,31 @@ export default { await interaction.editReply({ content: `삭제 완료:${discord_tag}(${discord_id})에게서 ${minecraft_id}(${minecraft_uuid})를 제거했습니다.`, }); - return; + + 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 + ); } else { await interaction.editReply({ content: `\`에러\`: 해당 계정이 없습니다.`, }); - return; } }, }; diff --git a/src/command/commands/del_user_super.ts b/src/command/commands/del_user_super.ts index cbcf2f1..382c724 100755 --- a/src/command/commands/del_user_super.ts +++ b/src/command/commands/del_user_super.ts @@ -2,9 +2,11 @@ import { SlashCommandBuilder, PermissionFlagsBits, ChatInputCommandInteraction, + EmbedBuilder, } from "discord.js"; import firebase from "../../wrapper/firebase.js"; +import { embed_to_channel } from "../../functions.js"; export default { data: new SlashCommandBuilder() @@ -42,6 +44,23 @@ 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 + ); return; } else { await interaction.editReply({ diff --git a/src/command/commands/mov_blacklist_super.ts b/src/command/commands/mov_blacklist_super.ts index 1a0dd30..2b5f4a4 100755 --- a/src/command/commands/mov_blacklist_super.ts +++ b/src/command/commands/mov_blacklist_super.ts @@ -2,10 +2,11 @@ import { SlashCommandBuilder, PermissionFlagsBits, ChatInputCommandInteraction, + EmbedBuilder, } from "discord.js"; import firebase from "../../wrapper/firebase.js"; -import { TUser } from "../../functions.js"; +import { TUser, embed_to_channel } from "../../functions.js"; export default { data: new SlashCommandBuilder() @@ -43,5 +44,33 @@ export default { await interaction.editReply({ content: "성공적으로 해당 discord id와 minecraft id를 블랙리스팅 했어요.", }); + + let UUIDs: string; + target_data + .map((user) => { + user.discord_id; + }) + .map((uuid) => { + UUIDs += uuid; + UUIDs += "\n"; + }); + 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 }, + { name: "UUIDs", value: UUIDs }, + ]) + .setTimestamp(interaction.createdAt); + await embed_to_channel( + interaction.client, + process.env.LOG_CHANNEL_ID, + embed + ); }, }; diff --git a/src/deploy.ts b/src/deploy.ts index 59855d6..c4f535c 100755 --- a/src/deploy.ts +++ b/src/deploy.ts @@ -2,7 +2,7 @@ import { REST, Routes } from "discord.js"; import { config } from "dotenv"; config(); -import { getCommands } from "./functions.js"; +import { getCommands } from "./command/commands.js"; const commands = getCommands(); // Construct and prepare an instance of the REST module diff --git a/src/functions.ts b/src/functions.ts index d1dcccf..25a82dc 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -1,28 +1,12 @@ -import { - Collection, - SlashCommandBuilder, - ChatInputCommandInteraction, -} from "discord.js"; +import { ChatInputCommandInteraction, Client, EmbedBuilder } from "discord.js"; +import { Firestore } from "firebase-admin/firestore"; -import commands from "./command/commands.js"; - -export type TCommand = { - data: SlashCommandBuilder; - execute: (interaction: ChatInputCommandInteraction) => Promise; -}; -export function getCommands(): Collection { - const commandCollection = new Collection(); - for (const command of commands) { - commandCollection.set(command.data.name, command); - } - return commandCollection; -} +import { MojangAPI } from "./wrapper/mojang-api.js"; 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 } { @@ -52,3 +36,85 @@ export function dmSlice(raw: string): string[] { } } } + +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] }); +} diff --git a/src/main.ts b/src/main.ts index 451dada..177e677 100755 --- a/src/main.ts +++ b/src/main.ts @@ -2,7 +2,7 @@ import { Client, Events, GatewayIntentBits } from "discord.js"; import { config } from "dotenv"; config(); -import { getCommands } from "./functions.js"; +import { getCommands } from "./command/commands.js"; const client = new Client({ intents: [GatewayIntentBits.Guilds] }); diff --git a/src/wrapper/mojang-api.ts b/src/wrapper/mojang-api.ts index 845af7c..0b0462e 100644 --- a/src/wrapper/mojang-api.ts +++ b/src/wrapper/mojang-api.ts @@ -2,7 +2,7 @@ import axios from "axios"; import TwoWayMap from "../utils/two-way-map.js"; -class MojangAPI { +export class MojangAPI { private cache: TwoWayMap = new TwoWayMap(); // uuid, id async getIdFromUUID(minecraft_uuid: string): Promise {