import { encode } from "../../deps.ts"; import { ActivityType, ImageFormats, ImageSize, StatusType, } from "../types/types.ts"; import { sendGatewayCommand } from "../ws/shard_manager.ts"; export const sleep = (timeout: number) => { return new Promise((resolve) => setTimeout(resolve, timeout)); }; export interface BotStatusRequest { status: StatusType; game: { name?: string; type: ActivityType; }; } export function editBotsStatus( status: StatusType, name?: string, type = ActivityType.Game, ) { sendGatewayCommand("EDIT_BOTS_STATUS", { status, game: { name, type } }); } export function chooseRandom(array: T[]) { return array[Math.floor(Math.random() * array.length)]; } export async function urlToBase64(url: string) { const buffer = await fetch(url).then((res) => res.arrayBuffer()); const imageStr = encode(buffer); const type = url.substring(url.lastIndexOf(".") + 1); return `data:image/${type};base64,${imageStr}`; } /** Allows easy way to add a prop to a base object when needing to use complicated getters solution. */ export function createNewProp(value: any): Partial { return { configurable: true, enumerable: true, writable: true, value }; } export function delay(ms: number): Promise { return new Promise((res): number => setTimeout((): void => { res(); }, ms) ); } export const formatImageURL = ( url: string, size: ImageSize = 128, format?: ImageFormats, ) => { return `${url}.${format || (url.includes("/a_") ? "gif" : "jpg")}?size=${size}`; };