From e4af50d337679ea9cff2e54bb855381dae6387c5 Mon Sep 17 00:00:00 2001 From: Zac Date: Thu, 3 Aug 2023 21:12:29 -0500 Subject: [PATCH] Update: new posts should start with embeds instead of show poster (#15) --- src/commands/link.ts | 2 +- src/commands/post.ts | 27 ++++++++++++++------------- src/commands/search.ts | 4 ++-- src/lib/messages.ts | 2 +- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/commands/link.ts b/src/commands/link.ts index f2522c3..017c421 100644 --- a/src/commands/link.ts +++ b/src/commands/link.ts @@ -118,7 +118,7 @@ export const command: CommandV2 = { await channel.send({ content: `Linked \`${tvdbSeries.name}\` to <#${channel.id}>`, - embeds: [await buildShowEmbed(imdbId, tvdbSeries, show.destinations)] + embeds: [buildShowEmbed(imdbId, tvdbSeries, show.destinations)] }) messages.push(`Linked show \`${tvdbSeries.name}\` (${imdbId})`) diff --git a/src/commands/post.ts b/src/commands/post.ts index 945e03e..e4f56f0 100644 --- a/src/commands/post.ts +++ b/src/commands/post.ts @@ -1,4 +1,4 @@ -import { type Channel, type ChannelManager, ChannelType, type ChatInputCommandInteraction, Collection, PermissionFlagsBits, SlashCommandBuilder, type TextBasedChannel, type ThreadChannel } from 'discord.js' +import { type Channel, ChannelType, type ChatInputCommandInteraction, Collection, PermissionFlagsBits, SlashCommandBuilder, type TextBasedChannel, type ThreadChannel, type APIEmbed } from 'discord.js' import client from '../lib/prisma' import { type CommandV2 } from '../interfaces/command' import { ProgressMessageBuilder } from '../lib/progressMessages' @@ -97,8 +97,11 @@ export const command: CommandV2 = { await progress.sendNextStep() // start step 3 for (const [imdbId, series] of seriesList) { + const embed = buildShowEmbed(imdbId, series.series) + try { - const newPost = await createForumPost(interaction.client.channels, series.series, tvForum) + const forumChannel = await interaction.client.channels.fetch(tvForum) + const newPost = await createForumPost(forumChannel, embed, series.series.name) seriesList.set(imdbId, { series: series.series, post: newPost @@ -122,10 +125,6 @@ export const command: CommandV2 = { const show = await saveShowToDB(imdbId, tvDBSeries.id, tvDBSeries.name, post as TextBasedChannel) - await post.send({ - embeds: [await buildShowEmbed(imdbId, tvDBSeries, show.destinations)] - }) - await updateEpisodes(show.imdbId, show.tvdbId, series.series) messages.push(`Created post for \`${tvDBSeries.name}\` (${imdbId}) - <#${post.id}>`) @@ -200,21 +199,23 @@ async function checkForExistingPosts (imdbId: string, tvForum: string): Promise< * @param tvForumId discord forum to create a post in * @returns the created forum thread */ -async function createForumPost (channels: ChannelManager, tvdbSeries: SeriesExtendedRecord, tvForumId: string): Promise> { - const forumChannel = await channels.fetch(tvForumId) - - if (forumChannel == null || !isForumChannel(forumChannel)) { +async function createForumPost (channel: Channel | null, embed: APIEmbed, seriesName: string): Promise> { + if (channel == null || !isForumChannel(channel)) { throw new ProgressError('No tv forum found') } // create the forum post - return await forumChannel.threads.create({ - name: tvdbSeries.name, + const result = await channel.threads.create({ + name: seriesName, autoArchiveDuration: 10080, message: { - content: `${tvdbSeries.image}` + embeds: [embed] } }) + + await result.lastMessage?.pin() + + return result } /** diff --git a/src/commands/search.ts b/src/commands/search.ts index 2eee911..21f89dd 100644 --- a/src/commands/search.ts +++ b/src/commands/search.ts @@ -38,7 +38,7 @@ export const command: CommandV2 = { } }) - return await interaction.editReply({ embeds: [await buildShowEmbed(imdbId, series, show?.destinations ?? [])] }) + return await interaction.editReply({ embeds: [buildShowEmbed(imdbId, series, show?.destinations ?? [])] }) } const series = await getSeriesByName(query) @@ -49,7 +49,7 @@ export const command: CommandV2 = { if (imdbId == null) return await interaction.editReply('Show not found') - return await interaction.editReply({ embeds: [await buildShowEmbed(imdbId, series, [])] }) + return await interaction.editReply({ embeds: [buildShowEmbed(imdbId, series, [])] }) }, async executeAutoComplate (app: App, interaction: AutocompleteInteraction) { await showSearchAutocomplete(interaction) diff --git a/src/lib/messages.ts b/src/lib/messages.ts index 6c47656..54a44b8 100644 --- a/src/lib/messages.ts +++ b/src/lib/messages.ts @@ -2,7 +2,7 @@ import { type Destination } from '@prisma/client' import { type APIEmbed, type APIEmbedField } from 'discord.js' import { type SeriesExtendedRecord } from '../interfaces/tvdb.generated' -export async function buildShowEmbed (imdbId: string, tvdbSeries: SeriesExtendedRecord, destinations: Destination[] = []): Promise { +export function buildShowEmbed (imdbId: string, tvdbSeries: SeriesExtendedRecord, destinations: Destination[] = []): APIEmbed { // put together some basic data fields const fields: APIEmbedField[] = [ {