File size: 1,625 Bytes
67bf4ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { ChatInputCommandInteraction, Message } from "discord.js";
import fs from "fs";
import path from "path";
import { commands } from "../index";

export default {
  data: {
    name: "reload",
    description: "Tải lại toàn bộ lệnh từ thư mục Commands.",
    toJSON() {
      return {
        name: "reload",
        description: "Tải lại toàn bộ lệnh từ thư mục Commands.",
      };
    },
  },
  ownersOnly: true,
  async execute(input: ChatInputCommandInteraction | Message) {
    const dir = path.join(__dirname);
    const newCommands = [];

    try {
      const files = fs.readdirSync(dir).filter((file) =>
        file.endsWith(".ts") || file.endsWith(".js")
      );

      for (const file of files) {
        if (file === "reload.ts" || file === "reload.js") continue;

        const filePath = path.join(dir, file);
        delete require.cache[require.resolve(filePath)];

        const command = require(filePath).default;
        if (command) newCommands.push(command);
      }

      commands.length = 0;
      newCommands.forEach((cmd) => commands.push(cmd));

      const replyText = `✅ Đã reload ${newCommands.length} lệnh.`;

      if (input instanceof Message) {
        await input.reply(replyText);
      } else {
        await input.reply({ content: replyText, ephemeral: true });
      }
    } catch (error) {
      console.error(error);
      if (input instanceof Message) {
        await input.reply("❌ Lỗi khi reload command.");
      } else {
        await input.reply({ content: "❌ Lỗi khi reload command.", ephemeral: true });
      }
    }
  },
};