string-store not finding identifiers other than first in Schema on deserialize typescript

I am working on some new code for my bot and using the string-store utility. I've gone through https://discord.com/channels/737141877803057244/1303945790057746452 and https://discord.com/channels/737141877803057244/1303856829461168138 and have been able to get the system working with the steps in there. My schema is:
import { Schema, SchemaStore, t } from '@sapphire/string-store';

export const SidekickCustomId = {
registerConfirm: 0,
registerCancel: 1
} as const;

export const SidekickCustomIdStore = new SchemaStore()
.add(
new Schema(SidekickCustomId.registerConfirm)
.string('playerId')
.uint4('characterSlot')
.bit('purchased')
.bit('humanoid')
.int4('cr')
.string('name')
.string('creature')
)
.add(new Schema(SidekickCustomId.registerCancel).snowflake('playerId'));
import { Schema, SchemaStore, t } from '@sapphire/string-store';

export const SidekickCustomId = {
registerConfirm: 0,
registerCancel: 1
} as const;

export const SidekickCustomIdStore = new SchemaStore()
.add(
new Schema(SidekickCustomId.registerConfirm)
.string('playerId')
.uint4('characterSlot')
.bit('purchased')
.bit('humanoid')
.int4('cr')
.string('name')
.string('creature')
)
.add(new Schema(SidekickCustomId.registerCancel).snowflake('playerId'));
It works fine when serializing and all the properties are accessible. but when I try to access in an associated listener it says deserialized.data does not have property foo after the first.
Solution:
Your store has 2 schemas, so when it deserializes a buffer, it can be one of the two. Compare the id or cast the value to the correct type to fix this.
Jump to solution
3 Replies
WhacK
WhacKOP3w ago
export class SidekickRegisterCancelHandler extends InteractionHandler<{ interactionHandlerType: InteractionHandlerTypes.Button }> {
public async run(interaction: ButtonInteraction) {
try {
const player = await getPlayerByDiscordId(interaction.user.id);
if (isNullish(player)) {
throw new Error('Unable to retrieve player');
}
if (player.active_character && player.active_character.active) {
const deserialized = SidekickCustomIdStore.deserialize(interaction.customId).data;

// const registeredSidekick = await registerSidekickToCharacter(
// deserialized.playerId.toString(),
// deserialized.characterSlot,
// deserialized.name,
// SidekickTier[deserialized.cr],
// SidekickCreatureTypes[deserialized.creature],
// deserialized.purchased
// );

return interaction.editReply({ content: 'Sidekick registered', embeds: [], components: [] });
} else {
throw new Error('Your active character must be active to register sidekicks.');
}
} catch (error) {
this.container.logger.debug(error.message ?? 'Unexpected error cancelling transaction.', error);
if (interaction.deferred) {
return await interaction.editReply(error.message ?? 'Unexpected error cancelling transaction.');
} else {
return await interaction.update(error.message ?? 'Unexpected error cancelling transaction.');
}
}
}
}
export class SidekickRegisterCancelHandler extends InteractionHandler<{ interactionHandlerType: InteractionHandlerTypes.Button }> {
public async run(interaction: ButtonInteraction) {
try {
const player = await getPlayerByDiscordId(interaction.user.id);
if (isNullish(player)) {
throw new Error('Unable to retrieve player');
}
if (player.active_character && player.active_character.active) {
const deserialized = SidekickCustomIdStore.deserialize(interaction.customId).data;

// const registeredSidekick = await registerSidekickToCharacter(
// deserialized.playerId.toString(),
// deserialized.characterSlot,
// deserialized.name,
// SidekickTier[deserialized.cr],
// SidekickCreatureTypes[deserialized.creature],
// deserialized.purchased
// );

return interaction.editReply({ content: 'Sidekick registered', embeds: [], components: [] });
} else {
throw new Error('Your active character must be active to register sidekicks.');
}
} catch (error) {
this.container.logger.debug(error.message ?? 'Unexpected error cancelling transaction.', error);
if (interaction.deferred) {
return await interaction.editReply(error.message ?? 'Unexpected error cancelling transaction.');
} else {
return await interaction.update(error.message ?? 'Unexpected error cancelling transaction.');
}
}
}
}
when I hover it shows everything there, and the logger statements show it properly deserialized in the console
Solution
kyra
kyra3w ago
Your store has 2 schemas, so when it deserializes a buffer, it can be one of the two. Compare the id or cast the value to the correct type to fix this.
WhacK
WhacKOP3w ago
Ok I'll try figuring that out. Thank you.
const deserialized = SidekickCustomIdStore.deserialize(interaction.customId).data as {
playerId: string;
characterSlot: number;
purchased: 0 | 1;
humanoid: boolean;
cr: number;
name: string;
creature: string;
};
const deserialized = SidekickCustomIdStore.deserialize(interaction.customId).data as {
playerId: string;
characterSlot: number;
purchased: 0 | 1;
humanoid: boolean;
cr: number;
name: string;
creature: string;
};
Seemed to fix it thanks
Want results from more Discord servers?
Add your server