|
| 1 | +import { AuthenticatorService } from '@/authenticator/authenticator.service'; |
| 2 | +import { PrismaService } from '@/prisma/prisma.service'; |
| 3 | +import { Injectable, Logger, UseInterceptors } from '@nestjs/common'; |
| 4 | +import { ConfigService } from '@nestjs/config'; |
| 5 | +import { EmbedBuilder, hideLinkEmbed, PermissionFlagsBits } from 'discord.js'; |
| 6 | +import { SlashCommand, Context, SlashCommandContext, Options } from 'necord'; |
| 7 | +import { URLSearchParams } from 'url'; |
| 8 | +import { CreateDto } from '../dto/create.dto'; |
| 9 | +import { EventAutocompleteInterceptor } from '../interceptors/event.interceptor'; |
| 10 | + |
| 11 | +@Injectable() |
| 12 | +export class CreateCommand { |
| 13 | + private readonly logger = new Logger(CreateCommand.name); |
| 14 | + |
| 15 | + constructor( |
| 16 | + private readonly db: PrismaService, |
| 17 | + private readonly config: ConfigService, |
| 18 | + private readonly authenticator: AuthenticatorService, |
| 19 | + ) {} |
| 20 | + |
| 21 | + @UseInterceptors(EventAutocompleteInterceptor) |
| 22 | + @SlashCommand({ |
| 23 | + name: 'create', |
| 24 | + description: 'Create a new event on the spot.', |
| 25 | + guilds: [process.env['GUILD_ID']], |
| 26 | + defaultMemberPermissions: PermissionFlagsBits.ManageRoles, |
| 27 | + }) |
| 28 | + public async onCreate( |
| 29 | + @Context() [interaction]: SlashCommandContext, |
| 30 | + @Options() { eventName, eventType, role, allday, description }: CreateDto, |
| 31 | + ) { |
| 32 | + const frontendUrl = this.config.getOrThrow<string>('FRONTEND_URL'); |
| 33 | + |
| 34 | + const startDate = interaction.createdAt.toISOString(); |
| 35 | + |
| 36 | + const endDate = new Date(startDate); |
| 37 | + |
| 38 | + endDate.setHours(interaction.createdAt.getHours() + 3); |
| 39 | + |
| 40 | + const endDateIso = endDate.toISOString(); |
| 41 | + |
| 42 | + const params = new URLSearchParams(); |
| 43 | + |
| 44 | + params.append('startDate', startDate); |
| 45 | + params.append('endDay', endDateIso); |
| 46 | + |
| 47 | + if (allday) { |
| 48 | + params.append('allDay', allday.toString()); |
| 49 | + } |
| 50 | + |
| 51 | + if (eventType) { |
| 52 | + params.append('eventType', eventType); |
| 53 | + } |
| 54 | + |
| 55 | + if (eventName) { |
| 56 | + params.append('eventName', eventName); |
| 57 | + } |
| 58 | + |
| 59 | + if (role) { |
| 60 | + params.append('role', role.id); |
| 61 | + } |
| 62 | + |
| 63 | + if (description) { |
| 64 | + params.append('description', description); |
| 65 | + } |
| 66 | + |
| 67 | + const createUrl = `${frontendUrl}/event/create?${params.toString()}`; |
| 68 | + |
| 69 | + const successEmbed = new EmbedBuilder() |
| 70 | + .setTitle('Success') |
| 71 | + .setColor('Green').setDescription(` |
| 72 | + Click on the link below to create the event, |
| 73 | + |
| 74 | + ${hideLinkEmbed(createUrl)} |
| 75 | + `); |
| 76 | + |
| 77 | + return interaction.reply({ embeds: [successEmbed], ephemeral: true }); |
| 78 | + } |
| 79 | +} |
0 commit comments