Skip to content

Commit

Permalink
feat: Improve condition of updating commands (#1345)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina authored May 31, 2024
1 parent 9848f85 commit fd882ad
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions packages/bot/src/service/command/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,32 @@ export const registerCommands = async ({
.flatMap((responder) => schemaToDiscordFormat(responder.schema));
const commandByName = new Map(commands.map((obj) => [obj.name, obj]));

const isCommandUpdated = (registered: RegisteredCommand): boolean => {
if (!commandByName.has(registered.name)) {
return false;
}
const mapped = {
...(commandByName.get(registered.name) ?? {}),
id: registered.id
};
const filteredRegistered = {
name: registered.name,
description: registered.description,
options: registered.options,
id: registered.id
};
console.log({ mapped, filteredRegistered });
return !equal(mapped, filteredRegistered);
};

const idsNeedToDelete = [...currentRegisteredByName.keys()]
.filter((name) => !commandByName.has(name))
.map(
(name) =>
(currentRegisteredByName.get(name)?.id ?? 'unknown') as Snowflake
);
const needToUpdate = [...currentRegisteredByName.values()]
.filter(
(registered) =>
commandByName.has(registered.name) &&
!equal(commandByName.get(registered.name) ?? {}, registered)
)
.filter(isCommandUpdated)
.map(
({ id, name }) =>
({ id, ...commandByName.get(name) }) as RegisteredCommand
Expand Down

0 comments on commit fd882ad

Please sign in to comment.