mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-02 04:56:07 +00:00
refactor: moved functions (#252)
* refactor: moved functions - Moved functions - Fixed imports - Removed duplicated file * chore: apply formatting * fix: import
This commit is contained in:
parent
2597c3c18d
commit
d9aa4a56d0
@ -1,6 +1,5 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { Logger, delay, lazyLoadPackage } from '../common';
|
||||
import { snowflakeToTimestamp } from '../structures/extra/functions';
|
||||
import { Logger, delay, lazyLoadPackage, snowflakeToTimestamp } from '../common';
|
||||
import type { WorkerData } from '../websocket';
|
||||
import type { WorkerSendApiRequest } from '../websocket/discord/worker';
|
||||
import { CDNRouter, type ProxyRequestMethod } from './Router';
|
||||
|
@ -1,6 +1,5 @@
|
||||
import { type APIMessageComponentEmoji, type ButtonStyle, ComponentType, type APIButtonComponent } from '../types';
|
||||
import type { EmojiResolvable } from '../common';
|
||||
import { resolvePartialEmoji } from '../structures/extra/functions';
|
||||
import { resolvePartialEmoji, type EmojiResolvable } from '../common';
|
||||
|
||||
/**
|
||||
* Represents a button component.
|
||||
|
@ -1,6 +1,5 @@
|
||||
import { type APIPollMedia, PollLayoutType, type RESTAPIPollCreate } from '../types';
|
||||
import type { DeepPartial, EmojiResolvable, RestOrArray } from '../common';
|
||||
import { resolvePartialEmoji } from '../structures/extra/functions';
|
||||
import { resolvePartialEmoji, type DeepPartial, type EmojiResolvable, type RestOrArray } from '../common';
|
||||
|
||||
export class PollBuilder {
|
||||
constructor(public data: DeepPartial<RESTAPIPollCreate> = {}) {
|
||||
|
@ -21,7 +21,7 @@ import type {
|
||||
StringSelectMenuInteraction,
|
||||
UserSelectMenuInteraction,
|
||||
} from '../structures';
|
||||
import { resolvePartialEmoji } from '../structures/extra/functions';
|
||||
import { resolvePartialEmoji } from '../common/it/utils';
|
||||
import { BaseComponentBuilder, type OptionValuesLength } from './Base';
|
||||
|
||||
export type BuilderSelectMenus =
|
||||
|
@ -237,4 +237,14 @@ export class Formatter {
|
||||
static emojiMention(emojiId: string, name: string | null, animated = false): string {
|
||||
return `<${animated ? 'a' : ''}:${name ?? '_'}:${emojiId}>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a channel link.
|
||||
* @param channelId The ID of the channel.
|
||||
* @param guildId The ID of the guild. Defaults to '@me'.
|
||||
* @returns The formatted channel link.
|
||||
*/
|
||||
static channelLink(channelId: string, guildId?: string) {
|
||||
return `https://discord.com/channels/${guildId ?? '@me'}/${channelId}`;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,17 @@
|
||||
import { promises } from 'node:fs';
|
||||
import { basename, join } from 'node:path';
|
||||
import { EmbedColors, type ColorResolvable, type Logger, type ObjectToLower, type ObjectToSnake } from '..';
|
||||
import {
|
||||
DiscordEpoch,
|
||||
EmbedColors,
|
||||
type EmojiResolvable,
|
||||
type TypeArray,
|
||||
type ColorResolvable,
|
||||
type Logger,
|
||||
type ObjectToLower,
|
||||
type ObjectToSnake,
|
||||
} from '..';
|
||||
import { type APIPartialEmoji, FormattingPatterns } from '../../types';
|
||||
import type { Cache } from '../..';
|
||||
|
||||
/**
|
||||
* Resolves the color to a numeric representation.
|
||||
@ -277,3 +288,64 @@ export function isCloudfareWorker() {
|
||||
//@ts-expect-error
|
||||
return process.platform === 'browser';
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Convert a timestamp to a snowflake.
|
||||
* @param id The timestamp to convert.
|
||||
* @returns The snowflake.
|
||||
*/
|
||||
export function snowflakeToTimestamp(id: string): bigint {
|
||||
return (BigInt(id) >> 22n) + DiscordEpoch;
|
||||
}
|
||||
|
||||
export function resolvePartialEmoji(emoji: EmojiResolvable): APIPartialEmoji | undefined {
|
||||
if (typeof emoji === 'string') {
|
||||
const groups: Partial<APIPartialEmoji> | undefined = emoji.match(FormattingPatterns.Emoji)?.groups;
|
||||
if (groups) {
|
||||
return { animated: !!groups.animated, name: groups.name!, id: groups.id! };
|
||||
}
|
||||
if (emoji.includes('%')) {
|
||||
emoji = encodeURIComponent(emoji);
|
||||
}
|
||||
if (!(emoji.includes(':') || emoji.match(/\d{17,20}/g))) {
|
||||
return { name: emoji, id: null };
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(emoji.id && emoji.name)) return;
|
||||
return { id: emoji.id, name: emoji.name, animated: !!emoji.animated };
|
||||
}
|
||||
|
||||
export async function resolveEmoji(emoji: EmojiResolvable, cache: Cache): Promise<APIPartialEmoji | undefined> {
|
||||
const partial = resolvePartialEmoji(emoji);
|
||||
if (partial) return partial;
|
||||
|
||||
if (typeof emoji === 'string') {
|
||||
if (!emoji.match(/\d{17,20}/g)) return;
|
||||
const fromCache = await cache.emojis?.get(emoji);
|
||||
return fromCache && { animated: fromCache.animated, id: fromCache.id, name: fromCache.name };
|
||||
}
|
||||
|
||||
const fromCache = await cache.emojis?.get(emoji.id!);
|
||||
if (fromCache) return { animated: fromCache.animated, id: fromCache.id, name: fromCache.name };
|
||||
return;
|
||||
}
|
||||
|
||||
export function encodeEmoji(rawEmoji: APIPartialEmoji) {
|
||||
return rawEmoji.id ? `${rawEmoji.name}:${rawEmoji.id}` : `${rawEmoji.name}`;
|
||||
}
|
||||
|
||||
export function hasProps<T extends Record<any, any>>(target: T, props: TypeArray<keyof T>): boolean {
|
||||
if (Array.isArray(props)) {
|
||||
return props.every(x => hasProps(target, x));
|
||||
}
|
||||
if (!((props as T[number]) in target)) {
|
||||
return false;
|
||||
}
|
||||
if (typeof target[props] === 'string' && !target[props as T[number]].length) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import type { RESTGetAPIChannelMessageReactionUsersQuery } from '../../types';
|
||||
import { encodeEmoji, resolveEmoji } from '../../structures/extra/functions';
|
||||
import { encodeEmoji, resolveEmoji } from '../../common/it/utils';
|
||||
import type { EmojiResolvable } from '../types/resolvables';
|
||||
import { BaseShorter } from './base';
|
||||
import { Transformers, type UserStructure } from '../../client/transformers';
|
||||
|
@ -15,7 +15,7 @@ import type { ActionRowMessageComponents } from '../components';
|
||||
import { MessageActionRowComponent } from '../components/ActionRow';
|
||||
import type { MessageWebhookMethodEditParams, MessageWebhookMethodWriteParams } from './Webhook';
|
||||
import { DiscordBase } from './extra/DiscordBase';
|
||||
import { messageLink } from './extra/functions';
|
||||
import { Formatter } from '../common';
|
||||
import { Embed } from '..';
|
||||
import {
|
||||
type PollStructure,
|
||||
@ -65,7 +65,7 @@ export class BaseMessage extends DiscordBase {
|
||||
}
|
||||
|
||||
get url() {
|
||||
return messageLink(this.channelId, this.id, this.guildId);
|
||||
return Formatter.messageLink(this.guildId!, this.channelId, this.id);
|
||||
}
|
||||
|
||||
guild(force = false) {
|
||||
|
@ -39,7 +39,6 @@ import type {
|
||||
import type { GuildMember } from './GuildMember';
|
||||
import type { GuildRole } from './GuildRole';
|
||||
import { DiscordBase } from './extra/DiscordBase';
|
||||
import { channelLink } from './extra/functions';
|
||||
import { Collection, Formatter, type RawFile } from '..';
|
||||
import {
|
||||
type BaseChannelStructure,
|
||||
@ -74,7 +73,7 @@ export class BaseChannel<T extends ChannelType> extends DiscordBase<APIChannelBa
|
||||
|
||||
/** The URL to the channel */
|
||||
get url() {
|
||||
return channelLink(this.id);
|
||||
return Formatter.channelLink(this.id);
|
||||
}
|
||||
|
||||
fetch(force = false) {
|
||||
@ -210,7 +209,7 @@ export class BaseGuildChannel extends BaseChannel<ChannelType> {
|
||||
}
|
||||
|
||||
get url() {
|
||||
return channelLink(this.id, this.guildId);
|
||||
return Formatter.channelLink(this.id, this.guildId);
|
||||
}
|
||||
|
||||
setPosition(position: number, reason?: string) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import type { UsingClient } from '../../commands';
|
||||
import { Base } from './Base';
|
||||
import { snowflakeToTimestamp } from './functions';
|
||||
import { snowflakeToTimestamp } from '../../common/it/utils';
|
||||
|
||||
export class DiscordBase<Data extends Record<string, any> = { id: string }> extends Base {
|
||||
id: string;
|
||||
|
@ -1,67 +0,0 @@
|
||||
import type { Cache } from '../../cache';
|
||||
import { DiscordEpoch, type EmojiResolvable, type TypeArray } from '../../common';
|
||||
import { type APIPartialEmoji, FormattingPatterns } from '../../types';
|
||||
|
||||
/** * Convert a timestamp to a snowflake. * @param timestamp The timestamp to convert. * @returns The snowflake. */
|
||||
export function snowflakeToTimestamp(id: string): bigint {
|
||||
return (BigInt(id) >> 22n) + DiscordEpoch;
|
||||
}
|
||||
|
||||
export function channelLink(channelId: string, guildId?: string) {
|
||||
return `https://discord.com/channels/${guildId ?? '@me'}/${channelId}`;
|
||||
}
|
||||
|
||||
export function messageLink(channelId: string, messageId: string, guildId?: string) {
|
||||
return `${channelLink(channelId, guildId)}/${messageId}`;
|
||||
}
|
||||
|
||||
export function resolvePartialEmoji(emoji: EmojiResolvable): APIPartialEmoji | undefined {
|
||||
if (typeof emoji === 'string') {
|
||||
const groups: Partial<APIPartialEmoji> | undefined = emoji.match(FormattingPatterns.Emoji)?.groups;
|
||||
if (groups) {
|
||||
return { animated: !!groups.animated, name: groups.name!, id: groups.id! };
|
||||
}
|
||||
if (emoji.includes('%')) {
|
||||
emoji = encodeURIComponent(emoji);
|
||||
}
|
||||
if (!(emoji.includes(':') || emoji.match(/\d{17,20}/g))) {
|
||||
return { name: emoji, id: null };
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(emoji.id && emoji.name)) return;
|
||||
return { id: emoji.id, name: emoji.name, animated: !!emoji.animated };
|
||||
}
|
||||
|
||||
export async function resolveEmoji(emoji: EmojiResolvable, cache: Cache): Promise<APIPartialEmoji | undefined> {
|
||||
const partial = resolvePartialEmoji(emoji);
|
||||
if (partial) return partial;
|
||||
|
||||
if (typeof emoji === 'string') {
|
||||
if (!emoji.match(/\d{17,20}/g)) return;
|
||||
const fromCache = await cache.emojis?.get(emoji);
|
||||
return fromCache && { animated: fromCache.animated, id: fromCache.id, name: fromCache.name };
|
||||
}
|
||||
|
||||
const fromCache = await cache.emojis?.get(emoji.id!);
|
||||
if (fromCache) return { animated: fromCache.animated, id: fromCache.id, name: fromCache.name };
|
||||
return;
|
||||
}
|
||||
|
||||
export function encodeEmoji(rawEmoji: APIPartialEmoji) {
|
||||
return rawEmoji.id ? `${rawEmoji.name}:${rawEmoji.id}` : `${rawEmoji.name}`;
|
||||
}
|
||||
|
||||
export function hasProps<T extends Record<any, any>>(target: T, props: TypeArray<keyof T>): boolean {
|
||||
if (Array.isArray(props)) {
|
||||
return props.every(x => hasProps(target, x));
|
||||
}
|
||||
if (!((props as T[number]) in target)) {
|
||||
return false;
|
||||
}
|
||||
if (typeof target[props] === 'string' && !target[props as T[number]].length) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user