diff --git a/src/commandCenter/commands/Partify.ts b/src/commandCenter/commands/Partify.ts index 7dd101f..b25b6e7 100755 --- a/src/commandCenter/commands/Partify.ts +++ b/src/commandCenter/commands/Partify.ts @@ -6,10 +6,12 @@ import { GuildScheduledEventCreateOptions, GuildScheduledEventEntityType, GuildScheduledEventPrivacyLevel, + ChannelType, + VoiceChannel, + GuildScheduledEvent, } from 'discord.js' import { Command } from '../../Command' import { stringToTime } from '../utils/stringToTime' -import dayjs from 'dayjs' import { fetchOpenAIChatCompletion } from '../../openai' const partySystemPrompt = @@ -36,32 +38,43 @@ export const Partify: Command = { }, ], run: async (_: Client, interaction: CommandInteraction) => { - console.log(interaction.options.get('theme')?.value) const timeInput = interaction.options.get('when')?.value as string const targetTime = stringToTime(timeInput, interaction.createdAt) + if (interaction.guild === null) { + throw new Error("can't get guild") + } + const guild = interaction.guild + const channels = await guild.channels.fetch() + let voiceChan = channels.find( + (chan) => chan?.type == ChannelType.GuildVoice + ) as VoiceChannel + if (voiceChan === undefined || voiceChan === null) { + voiceChan = await guild.channels.create({ + name: 'party', + type: ChannelType.GuildVoice, + }) + } const generatedPartyIdea = await fetchOpenAIChatCompletion( interaction.options.get('theme')?.value as string, partySystemPrompt ) - const partyJson: {name: string; description: string;} = JSON.parse(generatedPartyIdea) - console.log(partyJson) + const partyJson: { name: string; description: string } = + JSON.parse(generatedPartyIdea) // this gives us an object to use in creating the event - // { name: string, description: string } - const newEvent: GuildScheduledEventCreateOptions = { - ...partyJson, - scheduledStartTime: targetTime, - scheduledEndTime: dayjs(targetTime).add(1,"hour").toDate(), - entityType: GuildScheduledEventEntityType.External, - privacyLevel: GuildScheduledEventPrivacyLevel.GuildOnly, - entityMetadata: {location: "voice channel"} - } - // code to generate event goes here - interaction.guild?.scheduledEvents.create(newEvent) + const newEventDetails: GuildScheduledEventCreateOptions = { + ...partyJson, + scheduledStartTime: targetTime, + entityType: GuildScheduledEventEntityType.Voice, + privacyLevel: GuildScheduledEventPrivacyLevel.GuildOnly, + channel: voiceChan, + } - await interaction.followUp({ - content: `OK! You've got a party coming up at `, - }) + const newEvent: GuildScheduledEvent = + await interaction.guild.scheduledEvents.create(newEventDetails) + interaction.followUp( + `OK! Here's what I came up with! 🎉 ${newEvent.url}` + ) }, }