Files
blitzkit/src/core/discord/interactionToURL.ts
T

33 lines
882 B
TypeScript
Raw Normal View History

2023-06-28 18:45:14 +00:00
import {
CacheType,
ChatInputCommandInteraction,
CommandInteractionOption,
} from 'discord.js';
export interface Parameters {
[key: string]: CommandInteractionOption['value'] | undefined | null;
}
2023-06-25 02:32:56 +00:00
export default function interactionToURL(
interaction: ChatInputCommandInteraction<CacheType>,
2023-06-28 18:45:14 +00:00
parameters?: Parameters,
2023-06-25 02:32:56 +00:00
) {
const pathname = [
interaction.commandName,
2023-06-28 18:45:14 +00:00
interaction.options.getSubcommandGroup(false),
interaction.options.getSubcommand(false),
2023-06-25 02:32:56 +00:00
]
.filter(Boolean)
.join('/');
2023-06-28 18:45:14 +00:00
const params = [
...interaction.options.data.map(
(option) => [option.name, option.value] as const,
),
...Object.entries(parameters ?? {}),
]
.filter(([, value]) => value !== undefined && value !== null)
.map(([key, value]) => `${key}=${value}`)
.join('&');
2023-06-25 02:32:56 +00:00
2023-06-28 18:45:14 +00:00
return `${pathname}${params.length > 0 ? `?${params}` : ''}`;
2023-06-25 02:32:56 +00:00
}