Skip to content

Commit

Permalink
feat errors!!
Browse files Browse the repository at this point in the history
  • Loading branch information
oddyamill committed Oct 6, 2024
1 parent 6ccd6df commit dc8fa7e
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 146 deletions.
145 changes: 69 additions & 76 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"devDependencies": {
"@cloudflare/workers-types": "^4.20240117.0",
"typescript": "^5.0.4",
"wrangler": "^3.73.0"
"wrangler": "^3.78.12"
},
"dependencies": {
"@oddyamill/discord-workers": "^1.1.5",
Expand Down
31 changes: 13 additions & 18 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// TODO: refactoring

import {
ComponentType,
InteractionResponseType,
Expand All @@ -17,13 +15,14 @@ import {
makeMessageURL,
resolveArguments,
resolveId,
resolveMedia,
} from './utils'

export const command = async (interaction: Interaction, t: Translator, env: Env, ctx: ExecutionContext): Promise<Response> => {
const { text, message } = resolveArguments(interaction)
const id = await resolveId(text)

if (!id) {
if (id === undefined) {
return respond(InteractionResponseType.ChannelMessageWithSource, {
content: t('no_tiktok'),
flags: MessageFlags.Ephemeral,
Expand All @@ -32,26 +31,22 @@ export const command = async (interaction: Interaction, t: Translator, env: Env,

ctx.waitUntil(
(async () => {
const data = await getTikTok(id, env)
const [data, response, error] = await getTikTok(id)

if (!data) {
return editResponse(interaction, {
content: t('video_not_loading'),
})
if (error !== null) {
return editResponse(interaction, { content: t(error) })
}

const { format, author, stream, url } = data
const media = await resolveMedia(data, response, env)

if (!stream) {
return editResponse(interaction, {
content: t('video_not_supported'),
})
if (media === null) {
return editResponse(interaction, { content: t('video_not_supported') })
}

const { format, stream } = media

if (!stream.ok) {
return editResponse(interaction, {
content: t('tiktok_blocked'),
})
return editResponse(interaction, { content: t('tiktok_blocked') })
}

const body = new FormData(),
Expand All @@ -60,8 +55,8 @@ export const command = async (interaction: Interaction, t: Translator, env: Env,
{
type: ComponentType.ActionRow,
components: [
makeComponent(t('open_in_tiktok'), url),
await makeAuthorComponent(author.nickname, author.id, author.uniqueId, author.avatarThumb, interaction, env, ctx),
makeComponent(t('open_in_tiktok'), response.url),
await makeAuthorComponent(data.author, interaction, env, ctx),
],
},
],
Expand Down
9 changes: 8 additions & 1 deletion src/localization/en.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
{
"cannot_process_command": "Unable to process the command.",
"no_tiktok": "Unable to find the TikTok. Is the link correct?",
"video_not_loading": "The video is not loading. Please try again later.",
"video_not_found": "Video not found.",
"video_deleted": "Video deleted.",
"service_unavailable": "Service unavailable.",
"video_age_restricted": "This video has been removed due to age restrictions.",
"video_private": "This video is private.",
"video_for_friends": "This video is for friends only.",
"video_for_subscriber": "This video is for subscriber only.",
"video_for_creator": "This video is for creator only.",
"video_not_supported": "This video is not supported.",
"tiktok_blocked": "TikTok has blocked the bot's request. The video will not be available.",
"open_in_tiktok": "Open in TikTok",
Expand Down
Loading

0 comments on commit dc8fa7e

Please sign in to comment.