Skip to content

Commit a6c168e

Browse files
committed
0.0.5
1 parent e39884c commit a6c168e

File tree

5 files changed

+98
-26
lines changed

5 files changed

+98
-26
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66

7+
## [0.0.5] - 2023-07-02
8+
9+
### Added
10+
11+
- Ability to automatically update application commands (guilds and global) when there's changes to the description or number of options (slash commands only).
12+
713
## [0.0.4] - 2023-07-01
814

915
### Updated

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ _Tested with Discord.js version `v14.11.0`_
88

99
- Very beginner friendly 🚀
1010
- Support for slash and context menu commands ✅
11-
- Automatic command registrations and deletion 🤖
11+
- Automatic command registration, edits, and deletion 🤖
1212
- Supports multiple development servers 🤝
1313
- Supports multiple users as bot developers 👥
1414
- Object oriented 💻

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{
22
"name": "commandkit",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"main": "dist/index.js",
55
"license": "MIT",
6+
"scripts": {
7+
"build": "tsc"
8+
},
69
"repository": {
710
"type": "git",
811
"url": "https://github.com/notunderctrl/commandkit"

src/handlers/command-handler/CommandHandler.ts

Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
Guild,
3-
SlashCommandBuilder,
4-
ContextMenuCommandBuilder,
5-
GuildApplicationCommandManager,
6-
} from 'discord.js';
1+
import { Guild, GuildApplicationCommandManager } from 'discord.js';
72
import { getFilePaths } from '../../utils/get-paths';
83
import { CommandHandlerData, CommandHandlerOptions } from './typings';
94
import { ContextCommandObject, SlashCommandObject } from '../../../typings';
@@ -80,28 +75,17 @@ export class CommandHandler {
8075
}
8176

8277
for (const command of commands) {
83-
const commandData = command.data;
84-
85-
if (
86-
commandData instanceof SlashCommandBuilder ||
87-
commandData instanceof ContextMenuCommandBuilder
88-
) {
89-
try {
90-
commandData.toJSON();
91-
} catch (error) {}
92-
}
93-
94-
// <!-- TODO: Edit command if there's any changes -->
95-
9678
// <!-- Delete command if options.deleted -->
9779
if (command.options?.deleted) {
9880
const targetCommand = appCommands?.cache.find((cmd) => cmd.name === command.data.name);
9981

10082
if (!targetCommand) {
101-
console.log(`⏩ Ignoring: Command "${command.data.name}" is set to be deleted.`);
83+
console.log(
84+
`⏩ Ignoring: Command "${command.data.name}" is globally marked as deleted.`
85+
);
10286
} else {
10387
targetCommand.delete().then(() => {
104-
console.log(`🗑️ Deleted command "${command.data.name}" globally.`);
88+
console.log(`🚮 Deleted command "${command.data.name}" globally.`);
10589
});
10690
}
10791

@@ -110,12 +94,12 @@ export class CommandHandler {
11094

11195
if (!targetCommand) {
11296
console.log(
113-
`⏩ Ignoring: Command "${command.data.name}" is set to be deleted in ${guildCommands.guild.name}.`
97+
`⏩ Ignoring: Command "${command.data.name}" is marked as deleted for ${guildCommands.guild.name}.`
11498
);
11599
} else {
116100
targetCommand.delete().then(() => {
117101
console.log(
118-
`🗑️ Deleted command "${command.data.name}" in ${guildCommands.guild.name}.`
102+
`🚮 Deleted command "${command.data.name}" in ${guildCommands.guild.name}.`
119103
);
120104
});
121105
}
@@ -124,6 +108,70 @@ export class CommandHandler {
124108
continue;
125109
}
126110

111+
// <!-- Edit command if there's any changes -->
112+
let commandData = command.data;
113+
let editedCommand = false;
114+
115+
(() => {
116+
// global
117+
const appGlobalCommand = appCommands?.cache.find((cmd) => cmd.name === command.data.name);
118+
119+
if (appGlobalCommand) {
120+
const commandsAreDifferent = this._areSlashCommandsDifferent(
121+
appGlobalCommand,
122+
commandData
123+
);
124+
125+
if (commandsAreDifferent) {
126+
appGlobalCommand
127+
.edit(commandData)
128+
.then(() => {
129+
console.log(`✅ Edited command "${commandData.name}" globally.`);
130+
})
131+
.catch((error) => {
132+
console.log(`❌ Failed to edit command "${commandData.name}" globally.`);
133+
console.error(error);
134+
});
135+
136+
editedCommand = true;
137+
}
138+
}
139+
140+
// guilds
141+
for (const guildCommands of devGuildCommands) {
142+
const appGuildCommand = guildCommands.cache.find(
143+
(cmd) => cmd.name === commandData.name
144+
);
145+
146+
if (appGuildCommand) {
147+
const commandsAreDifferent = this._areSlashCommandsDifferent(
148+
appGuildCommand,
149+
commandData
150+
);
151+
152+
if (commandsAreDifferent) {
153+
appGuildCommand
154+
.edit(commandData)
155+
.then(() => {
156+
console.log(
157+
`✅ Edited command "${commandData.name}" in ${guildCommands.guild.name}.`
158+
);
159+
})
160+
.catch((error) => {
161+
console.log(
162+
`❌ Failed to edit command "${commandData.name}" in ${guildCommands.guild.name}.`
163+
);
164+
console.error(error);
165+
});
166+
167+
editedCommand = true;
168+
}
169+
}
170+
}
171+
})();
172+
173+
if (editedCommand) continue;
174+
127175
// <!-- Registration -->
128176
// guild-based command registration
129177
if (command.options?.devOnly) {
@@ -263,6 +311,21 @@ export class CommandHandler {
263311
});
264312
}
265313

314+
_areSlashCommandsDifferent(appCommand: any, localCommand: any) {
315+
if (!appCommand.options) appCommand.options = [];
316+
if (!localCommand.options) localCommand.options = [];
317+
318+
if (!appCommand.description) appCommand.description = '';
319+
if (!localCommand.description) localCommand.description = '';
320+
321+
if (
322+
localCommand.description !== appCommand.description ||
323+
localCommand.options.length !== appCommand.options.length
324+
) {
325+
return true;
326+
}
327+
}
328+
266329
getCommands() {
267330
return this._data.commands;
268331
}

typings.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface SlashCommandObject {
2929
name_localizations?: any;
3030
description: string;
3131
dm_permission?: boolean;
32-
options: APIApplicationCommandOption[];
32+
options?: APIApplicationCommandOption[];
3333
};
3434
options?: {
3535
guildOnly?: boolean;

0 commit comments

Comments
 (0)