Skip to content

Commit

Permalink
fix: ids
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed Jan 7, 2024
1 parent dda104a commit 43cb703
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 46 deletions.
9 changes: 5 additions & 4 deletions src/core/id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import { CommandType, EventType } from './structures';
/**
* Construct unique ID for a given interaction object.
* @param event The interaction object for which to create an ID.
* @returns A unique string ID based on the type and properties of the interaction object.
* @returns An array of unique string IDs based on the type and properties of the interaction object.
*/
export function reconstruct<T extends Interaction>(event: T) {
switch (event.type) {
case InteractionType.MessageComponent: {
return `${event.customId}_C${event.componentType}`;
return [`${event.customId}_C${event.componentType}`];
}
case InteractionType.ApplicationCommand:
case InteractionType.ApplicationCommandAutocomplete: {
return `${event.commandName}_A${event.commandType}`;
return [`${event.commandName}_A${event.commandType}`, `${event.commandName}_B`];
}
//Modal interactions are classified as components for sern
case InteractionType.ModalSubmit: {
return `${event.customId}_M`;
return [`${event.customId}_M`];
}
}
}
Expand All @@ -39,6 +39,7 @@ const TypeMap = new Map<number, number>([
[CommandType.StringSelect, ComponentType.StringSelect],
[CommandType.UserSelect, ComponentType.UserSelect],
[CommandType.MentionableSelect, ComponentType.MentionableSelect],
[CommandType.RoleSelect, ComponentType.RoleSelect],
[CommandType.ChannelSelect, ComponentType.ChannelSelect]]);

/*
Expand Down
5 changes: 4 additions & 1 deletion src/core/structures/services/module-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export class DefaultModuleManager implements ModuleManager {
const publishable = 0b000000110;
return Promise.all(
Array.from(entries)
.filter(([id]) => !(Number.parseInt(id.at(-1)!) & publishable))
.filter(([id]) => {
const last_entry = id.at(-1);
return last_entry == 'B' || !(publishable & Number.parseInt(last_entry!));
})
.map(([, path]) => Files.importModule<CommandModule>(path)),
);
}
Expand Down
3 changes: 1 addition & 2 deletions src/handlers/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { createResultResolver } from './event-utils';
import { BaseInteraction, Message } from 'discord.js';
import { CommandType, Context } from '../core';
import type { AnyFunction, Args } from '../types/utility';
import type { Args } from '../types/utility';
import { inspect } from 'node:util'
import type { CommandModule, Module, Processed } from '../types/core-modules';

Expand All @@ -25,7 +25,6 @@ export function dispatchMessage(module: Processed<CommandModule>, args: [Context

export function contextArgs(wrappable: Message | BaseInteraction, messageArgs?: string[]) {
const ctx = Context.wrap(wrappable);
console.log(ctx);
const args = ctx.isMessage() ? ['text', messageArgs!] : ['slash', ctx.options];
return [ctx, args] as [Context, Args];
}
Expand Down
23 changes: 14 additions & 9 deletions src/handlers/event-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,23 @@ export function createInteractionHandler<T extends Interaction>(
return createGenericHandler<Interaction, T, Result<ReturnType<typeof createDispatcher>, void>>(
source,
async event => {
let fullPath = mg.get(Id.reconstruct(event));
if(!fullPath) {
const possibleIds = Id.reconstruct(event);
let fullPaths= possibleIds
.map(id => mg.get(id))
.filter((id): id is string => id !== undefined);

if(fullPaths.length == 0) {
return Err.EMPTY;
}
const [ path ] = fullPaths;
return Files
.defaultModuleLoader<Processed<CommandModule>>(fullPath)
.then(payload => Ok(createDispatcher({
module: payload.module,
event,
})));
},
);
.defaultModuleLoader<Processed<CommandModule>>(path)
.then(payload => Ok(createDispatcher({
module: payload.module,
event,
})));
},
);
}

export function createMessageHandler(
Expand Down
5 changes: 2 additions & 3 deletions src/handlers/ready-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ function register<T extends Processed<AnyModule>>(
validModuleType,
`Found ${module.name} at ${fullPath}, which does not have a valid type`,
);
console.log(id, fullPath);
if (module.type === CommandType.Both) {
module.alias?.forEach(a => manager.set(`${a}_A1`, fullPath));
module.alias?.forEach(a => manager.set(`${a}_B`, fullPath));
} else {
if(module.type === CommandType.Text){
module.alias?.forEach(a => manager.set(`${a}_A0`, fullPath));
module.alias?.forEach(a => manager.set(`${a}_T`, fullPath));
}
}
return Result.wrap(() => manager.set(id, fullPath));
Expand Down
29 changes: 2 additions & 27 deletions test/handlers/id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { describe, expect, it, vi } from 'vitest';
import * as Id from '../../src/core/id';
import { faker } from '@faker-js/faker';
import { CommandModule, CommandType, commandModule } from '../../src';
import { CommandTypeDiscordApi } from '../../src/core/id';

function createRandomCommandModules() {
const randomCommandType = [
Expand Down Expand Up @@ -41,32 +40,8 @@ describe('id resolution', () => {
const metadata = modules.map(createMetadata);
metadata.forEach((meta, idx) => {
const associatedModule = modules[idx];
const am = (appBitField & associatedModule.type) !== 0 ? 'A' : 'C';
let uid = 0;
if (
associatedModule.type === CommandType.Both ||
associatedModule.type === CommandType.Modal
) {
uid = 1;
} else {
uid = CommandTypeDiscordApi[Math.log2(associatedModule.type)];
}
expect(meta.id).toBe(associatedModule.name + '_' + am + uid);
const uid = Id.create(associatedModule.name!, associatedModule.type!);
expect(meta.id).toBe(uid);
});
});

it('maps commands type to discord components or application commands', () => {
expect(CommandTypeDiscordApi[Math.log2(CommandType.Text)]).toBe(1);

expect(CommandTypeDiscordApi[1]).toBe(1);
expect(CommandTypeDiscordApi[Math.log2(CommandType.CtxUser)]).toBe(2);
expect(CommandTypeDiscordApi[Math.log2(CommandType.CtxMsg)]).toBe(3);
expect(CommandTypeDiscordApi[Math.log2(CommandType.Button)]).toBe(2);
expect(CommandTypeDiscordApi[Math.log2(CommandType.StringSelect)]).toBe(3);
expect(CommandTypeDiscordApi[Math.log2(CommandType.UserSelect)]).toBe(5);
expect(CommandTypeDiscordApi[Math.log2(CommandType.RoleSelect)]).toBe(6);
expect(CommandTypeDiscordApi[Math.log2(CommandType.MentionableSelect)]).toBe(7);
expect(CommandTypeDiscordApi[Math.log2(CommandType.ChannelSelect)]).toBe(8);
expect(CommandTypeDiscordApi[6]).toBe(1);
});
});

0 comments on commit 43cb703

Please sign in to comment.