Skip to content

Commit c216345

Browse files
author
xyzjesper
committed
Fixed Permission and Command Manager.
1 parent 7d5d023 commit c216345

File tree

40 files changed

+109
-76
lines changed

40 files changed

+109
-76
lines changed

src/handler/files/commands.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export async function loadCommands(client: ExtendedClient) {
6161
if (!commandModule) continue;
6262
const command = commandModule.default ?? commandModule;
6363

64-
const commandData = command?.data || command?.command;
64+
const commandData = command?.command;
6565

6666
if (!commandData && (command.subCommand || command.subCommandGroup)) {
6767
continue;
@@ -90,12 +90,12 @@ export async function loadCommands(client: ExtendedClient) {
9090
if (!contextMenuModule) continue;
9191
const contextMenu = contextMenuModule.default ?? contextMenuModule;
9292

93-
if (!contextMenu.data?.name) {
93+
if (!contextMenu.command?.name) {
9494
console.error(`${fileName}`.yellow, `❗ FAILED`.red, "Missing name.");
9595
continue;
9696
}
9797

98-
client.commands?.set(contextMenu.data.name, contextMenu);
98+
client.commands?.set(contextMenu.command.name, contextMenu);
9999
loadedStats.contextMenus++;
100100
} catch (error) {
101101
console.error(`Failed to load context menu ${fileName} from ${moduleDir}:`.red, error);
@@ -114,7 +114,7 @@ export async function loadCommands(client: ExtendedClient) {
114114
if (!userInstallModule) continue;
115115
const userInstall = userInstallModule.default ?? userInstallModule;
116116

117-
const userInstallData = userInstall.data ?? userInstall.command;
117+
const userInstallData = userInstall.command;
118118

119119
if (!userInstallData && (userInstall.subCommand || userInstall.subCommandGroup)) {
120120
continue;

src/helper/CommandHelper.ts

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,19 @@ export class CommandHelper {
2121

2222
Logger.info(`Starting Command loading for ${guildId}....`.gray.italic)
2323

24+
// ADD /commands to GUILD
25+
await fetch(`https://discord.com/api/v10/applications/${client.user.id}/guilds/${guildId}/commands`, {
26+
method: "POST",
27+
headers: {
28+
"Content-Type": "application/json",
29+
Authorization: `Bot ${Config.Bot.DiscordBotToken}`
30+
},
31+
body: JSON.stringify(client.commands.get("commands").command)
32+
})
33+
2434
let cmdlist: any[] = [];
2535
const stats = {
2636
commands: 0,
27-
userInstall: 0,
2837
contextMenus: 0,
2938
subCommands: 0,
3039
subCommandGroups: 0
@@ -51,7 +60,6 @@ export class CommandHelper {
5160
commands: moduleCommandFolder,
5261
contextMenus: path.join(modulesFolder, moduleDir, "contextmenu"),
5362
subCommands: path.join(moduleCommandFolder, "subCommand"),
54-
userInstall: path.join(moduleCommandFolder, "userInstall"),
5563
subCommandGroups: path.join(moduleCommandFolder, "subCommandGroup"),
5664
};
5765

@@ -67,8 +75,8 @@ export class CommandHelper {
6775

6876
try {
6977
const module = await import(pathToFileURL(filePath).href);
70-
if (module.default?.data) {
71-
cmdlist.push(module.default.data.toJSON());
78+
if (module.default?.command) {
79+
cmdlist.push(module.default.command.toJSON());
7280
stats.commands++;
7381
}
7482
} catch (error) {
@@ -83,8 +91,8 @@ export class CommandHelper {
8391
for (const filePath of contextCommandFiles) {
8492
try {
8593
const module = await import(pathToFileURL(filePath).href);
86-
if (module.default?.data) {
87-
cmdlist.push(module.default.data.toJSON());
94+
if (module.default?.command) {
95+
cmdlist.push(module.default.command.toJSON());
8896
stats.contextMenus++;
8997
}
9098
} catch (error) {
@@ -115,22 +123,45 @@ export class CommandHelper {
115123
}
116124
})
117125

118-
// TODO: ADD COMMAND_OVERWRITES
126+
if (buildInCommandOverrides.length > 0) {
127+
cmdlist = cmdlist
128+
.filter(cmd => {
129+
const override = buildInCommandOverrides.find(o => o.CodeName === cmd.name);
130+
return !(override && override.IsEnabled === false);
131+
})
132+
.map(cmd => {
133+
const override = buildInCommandOverrides.find(o => o.CodeName === cmd.name);
134+
if (override) {
135+
return {
136+
...cmd,
137+
name: override.CustomName,
138+
description: override.Description ?? client.commands.get(override.CodeName).command.description,
139+
default_member_permissions: override.Permissions ?? client.commands.get(override.CodeName).command.default_member_permissions
140+
};
141+
}
142+
return cmd;
143+
})
144+
}
119145

120146
Logger.info(`Sending commands to guild ${guildId} for client ${client.user.username}`);
121147

122148
try {
123-
const commandReq = await fetch(`https://discord.com/api/v10/applications/${client.user.id}/guilds/${guildId}/commands`, {
149+
await fetch(`https://discord.com/api/v10/applications/${client.user.id}/guilds/${guildId}/commands`, {
150+
method: "PUT",
151+
headers: {
152+
"Content-Type": "application/json",
153+
Authorization: `Bot ${Config.Bot.DiscordBotToken}`
154+
},
155+
body: JSON.stringify([])
156+
})
157+
await fetch(`https://discord.com/api/v10/applications/${client.user.id}/guilds/${guildId}/commands`, {
124158
method: "PUT",
125159
headers: {
126160
"Content-Type": "application/json",
127161
Authorization: `Bot ${Config.Bot.DiscordBotToken}`
128162
},
129163
body: JSON.stringify(cmdlist)
130164
})
131-
const data = await commandReq.json()
132-
console.log(JSON.stringify(data))
133-
134165
} catch (e) {
135166
Logger.error(`Failed to load commands: ${e}`)
136167
}
@@ -192,11 +223,12 @@ export class CommandHelper {
192223
}
193224
}
194225
}
226+
195227
Logger.info({
196228
timestamp: new Date().toISOString(),
197229
level: "info",
198230
label: "CommandHelper",
199-
message: `Discord added ${cmdlist.length} commands (${stats.subCommands} subCommands, ${stats.subCommandGroups} subCommandGroups), ${stats.userInstall} userInstall commands, ${stats.contextMenus} context menu commands from ${moduleDirectories.length} module(s) for ${guildId}`,
231+
message: `Discord added ${cmdlist.length} commands (${stats.subCommands} subCommands, ${stats.subCommandGroups} subCommandGroups), ${stats.contextMenus} context menu commands from ${moduleDirectories.length} module(s) for ${guildId}`,
200232
botType: Config.BotType.toString() || "Unknown",
201233
action: LoggingAction.Command,
202234
});
@@ -250,8 +282,8 @@ export class CommandHelper {
250282

251283
try {
252284
const module = await import(pathToFileURL(filePath).href);
253-
if (module.default?.data) {
254-
cmdlist.push(module.default.data.toJSON());
285+
if (module.default?.command) {
286+
cmdlist.push(module.default.command.toJSON());
255287
stats.commands++;
256288
}
257289
} catch (error) {
@@ -266,8 +298,8 @@ export class CommandHelper {
266298
for (const filePath of userCommandFiles) {
267299
try {
268300
const module = await import(pathToFileURL(filePath).href);
269-
if (module.default?.data) {
270-
applicationcmdlist.push(module.default.data.toJSON());
301+
if (module.default?.command) {
302+
applicationcmdlist.push(module.default.command.toJSON());
271303
stats.userInstall++;
272304
}
273305
} catch (error) {
@@ -282,8 +314,8 @@ export class CommandHelper {
282314
for (const filePath of contextCommandFiles) {
283315
try {
284316
const module = await import(pathToFileURL(filePath).href);
285-
if (module.default?.data) {
286-
cmdlist.push(module.default.data.toJSON());
317+
if (module.default?.command) {
318+
cmdlist.push(module.default.command.toJSON());
287319
stats.contextMenus++;
288320
}
289321
} catch (error) {
@@ -350,7 +382,7 @@ export class CommandHelper {
350382
let successCount = 0;
351383
let errorCount = 0;
352384
const BATCH_SIZE = 5;
353-
const DELAY_MS = 3000;
385+
const DELAY_MS = 3000;
354386

355387
for (const guild of guildArray) {
356388
try {
@@ -360,7 +392,7 @@ export class CommandHelper {
360392
});
361393
await delay(DELAY_MS);
362394
}
363-
395+
364396
await CommandHelper.loadCommandsForGuild(client, guild.id);
365397
successCount++;
366398

src/main/bot.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ await disbotClient
8484
await initDataToDatabase(disbotClient)
8585

8686
// Load Commands
87-
// await CommandHelper.loadCommands(disbotClient);
87+
await CommandHelper.loadCommands(disbotClient);
88+
await CommandHelper.loadCustomAdminCommands(disbotClient);
8889

8990
// API && Version
9091
await api(disbotClient);

src/modules/automation/commands/automation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default {
2727
userHasOnePermission: true,
2828
isGuildOwner: false,
2929
},
30-
data: new SlashCommandBuilder()
30+
command: new SlashCommandBuilder()
3131
.setName("automations")
3232
.setDescription("Manage all automations")
3333
.setDescriptionLocalizations({

src/modules/backup/commands/backups.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default {
2222
aliases: [],
2323
docsLink: 'https://docs.disbot.app/docs/commands/backups'
2424
},
25-
data: new SlashCommandBuilder()
25+
command: new SlashCommandBuilder()
2626
.setName("backups")
2727
.setDescription("Manage Backups")
2828
.setDescriptionLocalizations({

src/modules/bot/commands/help.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default {
2121
aliases: [],
2222
docsLink: 'https://docs.disbot.app/docs/commands/help'
2323
},
24-
data: new SlashCommandBuilder()
24+
command: new SlashCommandBuilder()
2525
.setName("help")
2626
.setDescription("Use this command to get help about the bot")
2727
.setContexts(InteractionContextType.Guild)

src/modules/bot/events/bot-ready.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ export default {
2525
async execute(client: ExtendedClient) {
2626
try {
2727

28-
// Load Admin Guild Commands
29-
await CommandHelper.loadCustomAdminCommands(client);
30-
3128
// Invite Tracker Fetch
3229
client.guilds.cache.forEach(async (guild: Guild) => {
3330
guildFetcher(client, guild);

src/modules/channel-link/commands/channel-link.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default {
2525
aliases: [],
2626
docsLink: 'https://docs.disbot.app/docs/commands/channel-link'
2727
},
28-
data: new SlashCommandBuilder()
28+
command: new SlashCommandBuilder()
2929
.setName("channel-link")
3030
.setDescription("Channel Link to link channel over servers")
3131
.setDescriptionLocalizations({

src/modules/commands/buttons/commands-manager.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export default {
1818
const pageSize = 5;
1919
const data = client.commands.map(value => value)
2020

21+
console.log(data)
22+
23+
2124
if (data.length <= 0) return interaction.reply({
2225
content: "No commands found.",
2326
flags: MessageFlags.Ephemeral
@@ -34,7 +37,7 @@ export default {
3437
.setPlaceholder("Select a Option to manage")
3538
.addOptions(
3639
await Promise.all(list.map(async (l) => ({
37-
label: `${l.command.name}`,
40+
label: `${l.command.name ?? "N/A"}`,
3841
description: `${l.command.description ?? "N/A"}`,
3942
value: l.command.name,
4043
emoji: "<:terminal:1260322426323996783>",

src/modules/commands/commands/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {database} from "../../../main/database.js";
1717

1818

1919
export default {
20-
data: new SlashCommandBuilder()
20+
command: new SlashCommandBuilder()
2121
.setName("commands")
2222
.setDescription("Create, Manage, and use the Command Manager!")
2323
.setContexts(InteractionContextType.Guild)

0 commit comments

Comments
 (0)