-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
395 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,3 +170,8 @@ dist | |
|
||
.dev.vars | ||
.wrangler/ | ||
|
||
# ide | ||
|
||
.idea | ||
.vscode |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { BotEnv } from '@oddyamill/discord-workers' | ||
|
||
export interface Env extends BotEnv { | ||
DISCORD_TOKEN: string | ||
DISCORD_APPLICATION_ID: string | ||
IMAGE_WORKER_ENDPOINT?: string | ||
IMAGE_WORKER_CLIENT_ID: string | ||
IMAGE_WORKER_CLIENT_SECRET: string | ||
CACHE: KVNamespace | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
https://gist.github.com/958841 | ||
MIT LICENSE | ||
Copyright 2011 Jon Leighton | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
const encodings = | ||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' | ||
|
||
export const arrayBufferToBase64 = (buffer: ArrayBuffer) => { | ||
const bytes = new Uint8Array(buffer), | ||
byteLength = bytes.byteLength, | ||
byteRemainder = byteLength % 3, | ||
mainLength = byteLength - byteRemainder | ||
|
||
let a, b, c, d, chunk, result = '' | ||
|
||
for (let i = 0; i < mainLength; i = i + 3) { | ||
chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2] | ||
|
||
a = (chunk & 16515072) >> 18 | ||
b = (chunk & 258048) >> 12 | ||
c = (chunk & 4032) >> 6 | ||
d = chunk & 63 | ||
|
||
result += encodings[a] + encodings[b] + encodings[c] + encodings[d] | ||
} | ||
|
||
switch (byteRemainder) { | ||
case 1: | ||
chunk = bytes[mainLength] | ||
|
||
a = (chunk & 252) >> 2 | ||
b = (chunk & 3) << 4 | ||
|
||
result += encodings[a] + encodings[b] + '==' | ||
break | ||
|
||
case 2: | ||
chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1] | ||
|
||
a = (chunk & 64512) >> 10 | ||
b = (chunk & 1008) >> 4 | ||
c = (chunk & 15) << 2 | ||
|
||
result += encodings[a] + encodings[b] + encodings[c] + '=' | ||
break | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { APIMessageComponentEmoji } from 'discord-api-types/v10' | ||
import { EMOJI_CACHE_TTL } from '../constants' | ||
import { Env } from '../env' | ||
|
||
export const getComponentEmojiFromCache = async ( | ||
env: Env, | ||
authorId: string | ||
): Promise<APIMessageComponentEmoji | undefined> => { | ||
return (await env.CACHE.get(authorId, 'json')) ?? undefined | ||
} | ||
|
||
export const putComponentEmojiToCache = async ( | ||
env: Env, | ||
authorId: string, | ||
emoji: APIMessageComponentEmoji | ||
): Promise<void> => { | ||
await env.CACHE.put(authorId, JSON.stringify(emoji), { expirationTtl: EMOJI_CACHE_TTL }) | ||
} | ||
|
||
export const deleteComponentEmojiFromCache = async ( | ||
env: Env, | ||
authorId: string | ||
): Promise<void> => { | ||
await env.CACHE.delete(authorId) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { APIButtonComponentWithURL } from 'discord-api-types/v10' | ||
import { makeComponent } from './makeComponent' | ||
import { Env } from '../env' | ||
import { Interaction } from '../interaction' | ||
import { createApplicationEmoji } from '@oddyamill/discord-workers' | ||
import { arrayBufferToBase64 } from './arrayBufferToBase64' | ||
import { getComponentEmojiFromCache, putComponentEmojiToCache } from './emojiCache' | ||
import { TIKTOK_USER_ENDPOINT } from '../constants' | ||
|
||
export const makeAuthorComponent = async ( | ||
label: string, | ||
authorId: string, | ||
authorUsername: string, | ||
authorAvatar: string, | ||
interaction: Interaction, | ||
env: Env, | ||
ctx: ExecutionContext, | ||
): Promise<APIButtonComponentWithURL> => { | ||
const component = makeComponent( | ||
label, | ||
TIKTOK_USER_ENDPOINT + authorUsername, | ||
await getComponentEmojiFromCache(env, authorId) | ||
) | ||
|
||
if (component.emoji !== undefined) { | ||
return component | ||
} | ||
|
||
const url = | ||
env.IMAGE_WORKER_ENDPOINT !== undefined | ||
? env.IMAGE_WORKER_ENDPOINT + '/circle?url=' + encodeURIComponent(authorAvatar) | ||
: authorAvatar | ||
|
||
const image = await fetch(url, { | ||
headers: { | ||
'Cf-Access-Client-Id': env.IMAGE_WORKER_CLIENT_ID, | ||
'Cf-Access-Client-Secret': env.IMAGE_WORKER_CLIENT_SECRET, | ||
}, | ||
}) | ||
|
||
if (!image.ok) { | ||
return component | ||
} | ||
|
||
try { | ||
const emoji = await createApplicationEmoji( | ||
env, | ||
interaction.application_id, | ||
authorId, | ||
'data:image/png;base64,' + arrayBufferToBase64(await image.arrayBuffer()) | ||
) | ||
|
||
component.emoji = { id: emoji.id!, name: emoji.name! } | ||
ctx.waitUntil(putComponentEmojiToCache(env, authorId, component.emoji)) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
|
||
return component | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
import { | ||
APIButtonComponentWithURL, | ||
APIMessageComponentEmoji, | ||
ButtonStyle, | ||
ComponentType, | ||
} from 'discord-api-types/v10' | ||
|
||
export const makeComponent = (label: string, url: string): APIButtonComponentWithURL => { | ||
export const makeComponent = (label: string, url: string, emoji?: APIMessageComponentEmoji): APIButtonComponentWithURL => { | ||
return { | ||
type: ComponentType.Button, | ||
style: ButtonStyle.Link, | ||
label, | ||
url, | ||
emoji, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters