Skip to content

Commit

Permalink
fix: command reloading issues with HMR plugin (#432)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeroen Claassens <jeroen.claassens@live.nl>
  • Loading branch information
PoroUsedSnax and favna committed May 15, 2022
1 parent ad90d3a commit c668f90
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/lib/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,17 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.
// Reload the command
await super.reload();

// Get the command from the store to get any changes from the reload
const updatedPiece = store.get(this.name);

// This likely shouldn't happen but not worth continuing if the piece is somehow no longer available
if (!updatedPiece) return;

const updatedRegistry = updatedPiece.applicationCommandRegistry;

// Rerun the registry
try {
await this.registerApplicationCommands(this.applicationCommandRegistry);
await updatedPiece.registerApplicationCommands(updatedRegistry);
} catch (err) {
emitRegistryError(err, this);
// No point on continuing
Expand All @@ -324,15 +332,15 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.

// Handle the API calls
// eslint-disable-next-line @typescript-eslint/dot-notation
await registry['runAPICalls'](applicationCommands, globalCommands, guildCommands);
await updatedRegistry['runAPICalls'](applicationCommands, globalCommands, guildCommands);

// Re-set the aliases
for (const nameOrId of registry.chatInputCommands) {
store.aliases.set(nameOrId, this);
for (const nameOrId of updatedRegistry.chatInputCommands) {
store.aliases.set(nameOrId, updatedPiece);
}

for (const nameOrId of registry.contextMenuCommands) {
store.aliases.set(nameOrId, this);
for (const nameOrId of updatedRegistry.contextMenuCommands) {
store.aliases.set(nameOrId, updatedPiece);
}
}

Expand Down

0 comments on commit c668f90

Please sign in to comment.