This repository was archived by the owner on Feb 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
152 lines (121 loc) · 4.46 KB
/
Copy pathindex.ts
File metadata and controls
152 lines (121 loc) · 4.46 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Require the necessary discord.js classes
import { Client, Collection, GatewayIntentBits, REST, Routes } from "discord.js"
import * as fs from "fs"
import log, { errLog } from "./handlers/logger";
export interface CustomClient extends Client {
commands: Collection<string, any>;
slashCommands: Collection<string, any>;
buttonCommands: Collection<string, any>;
contextCommands: Map<string, any>;
modalCommands: Map<string, any>;
cooldowns: Collection<string, any>;
};
// Create a new client instance
const client: any = new Client({ intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildModeration,
GatewayIntentBits.GuildInvites,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent,
] });
client.commands = new Collection();
client.slashCommands = new Collection();
client.buttonCommands = new Collection();
client.contextCommands = new Collection();
client.modalCommands = new Collection();
client.cooldowns = new Collection();
// Event Handler
const eventFiles = fs
.readdirSync("./events")
.filter((file: any) => file.endsWith(".ts"));
// Loop through all files and execute the event when it is actually emmited.
for (const file of eventFiles) {
const event = require(`./events/${file}`);
try{
if (event.once) {
client.once(event.name, (...args: any) => event.execute(...args, client));
} else {
client.on(
event.name,
async (...args: any) => await event.execute(...args, client)
);
};
} catch (error: any) {
errLog(client, error, false);
}
}
// Message-Based Command Handler
const commandFolders = fs.readdirSync("./commands");
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`./commands/${folder}`)
.filter((file) => file.endsWith(".ts"));
for (const file of commandFiles) {
const command = require(`./commands/${folder}/${file}`);
client.commands.set(command.name, command);
}
}
//Context-Menu Handler
const contextMenus = fs.readdirSync('./interactions/context-menus');
for (const folder of contextMenus) {
const files = fs.readdirSync(`./interactions/context-menus/${folder}`).filter((file) => file.endsWith('.ts'));
for (const file of files) {
const menu = require(`./interactions/context-menus/${folder}/${file}`);
const keyName = `${folder.toUpperCase()} ${menu.data.name}`;
client.contextCommands.set(keyName, menu);
}
}
// Registration of Modal-Command Interactions.
const modalCommands = fs.readdirSync('./interactions/modals');
for (const module of modalCommands) {
const commandFiles = fs.readdirSync(`./interactions/modals/${module}`).filter((file) => file.endsWith('.ts'));
for (const commandFile of commandFiles) {
const command = require(`./interactions/modals/${module}/${commandFile}`);
client.modalCommands.set(command.id, command);
}
}
// Slash Command Handler
const slashCommands = fs.readdirSync("./interactions/slash");
for (const module of slashCommands) {
const commandFiles = fs
.readdirSync(`./interactions/slash/${module}`)
.filter((file) => file.endsWith(".ts"));
for (const commandFile of commandFiles) {
const command = require(`./interactions/slash/${module}/${commandFile}`);
client.slashCommands.set(command.data.name, command);
}
}
// Button Command Handler
const buttonCommands = fs.readdirSync("./interactions/buttons");
for (const module of buttonCommands) {
const commandFiles = fs
.readdirSync(`./interactions/buttons/${module}`)
.filter((file) => file.endsWith(".ts"));
for (const commandFile of commandFiles) {
const command = require(`./interactions/buttons/${module}/${commandFile}`);
client.buttonCommands.set(command.id, command);
}
}
// Command Registration
const rest = new REST({ version: "9" }).setToken(process.env.TOKEN!);
const commandJsonData = [
...Array.from(client.slashCommands.values()).map((c: any) => c.data.toJSON()),
...Array.from(client.contextCommands.values()).map((c: any) => c.data),
];
(async () => {
try {
log.info("Started refreshing application (/) commands.");
await rest.put(
Routes.applicationGuildCommands(process.env.CLIENT_ID!, process.env.TEST_GUILD_ID!),
{ body: commandJsonData }
);
log.info("Successfully reloaded application (/) commands.");
} catch (error: any) {
throw(error);
}
})();
// Log in to Discord with your client's token
client.login(process.env.TOKEN);