summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-09-09 18:07:32 -0700
committerFuwn <[email protected]>2025-09-09 18:07:32 -0700
commit982e16f8d94f74b5ec7dbbdf62fbdbc106530941 (patch)
tree1c4163564305fb45cd9f105462cc2cd783399364
parentrefactor: Move Discord APIs to Discord module (diff)
downloadumabotdiscord-982e16f8d94f74b5ec7dbbdf62fbdbc106530941.tar.xz
umabotdiscord-982e16f8d94f74b5ec7dbbdf62fbdbc106530941.zip
refactor(src): Use arrow functions everywhere
-rw-r--r--src/discord/embeds.ts8
-rw-r--r--src/discord/verification.ts6
-rw-r--r--src/reddit.ts40
3 files changed, 27 insertions, 27 deletions
diff --git a/src/discord/embeds.ts b/src/discord/embeds.ts
index f88cec2..833c831 100644
--- a/src/discord/embeds.ts
+++ b/src/discord/embeds.ts
@@ -1,7 +1,7 @@
import type { DiscordEmbed } from './interfaces.ts';
import type { RedditPost } from '../reddit.ts';
-function decodeHtmlEntities(str: string): string {
+const decodeHtmlEntities = (str: string): string => {
return str
.replace(/&amp;/g, '&')
.replace(/&lt;/g, '<')
@@ -11,9 +11,9 @@ function decodeHtmlEntities(str: string): string {
.replace(/&#x2F;/g, '/')
.replace(/&#x60;/g, '`')
.replace(/&#x3D;/g, '=');
-}
+};
-export function createPostEmbed(post: RedditPost): DiscordEmbed {
+export const createPostEmbed = (post: RedditPost): DiscordEmbed => {
const mediaUrl =
post.media?.reddit_video?.fallback_url ||
post.secure_media?.reddit_video?.fallback_url ||
@@ -87,4 +87,4 @@ export function createPostEmbed(post: RedditPost): DiscordEmbed {
}
return embed;
-}
+};
diff --git a/src/discord/verification.ts b/src/discord/verification.ts
index c60c70e..e4679db 100644
--- a/src/discord/verification.ts
+++ b/src/discord/verification.ts
@@ -1,10 +1,10 @@
import { verifyKey } from 'discord-interactions';
import type { Environment, DiscordInteraction } from './interfaces.ts';
-export async function verifyDiscordRequest(
+export const verifyDiscordRequest = async (
request: Request,
environment: Environment,
-): Promise<{ isValid: boolean; interaction?: DiscordInteraction }> {
+): Promise<{ isValid: boolean; interaction?: DiscordInteraction }> => {
const signature = request.headers.get('x-signature-ed25519');
const timestamp = request.headers.get('x-signature-timestamp');
const body = await request.text();
@@ -21,4 +21,4 @@ export async function verifyDiscordRequest(
if (!isValidRequest) return { isValid: false };
return { interaction: JSON.parse(body) as DiscordInteraction, isValid: true };
-}
+};
diff --git a/src/reddit.ts b/src/reddit.ts
index 0c12739..f57cf75 100644
--- a/src/reddit.ts
+++ b/src/reddit.ts
@@ -51,10 +51,10 @@ export interface RedditResponse {
type SortType = 'hot' | 'top';
-async function fetchWithRetry(
+const fetchWithRetry = async (
url: string,
maxRetries: number = 3,
-): Promise<Response> {
+): Promise<Response> => {
for (let attempt = 0; attempt < maxRetries; attempt++)
try {
await new Promise((resolve) =>
@@ -87,12 +87,12 @@ async function fetchWithRetry(
}
throw new Error('Max retries exceeded');
-}
+};
-export async function fetchRedditPosts(
+export const fetchRedditPosts = async (
sort: SortType = 'hot',
time: TimePeriod = 'day',
-): Promise<RedditPost[]> {
+): Promise<RedditPost[]> => {
const url = `https://www.reddit.com/r/okbuddyumamusume/${sort}.json${sort === 'top' ? `?t=${time}` : ''}`;
const response = await fetchWithRetry(url);
@@ -125,13 +125,13 @@ export async function fetchRedditPosts(
const data: RedditResponse = await response.json();
return data.data.children.map((post) => post.data);
-}
+};
-export function filterPostsByFlair(
+export const filterPostsByFlair = (
posts: RedditPost[],
excludedFlairs: string[] = [],
includedFlairs: string[] = [],
-): RedditPost[] {
+): RedditPost[] => {
return posts.filter((post) => {
if (post.is_gallery) return false;
@@ -166,43 +166,43 @@ export function filterPostsByFlair(
return true;
});
-}
+};
-function getRandomPost(posts: RedditPost[]): RedditPost {
+const getRandomPost = (posts: RedditPost[]): RedditPost => {
if (posts.length === 0)
throw new Error('No posts found matching the criteria');
const randomIndex = Math.floor(Math.random() * posts.length);
return posts[randomIndex];
-}
+};
-export async function getCutePost(): Promise<RedditPost> {
+export const getCutePost = async (): Promise<RedditPost> => {
const posts = await fetchRedditPosts('hot');
const filteredPosts = filterPostsByFlair(posts, ['roleplay', 'announcement']);
return getRandomPost(filteredPosts);
-}
+};
-export async function getRoleplayPost(): Promise<RedditPost> {
+export const getRoleplayPost = async (): Promise<RedditPost> => {
const posts = await fetchRedditPosts('hot');
const filteredPosts = filterPostsByFlair(posts, [], ['roleplay']);
return getRandomPost(filteredPosts);
-}
+};
-export async function getNSFWPost(): Promise<RedditPost> {
+export const getNSFWPost = async (): Promise<RedditPost> => {
const posts = await fetchRedditPosts('hot');
const filteredPosts = filterPostsByFlair(posts, [], ['nsfw']);
return getRandomPost(filteredPosts);
-}
+};
-export async function getTopPost(
+export const getTopPost = async (
time: TimePeriod = 'day',
-): Promise<RedditPost> {
+): Promise<RedditPost> => {
const posts = await fetchRedditPosts('top', time);
const filteredPosts = filterPostsByFlair(posts, ['roleplay', 'announcement']);
return getRandomPost(filteredPosts);
-}
+};