diff --git a/src/command/commands.ts b/src/command/commands.ts index 0065084..c0c8307 100644 --- a/src/command/commands.ts +++ b/src/command/commands.ts @@ -6,7 +6,6 @@ import { import add_nick_super from "./commands/add_nick_super.js"; import add_nick from "./commands/add_nick.js"; -import del_members_who_left_super from "./commands/del_members_who_left_super.js"; import del_nick_super from "./commands/del_nick_super.js"; import del_user_super from "./commands/del_user_super.js"; import get_blacklist from "./commands/get_blacklist.js"; @@ -20,7 +19,6 @@ import search from "./commands/search.js"; const commands: TCommand[] = [ add_nick_super, add_nick, - del_members_who_left_super, del_nick_super, del_user_super, get_blacklist, diff --git a/src/command/commands/del_members_who_left_super.ts b/src/command/commands/del_members_who_left_super.ts deleted file mode 100644 index d0144c6..0000000 --- a/src/command/commands/del_members_who_left_super.ts +++ /dev/null @@ -1,95 +0,0 @@ -import { - SlashCommandBuilder, - PermissionFlagsBits, - ChatInputCommandInteraction, - EmbedBuilder, -} from "discord.js"; - -import firebase from "../../wrapper/firebase.js"; -import { TUser, embed_to_channel } from "../../functions.js"; - -export default { - data: new SlashCommandBuilder() - .setName("del_members_who_left_super") - .setDescription( - "물갈이(서버에 부하가 있으므로, 과도한 사용을 지양해주세요!)" - ) - .setDefaultMemberPermissions(PermissionFlagsBits.Administrator) - .setDMPermission(false), - async execute(interaction: ChatInputCommandInteraction) { - 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 interaction.guild.members.fetch(data.discord_id); - } catch { - doc.ref.delete(); - - const embed = new EmbedBuilder() - .setTitle("물갈이됨") - .setDescription("deleted") - .setColor(0x0099ff) - .setFields([ - { - name: "Command Sender", - value: `${interaction.user.tag}(${interaction.user.id})`, - }, - { name: " ", value: " " }, - { name: "Discord Id", value: data.discord_id }, - { name: "Minecraft Uuid", value: data.minecraft_uuid }, - ]) - .setTimestamp(interaction.createdAt); - await embed_to_channel( - interaction.client, - process.env.LOG_CHANNEL_ID, - embed - ); - } - } - - // const should_remove = await members - // .where("discord_id", "==", discord_id) - // .get(); - - // if (!should_remove.empty) { - // should_remove.forEach((item) => { - // item.ref.delete(); - // }); - - // let discord_tag; - // try { - // const discord_user = await interaction.client.users.fetch(discord_id); - // discord_tag = discord_user.tag; - // } catch { - // discord_tag = "Deleted User#0000"; - // } - - // 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({ - // content: `\`에러\`: 해당 유저를 찾을 수 없습니다.`, - // }); - // } - }, -}; diff --git a/src/functions.ts b/src/functions.ts index 61e4ebd..72db536 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -1,7 +1,13 @@ -import { ChatInputCommandInteraction, Client, EmbedBuilder } from "discord.js"; +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 @@ -120,3 +126,29 @@ export async function embed_to_channel( 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/main.ts b/src/main.ts index 9d05380..e6d1867 100755 --- a/src/main.ts +++ b/src/main.ts @@ -1,9 +1,16 @@ -import { Client, Events, GatewayIntentBits } from "discord.js"; +import { Client, EmbedBuilder, Events, GatewayIntentBits } from "discord.js"; import { config } from "dotenv"; 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 firebase from "./wrapper/firebase.js"; + await mojangAPI.load_cache_from_firestore(); const client = new Client({ intents: [GatewayIntentBits.Guilds] }); @@ -11,9 +18,14 @@ const client = new Client({ intents: [GatewayIntentBits.Guilds] }); const commandCollection = getCommands(); export let onTime: Date; -client.once(Events.ClientReady, (event) => { +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) + ); }); client.on(Events.InteractionCreate, async (interaction) => { @@ -43,4 +55,24 @@ client.on(Events.InteractionCreate, async (interaction) => { } }); +client.on(Events.GuildMemberRemove, async (interaction) => { + const exited_user = await firebase + .collection("members") + .where("discord_id", "==", interaction.user.id) + .get(); + 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 client.login(process.env.DISCORD_TOKEN);