Skip to content

Commit

Permalink
✨ feat: add admincommand & restart bot when failed
Browse files Browse the repository at this point in the history
  • Loading branch information
synoti21 committed Feb 10, 2024
1 parent 730eb98 commit 1cc2012
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
28 changes: 0 additions & 28 deletions src/bot/adminMessage.ts

This file was deleted.

12 changes: 10 additions & 2 deletions src/bot/initialize-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<ApplicationCommandDataResolvable>();
for (const file of commands) {
const commandName = file.split(".")[0];
Expand All @@ -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);
}
}
29 changes: 29 additions & 0 deletions src/commands/adminMessage.ts
Original file line number Diff line number Diff line change
@@ -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("메시지를 전송했습니다.")
}
}
}
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 1cc2012

Please sign in to comment.