Interaction Handlers

Am I required to specify to sapphire where my interaction handlers directory is? A interaction-handlers directory doesn't seem to be detected. - ├─ Loaded 0 interaction-handlers.
Solution:
For AnswerOverflow: sapphire auto detects the interaction-handlers directory
Jump to solution
16 Replies
Favna
Favna2y ago
no, the interaction-handlers directory is correct. It should be in the same directory as things like commands and listeners
net-tech-
net-tech-OP2y ago
and it is
net-tech-
net-tech-OP2y ago
https://hst.sh/rupiduyoti.typescript Maybe its the handler itself
Favna
Favna2y ago
did you rebuild your code
net-tech-
net-tech-OP2y ago
I ran tsc yes
Favna
Favna2y ago
idk then... needs full code ig but it works for many other bots
net-tech-
net-tech-OP2y ago
.
Favna
Favna2y ago
for example check @Dragonite's source code full code as in the full repo
net-tech-
net-tech-OP2y ago
wait i need to have sub folders for every type of interaction? E.g. interactions-handlers => modals => modal.ts
Favna
Favna2y ago
no that's optional
net-tech-
net-tech-OP2y ago
okay so i copied the example from the guide at https://www.sapphirejs.dev/docs/Guide/interaction-handlers/modals and it still doesn't seem to be detecting
Sapphire Framework
Modals | Sapphire
Modals are like your annoying pop-ups, but cooler! These interactions will display a pop-up window on the user's client,
net-tech-
net-tech-OP2y ago
import { InteractionHandler, InteractionHandlerTypes, PieceContext } from '@sapphire/framework';
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ModalSubmitInteraction } from 'discord.js';
import { startBenchmark } from '../services/benchmarkRunner';

export class ModalHandler extends InteractionHandler {
public constructor(ctx: PieceContext, options: InteractionHandler.Options) {
super(ctx, {
...options,
interactionHandlerType: InteractionHandlerTypes.ModalSubmit
});
}

public override parse(interaction: ModalSubmitInteraction) {
if (interaction.customId !== 'benchmark') return this.none();

return this.some();
}

public async run(interaction: ModalSubmitInteraction) {
console.log('1');
const controlRow = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder().setCustomId('abort').setLabel('Abort').setStyle(ButtonStyle.Danger)
);

const iterations = interaction.fields.getTextInputValue('iterations') ?? '1000';
const case1 = interaction.fields.getTextInputValue('case-1');
const case2 = interaction.fields.getTextInputValue('case-2');

// If we cannot parse a number, we abort.
if (isNaN(Number(iterations))) return interaction.reply({ content: 'Unable to parse number of iterations. Please provide a valid number.' });

// If the code is empty, we abort.
if (!(case1 && case2)) return interaction.reply({ content: 'Please provide code for both test cases.' });

const startedTimestamp = `<t:${Math.floor(Date.now() / 1000)}:R>`;

await interaction.reply({
content: `Benchmark with ${iterations} iterations in progress - Started ${startedTimestamp}.`,
components: [controlRow]
});

await startBenchmark(Number(iterations), case1, case2);

interaction.reply({
content: `Benchmark finished - Started ${startedTimestamp}.`
});
}
}
import { InteractionHandler, InteractionHandlerTypes, PieceContext } from '@sapphire/framework';
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ModalSubmitInteraction } from 'discord.js';
import { startBenchmark } from '../services/benchmarkRunner';

export class ModalHandler extends InteractionHandler {
public constructor(ctx: PieceContext, options: InteractionHandler.Options) {
super(ctx, {
...options,
interactionHandlerType: InteractionHandlerTypes.ModalSubmit
});
}

public override parse(interaction: ModalSubmitInteraction) {
if (interaction.customId !== 'benchmark') return this.none();

return this.some();
}

public async run(interaction: ModalSubmitInteraction) {
console.log('1');
const controlRow = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder().setCustomId('abort').setLabel('Abort').setStyle(ButtonStyle.Danger)
);

const iterations = interaction.fields.getTextInputValue('iterations') ?? '1000';
const case1 = interaction.fields.getTextInputValue('case-1');
const case2 = interaction.fields.getTextInputValue('case-2');

// If we cannot parse a number, we abort.
if (isNaN(Number(iterations))) return interaction.reply({ content: 'Unable to parse number of iterations. Please provide a valid number.' });

// If the code is empty, we abort.
if (!(case1 && case2)) return interaction.reply({ content: 'Please provide code for both test cases.' });

const startedTimestamp = `<t:${Math.floor(Date.now() / 1000)}:R>`;

await interaction.reply({
content: `Benchmark with ${iterations} iterations in progress - Started ${startedTimestamp}.`,
components: [controlRow]
});

await startBenchmark(Number(iterations), case1, case2);

interaction.reply({
content: `Benchmark finished - Started ${startedTimestamp}.`
});
}
}
I already checked the custom id in the command trigger the modal, it has the custom id of benchmark the same as the listener i gave up Alright so uh it seems node wont detect any new files
Favna
Favna2y ago
something is seriously wrong with your setup then consider all the base factors of using sapphire
net-tech-
net-tech-OP2y ago
yeah im trying to figure it out @Favna just getting the issue from <#1053123174012813362>, is there any alternative to switching to yarn? switching to yarn is more of a workaround than a solution Is parse required for interaction handlers? If not I'm happy to remove it and to it directly in run
Favna
Favna2y ago
yes switching to pnpm, idk.
Solution
net-tech-
net-tech-2y ago
For AnswerOverflow: sapphire auto detects the interaction-handlers directory
Want results from more Discord servers?
Add your server