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

24 lines
737 B
TypeScript
Raw Normal View History

2023-10-11 18:26:35 -05:00
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
2023-06-28 18:45:14 +00:00
export interface Parameters {
2023-10-11 18:26:35 -05:00
[key: string]: string | number | boolean | undefined | null;
2023-06-28 18:45:14 +00:00
}
2023-06-25 02:32:56 +00:00
2023-10-14 11:32:02 -05:00
export default function commandToURL(
2023-06-25 02:32:56 +00:00
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('/');
const params = (parameters ? Object.entries(parameters) : [])
2023-06-28 18:45:14 +00:00
.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
}