Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update commands fix #36

Merged
merged 3 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 26 additions & 29 deletions lib/creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,31 +246,29 @@ class SlashCreator extends eventemitter3_1.default {
delete partialCommand.version;
const command = this.commands.find((command) => !!(command.guildIDs && command.guildIDs.includes(guildID) && command.commandName === partialCommand.name));
if (command) {
if (!lodash_1.isEqual(util_1.objectKeySort(partialCommand), util_1.objectKeySort(command.commandJSON))) {
this.emit('debug', `Updating guild command "${applicationCommand.name}" (${applicationCommand.id}, guild: ${guildID})`);
updatePayload.push({
id: applicationCommand.id,
...command.commandJSON
});
}
else {
this.emit('debug', `Guild command "${applicationCommand.name}" (${applicationCommand.id}) synced (guild: ${guildID})`);
}
this.emit('debug', `Found guild command "${applicationCommand.name}" (${applicationCommand.id}, guild: ${guildID})`);
updatePayload.push({
id: applicationCommand.id,
...command.commandJSON
});
handledCommands.push(command.keyName);
}
else if (deleteCommands) {
// Command is removed
this.emit('debug', `Removing guild command "${applicationCommand.name}" (${applicationCommand.id}, guild: ${guildID})`);
await this.api.deleteCommand(applicationCommand.id, guildID);
}
else {
updatePayload.push(applicationCommand);
}
}
if (updatePayload.length)
await this.api.updateCommands(updatePayload, guildID);
const unhandledCommands = this.commands.filter((command) => !!(command.guildIDs && command.guildIDs.includes(guildID) && !handledCommands.includes(command.keyName)));
for (const [, command] of unhandledCommands) {
this.emit('debug', `Creating guild command "${command.commandName}" (guild: ${guildID})`);
await this.api.createCommand(command.commandJSON, guildID);
updatePayload.push({
...command.commandJSON
});
}
if (updatePayload.length)
await this.api.updateCommands(updatePayload, guildID);
}
/**
* Sync global commands.
Expand All @@ -289,30 +287,29 @@ class SlashCreator extends eventemitter3_1.default {
delete partialCommand.version;
const command = this.commands.get(commandKey);
if (command) {
if (!lodash_1.isEqual(util_1.objectKeySort(partialCommand), util_1.objectKeySort(command.commandJSON))) {
this.emit('debug', `Updating command "${applicationCommand.name}" (${applicationCommand.id})`);
updatePayload.push({
id: applicationCommand.id,
...command.commandJSON
});
}
else {
this.emit('debug', `Command "${applicationCommand.name}" (${applicationCommand.id}) synced`);
}
this.emit('debug', `Found command "${applicationCommand.name}" (${applicationCommand.id})`);
updatePayload.push({
id: applicationCommand.id,
...command.commandJSON
});
}
else if (deleteCommands) {
this.emit('debug', `Removing command "${applicationCommand.name}" (${applicationCommand.id})`);
await this.api.deleteCommand(applicationCommand.id);
}
else {
updatePayload.push(applicationCommand);
}
handledCommands.push(commandKey);
}
if (updatePayload.length)
this.api.updateCommands(updatePayload);
const unhandledCommands = this.commands.filter((command) => !command.guildIDs && !handledCommands.includes(command.keyName));
for (const [, command] of unhandledCommands) {
this.emit('debug', `Creating command "${command.commandName}"`);
await this.api.createCommand(command.commandJSON);
updatePayload.push({
...command.commandJSON
});
}
if (updatePayload.length)
this.api.updateCommands(updatePayload);
}
_getCommandFromInteraction(interaction) {
return 'guild_id' in interaction
Expand Down
69 changes: 28 additions & 41 deletions src/creator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import EventEmitter from 'eventemitter3';
import Collection from '@discordjs/collection';
import HTTPS from 'https';
import {
formatAllowedMentions,
FormattedAllowedMentions,
MessageAllowedMentions,
objectKeySort,
oneLine,
verifyKey
} from './util';
import { formatAllowedMentions, FormattedAllowedMentions, MessageAllowedMentions, oneLine, verifyKey } from './util';
import {
ImageFormat,
InteractionType,
Expand Down Expand Up @@ -383,43 +376,38 @@ class SlashCreator extends ((EventEmitter as any) as new () => TypedEmitter<Slas
!!(command.guildIDs && command.guildIDs.includes(guildID) && command.commandName === partialCommand.name)
);
if (command) {
if (!isEqual(objectKeySort(partialCommand), objectKeySort(command.commandJSON))) {
this.emit(
'debug',
`Updating guild command "${applicationCommand.name}" (${applicationCommand.id}, guild: ${guildID})`
);
updatePayload.push({
id: applicationCommand.id,
...command.commandJSON
});
} else {
this.emit(
'debug',
`Guild command "${applicationCommand.name}" (${applicationCommand.id}) synced (guild: ${guildID})`
);
}
this.emit(
'debug',
`Found guild command "${applicationCommand.name}" (${applicationCommand.id}, guild: ${guildID})`
);
updatePayload.push({
id: applicationCommand.id,
...command.commandJSON
});
handledCommands.push(command.keyName);
} else if (deleteCommands) {
// Command is removed
this.emit(
'debug',
`Removing guild command "${applicationCommand.name}" (${applicationCommand.id}, guild: ${guildID})`
);
await this.api.deleteCommand(applicationCommand.id, guildID);
} else {
updatePayload.push(applicationCommand);
}
}

if (updatePayload.length) await this.api.updateCommands(updatePayload, guildID);

const unhandledCommands = this.commands.filter(
(command) =>
!!(command.guildIDs && command.guildIDs.includes(guildID) && !handledCommands.includes(command.keyName))
);

for (const [, command] of unhandledCommands) {
this.emit('debug', `Creating guild command "${command.commandName}" (guild: ${guildID})`);
await this.api.createCommand(command.commandJSON, guildID);
updatePayload.push({
...command.commandJSON
});
}

if (updatePayload.length) await this.api.updateCommands(updatePayload, guildID);
}

/**
Expand All @@ -441,33 +429,32 @@ class SlashCreator extends ((EventEmitter as any) as new () => TypedEmitter<Slas

const command = this.commands.get(commandKey);
if (command) {
if (!isEqual(objectKeySort(partialCommand), objectKeySort(command.commandJSON))) {
this.emit('debug', `Updating command "${applicationCommand.name}" (${applicationCommand.id})`);
updatePayload.push({
id: applicationCommand.id,
...command.commandJSON
});
} else {
this.emit('debug', `Command "${applicationCommand.name}" (${applicationCommand.id}) synced`);
}
this.emit('debug', `Found command "${applicationCommand.name}" (${applicationCommand.id})`);
updatePayload.push({
id: applicationCommand.id,
...command.commandJSON
});
} else if (deleteCommands) {
this.emit('debug', `Removing command "${applicationCommand.name}" (${applicationCommand.id})`);
await this.api.deleteCommand(applicationCommand.id);
} else {
updatePayload.push(applicationCommand);
}

handledCommands.push(commandKey);
}

if (updatePayload.length) this.api.updateCommands(updatePayload);

const unhandledCommands = this.commands.filter(
(command) => !command.guildIDs && !handledCommands.includes(command.keyName)
);

for (const [, command] of unhandledCommands) {
this.emit('debug', `Creating command "${command.commandName}"`);
await this.api.createCommand(command.commandJSON);
updatePayload.push({
...command.commandJSON
});
}

if (updatePayload.length) this.api.updateCommands(updatePayload);
}

private _getCommandFromInteraction(interaction: InteractionRequestData) {
Expand Down
86 changes: 23 additions & 63 deletions test/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@ import GatewayServer from '../src/servers/gateway';
import GCFServer from '../src/servers/gcf';
import { createBasicCommand } from './util/commands';
import { basicCommands } from './util/constants';
import {
deleteGlobalCommand,
deleteGuildCommand,
globalCommands,
guildCommands,
newGlobalCommand,
newGuildCommand,
updateGlobalCommands,
updateGuildCommands
} from './util/nock';
import { globalCommands, guildCommands, updateGlobalCommands, updateGuildCommands } from './util/nock';

describe('SlashCreator', () => {
describe('constructor', () => {
Expand Down Expand Up @@ -256,13 +247,6 @@ describe('SlashCreator', () => {

const cmdsScope = globalCommands(basicCommands),
guildCmdsScope = guildCommands([]),
postScope = newGuildCommand({
id: '0',
name: 'to-create-guild',
description: 'description',
application_id: '1',
version: '1'
}),
putScope = updateGlobalCommands([
{
id: '1',
Expand All @@ -272,19 +256,26 @@ describe('SlashCreator', () => {
version: '1'
}
]),
deleteScope = deleteGlobalCommand('2');
putGuildScope = updateGuildCommands([
{
id: '1',
name: 'to-update',
description: 'description',
application_id: '1',
version: '1'
}
]);

creator.syncCommands();
await expect(cmdsScope, 'requests commands').to.have.been.requested;
await expect(deleteScope, 'deletes old commands').to.have.been.requested;
await expect(putScope, 'updates commands').to.have.been.requestedWith([
{ id: '1', name: 'to-update', description: 'description' }
{ id: '1', name: 'to-update', description: 'description' },
{ id: '3', name: 'to-leave-alone', description: 'description' }
]);
await expect(guildCmdsScope, 'requests guild commands').to.have.been.requested;
await expect(postScope, 'creates new guild commands').to.have.been.requestedWith({
name: 'to-create-guild',
description: 'description'
});
await expect(putGuildScope, 'updates guild commands').to.have.been.requestedWith([
{ name: 'to-create-guild', description: 'description' }
]);
});
});

Expand All @@ -301,14 +292,6 @@ describe('SlashCreator', () => {
.registerCommand(createBasicCommand({ name: 'to-leave-alone', guildIDs: '123' }));

const cmdsScope = guildCommands(basicCommands),
postScope = newGuildCommand({
id: '0',
name: 'to-create',
description: 'description',
guild_id: '123',
application_id: '1',
version: '1'
}),
putScope = updateGuildCommands([
{
id: '1',
Expand All @@ -318,23 +301,15 @@ describe('SlashCreator', () => {
application_id: '1',
version: '1'
}
]),
deleteScope = deleteGuildCommand('2');
]);

const promise = expect(creator.syncCommandsIn('123')).to.be.fulfilled;
await expect(cmdsScope, 'requests commands').to.have.been.requested;
await expect(deleteScope, 'deletes old commands').to.have.been.requested;
await expect(putScope, 'updates commands').to.have.been.requestedWith([
{
id: '1',
name: 'to-update',
description: 'description'
}
{ id: '1', name: 'to-update', description: 'description' },
{ id: '3', name: 'to-leave-alone', description: 'description' },
{ name: 'to-create', description: 'description' }
]);
await expect(postScope, 'creates new commands').to.have.been.requestedWith({
name: 'to-create',
description: 'description'
});
return promise;
});
});
Expand All @@ -352,13 +327,6 @@ describe('SlashCreator', () => {
.registerCommand(createBasicCommand({ name: 'to-leave-alone' }));

const cmdsScope = globalCommands(basicCommands),
postScope = newGlobalCommand({
id: '0',
name: 'to-create',
description: 'description',
application_id: '1',
version: '1'
}),
putScope = updateGlobalCommands([
{
id: '1',
Expand All @@ -367,23 +335,15 @@ describe('SlashCreator', () => {
application_id: '1',
version: '1'
}
]),
deleteScope = deleteGlobalCommand('2');
]);

const promise = expect(creator.syncGlobalCommands()).to.be.fulfilled;
await expect(cmdsScope, 'requests commands').to.have.been.requested;
await expect(deleteScope, 'deletes old commands').to.have.been.requested;
await expect(putScope, 'updates commands').to.have.been.requestedWith([
{
id: '1',
name: 'to-update',
description: 'description'
}
{ id: '1', name: 'to-update', description: 'description' },
{ id: '3', name: 'to-leave-alone', description: 'description' },
{ name: 'to-create', description: 'description' }
]);
await expect(postScope, 'creates new commands').to.have.been.requestedWith({
name: 'to-create',
description: 'description'
});
return promise;
});
});
Expand Down