diff --git a/src/bot/adminMessage.ts b/src/bot/adminMessage.ts deleted file mode 100644 index 1c3219f..0000000 --- a/src/bot/adminMessage.ts +++ /dev/null @@ -1,28 +0,0 @@ -import {Client, Message} from "discord.js"; -import {MongoUtil} from "../util/mongoUtil.js"; -import {getRecommendedProblem} from "../commands/prob.js"; -import {logger} from "../logger.js"; - -export async function sendAdminMessage(message: Message, client: Client) { - message.channel.send("공지사항을 입력해주세요."); - const botFilter = (m: { author: { bot: any; id: any; }; content: string; }) => !m.author.bot && m.author.id === message.author.id && !m.content.startsWith('!'); - const responseCollector = message.channel.createMessageCollector({ filter: botFilter, max: 1, time: 20000 }); - - responseCollector.on('collect', async msg => { - const users = await MongoUtil.findAllUser(); - for (let user of users) { - try{ - const targetUser = await client.users.fetch(user.discord_id) - await targetUser.send(msg.content); - }catch (error){ - logger.error(error); - } - } - }); - responseCollector.on('end', collected => { - if (collected.size === 0) { - message.reply("입력 시간이 만료되었습니다.") - } - }); - -} \ No newline at end of file diff --git a/src/bot/initialize-bot.ts b/src/bot/initialize-bot.ts index 74a7da0..fd9d506 100644 --- a/src/bot/initialize-bot.ts +++ b/src/bot/initialize-bot.ts @@ -2,6 +2,7 @@ import {ApplicationCommandDataResolvable, Client, Collection, GatewayIntentBits, import { mongoConnect } from "../config/mongoDb.js"; import * as dotenv from "dotenv"; import * as fs from "fs"; +import {exit} from "process"; export async function initializeBot(client: Client){ dotenv.config(); @@ -17,7 +18,7 @@ export async function initializeBot(client: Client){ mongoConnect(); //Loading Commands - const commands = fs.readdirSync("./dist/commands").filter(file => file.endsWith(".js")); + const commands = fs.readdirSync("./dist/commands").filter(file => file.endsWith(".js")).filter(file => file !== "adminMessage.js") const slashCommands = new Array(); for (const file of commands) { const commandName = file.split(".")[0]; @@ -30,11 +31,18 @@ export async function initializeBot(client: Client){ const rest = new REST({ version: "9" }).setToken(String(process.env.DISCORD_TOKEN)); const response = await rest.put(Routes.applicationCommands(client.user!.id), { body: slashCommands }); + //adminCommand + const adminCommand = await import("../commands/adminMessage.js"); + const adminCommandData = adminCommand.default.data; + await rest.put(Routes.applicationGuildCommands(client.user!.id, String(process.env.ADMIN_SERVER)), { body: [adminCommandData.toJSON()] }); + + //Initialize Success client.once('ready', async () => { console.log("BOJ Bot is ready") }) }catch (error){ - console.error(error) + console.error(error); + exit(0); } } diff --git a/src/commands/adminMessage.ts b/src/commands/adminMessage.ts new file mode 100644 index 0000000..4ebaa6c --- /dev/null +++ b/src/commands/adminMessage.ts @@ -0,0 +1,29 @@ +import {ChatInputCommandInteraction, Client, Message, SlashCommandBuilder} from "discord.js"; +import {MongoUtil} from "../util/mongoUtil.js"; +import {getRecommendedProblem} from "./prob.js"; +import {logger} from "../logger.js"; + +export default{ + data: new SlashCommandBuilder() + .setName('adminmessage') + .setDescription('ADMIN COMMAND') + .addStringOption(option => option.setName('message').setDescription('공지사항을 입력해주세요.').setRequired(true)), + + async execute(interaction: ChatInputCommandInteraction){ + if(interaction.user.id !== process.env.ADMIN_ID){ + await interaction.reply("권한이 없습니다.") + return; + }else{ + const users = await MongoUtil.findAllUser(); + for (let user of users) { + try{ + const targetUser = await interaction.client.users.fetch(user.discord_id); + await targetUser.send(interaction.options.getString('message')!); + }catch (error){ + logger.error(error); + } + } + await interaction.reply("메시지를 전송했습니다.") + } + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index ba5558c..f60077f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,8 +4,6 @@ import * as cron from 'node-cron'; import { logger } from './logger.js' import {initializeBot} from "./bot/initialize-bot.js"; import {embedWelcome} from "./embedMessage/guideMessage.js"; -import {sendAdminMessage} from "./bot/adminMessage.js"; -import {ModelUtil} from "./util/modelUtil.js"; import {MongoUtil} from "./util/mongoUtil.js"; declare module "discord.js" {