Skip to content

Commit

Permalink
* Posted content now uses embeds
Browse files Browse the repository at this point in the history
* Fixed "iconURL" properties on embeds
* Small text changes
  • Loading branch information
danthonywalker committed Aug 14, 2023
1 parent 2123737 commit 7a8f251
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/features/creators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const json = new SlashCommandBuilder()
.addSubcommand((subcommand) =>
subcommand
.setName(Subcommand.UNSUBSCRIBE)
.setDescription("Unsubscribe to creators"),
.setDescription("Unsubscribe from creators"),
)
.toJSON();

Expand Down
57 changes: 38 additions & 19 deletions src/features/creators/post/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import PlaylistItemSnippet = youtube_v3.Schema$PlaylistItemSnippet;

import type { Guild } from "discord.js";
import {
bold,
ChannelType,
DiscordAPIError,
EmbedBuilder,
Events,
roleMention,
} from "discord.js";
import { compress } from "compress-tag";
import loggerFactory from "pino";

import discord from "../../../services/discord";
import { getThumbnailUrl, getVideoUrl } from "../../../services/youtube";
import discord, { Color } from "../../../services/discord";
import {
getChannelUrl,
getThumbnailUrl,
getVideoUrl,
} from "../../../services/youtube";
import sleep from "../../../sleep";

import type { CreatorSubscription } from "./database";
Expand Down Expand Up @@ -64,7 +67,10 @@ const postFromYouTube = async (creatorSubscription: CreatorSubscription) => {
creatorMentionRoleId,
} = creatorSubscription;

const { contentDetails, snippet } = await youtube.getChannel(creatorDomainId);
const channel = await youtube.getChannel(creatorDomainId);
const { contentDetails, id: channelId, snippet } = channel;
if (typeof channelId !== "string") return;

const { relatedPlaylists } = contentDetails ?? {};
const { uploads } = relatedPlaylists ?? {};
if (uploads === undefined) return;
Expand All @@ -73,39 +79,52 @@ const postFromYouTube = async (creatorSubscription: CreatorSubscription) => {
if (typeof channelName !== "string") return;

const post = async (video: PlaylistItemSnippet = {}) => {
const { description, publishedAt, resourceId, title } = video;
const { description, publishedAt, resourceId, title: videoTitle } = video;
const { videoId } = resourceId ?? {};

// prettier-ignore
if (typeof publishedAt !== "string" || typeof title !== "string") return false;
if (typeof publishedAt !== "string" || typeof videoTitle !== "string") return false;
if (typeof videoId !== "string" || videoId === lastContentId) return false;

const videoDate = new Date(publishedAt);
if (videoDate < createdAt) return false;

const videoUrl = getVideoUrl(videoId);
const header =
const thumbnailUrl = getThumbnailUrl(thumbnails);

const content =
creatorMentionRoleId === null
? videoUrl
: `${roleMention(creatorMentionRoleId)}\n${videoUrl}`;

const rawContent = compress`
${header}
\n\n${bold("Title")}
\n${title}
\n\n${bold("Description")}
\n${description ?? ""}
`;
const author = {
iconURL: thumbnailUrl,
name: channelName,
url: getChannelUrl(channelId),
};

const footer = {
iconURL: thumbnailUrl,
text: `${channelName} - ${videoTitle}`,
};

const embed = new EmbedBuilder()
.setAuthor(author)
.setColor(Color.INFORMATIONAL)
.setDescription(description ?? null)
.setFooter(footer)
.setTitle(videoTitle)
.setTimestamp(videoDate)
.setURL(videoUrl);

const content = rawContent.substring(0, 2000);
const threadId = creatorParentId === null ? undefined : creatorChannelId;
const threadName = `${channelName} - ${title}`.substring(0, 100);

const threadName = `${channelName} - ${videoTitle}`.substring(0, 100);
const webhookThreadName =
creatorChannelType === ChannelType.GuildForum ? threadName : undefined;

const message = await webhook.send({
avatarURL: getThumbnailUrl(thumbnails),
avatarURL: thumbnailUrl,
embeds: [embed],
content,
username: channelName,
threadId,
Expand Down
4 changes: 2 additions & 2 deletions src/features/creators/subscribe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const getEmbedByYoutubeChannel = ({
channelName === null
? null
: {
iconUrl: thumbnailUrl,
iconURL: thumbnailUrl ?? undefined,
name: channelName,
url: channelUrl ?? undefined,
};
Expand All @@ -78,7 +78,7 @@ const getEmbedByYoutubeChannel = ({
channelName === null
? null
: {
iconUrl: thumbnailUrl,
iconURL: thumbnailUrl ?? undefined,
text: channelName,
};

Expand Down
4 changes: 2 additions & 2 deletions src/features/creators/unsubscribe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ export default async (interaction: ChatInputCommandInteraction) => {

const options = () => {
// prettier-ignore
const applyButtonLabel = "I am finished selecting creators to unsubscribe to";
const applyButtonLabel = "I am finished selecting creators to unsubscribe from";
const cancelButtonLabel = "I do not want to unsubscribe from any creators";

const description =
"Use the select menu to choose the channel and creator to unsubscribe from.";
"Use the select menu to choose the creators to unsubscribe from.";

const embed = new EmbedBuilder()
.setColor(Color.INFORMATIONAL)
Expand Down

0 comments on commit 7a8f251

Please sign in to comment.