Squashed commit of the following:

commit e8a57468de
Author: Tarrgon <61888458+Tarrgon@users.noreply.github.com>
Date:   Sat May 31 11:10:56 2025 -0400

    Add record command

commit 5a8ca6bb54
Author: Tarrgon <61888458+Tarrgon@users.noreply.github.com>
Date:   Sat May 31 11:10:30 2025 -0400

    Move context menus to their own folder

commit 2d7c14ed7a
Merge: 33a6ba8 5107759
Author: Tarrgon <61888458+Tarrgon@users.noreply.github.com>
Date:   Sat May 31 10:54:27 2025 -0400

    Merge branch 'alt-data' into records

commit 5107759923
Author: Tarrgon <61888458+Tarrgon@users.noreply.github.com>
Date:   Fri May 30 15:26:21 2025 -0400

    Recursive alt lookup data
This commit is contained in:
Tarrgon
2025-05-31 11:11:37 -04:00
parent 33a6ba893b
commit f5268ace77
11 changed files with 306 additions and 50 deletions
+29 -25
View File
@@ -1,16 +1,24 @@
import { Guild } from 'discord.js';
import { config } from '../config';
import { E621Post, E621User } from '../types';
import { E621Post, E621User, Record } from '../types';
import { Database } from '../shared/Database';
const BLACKLISTED_TAGS: string[] = [];
const BLACKLISTED_NONSAFE_TAGS: string[] = ['young'];
const USER_AGENT = 'E621DiscordBot';
const E621_NAME_URL = `${config.E621_BASE_URL}/users/{idOrName}.json`;
const E621_POST_URL = `${config.E621_BASE_URL}/posts/{id}.json`;
const E621_MD5_POST_URL = `${config.E621_BASE_URL}/posts.json?md5={md5}`;
export async function getE621User(idOrName: string | number): Promise<E621User | null> {
const res = await fetch(E621_NAME_URL.replace('{idOrName}', idOrName.toString()), {
async function request(path: string, query?: { [name: string]: string }): Promise<any> {
const url = new URL(config.E621_BASE_URL!);
url.pathname = path + '.json';
if (query) {
for (const [name, value] of Object.entries(query)) {
url.searchParams.set(name, value);
}
}
const res = await fetch(url, {
headers: {
'User-Agent': USER_AGENT
}
@@ -18,31 +26,19 @@ export async function getE621User(idOrName: string | number): Promise<E621User |
if (!res.ok) return null;
return await res.json() as E621User;
return await res.json();
}
export async function getE621User(idOrName: string | number): Promise<E621User | null> {
return await request(`/users/${idOrName}`) as E621User;
}
export async function getE621Post(id: string | number): Promise<E621Post | null> {
const res = await fetch(E621_POST_URL.replace('{id}', id.toString()), {
headers: {
'User-Agent': USER_AGENT
}
});
if (!res.ok) return null;
return (await res.json() as { post: E621Post }).post as E621Post;
return (await request(`/posts/${id}`)).post as E621Post;
}
export async function getE621PostByMd5(md5: string): Promise<E621Post | null> {
const res = await fetch(E621_MD5_POST_URL.replace('{md5}', md5), {
headers: {
'User-Agent': USER_AGENT
}
});
if (!res.ok) return null;
return (await res.json() as { post: E621Post }).post as E621Post;
return (await request('/posts', { md5 })).post as E621Post;
}
export function hasBlacklistedTags(post: E621Post): boolean {
@@ -62,4 +58,12 @@ export function getPostUrl(post: E621Post): string {
export async function userIsBanned(idOrName: string | number): Promise<boolean> {
const user = await getE621User(idOrName);
return user?.is_banned ?? false;
}
export async function getUserRecords(id: number): Promise<Record[]> {
const records = await request('/user_feedbacks', { 'search[user_id]': id.toString() });
if (records.user_feedbacks) return [];
return records as Record[];
}