Utils
@lunibee/utils provides a small set of general-purpose helpers.
import { sleep, randomInt, isSnowflake } from "lunibee";// or from the sub-package:import { sleep, randomInt, isSnowflake } from "@lunibee/utils";import { sleep, randomInt, isSnowflake } from "lunibee";// or from the sub-package:import { sleep, randomInt, isSnowflake } from "@lunibee/utils";sleep(ms)
Section titled βsleep(ms)βReturns a Promise that resolves after ms milliseconds.
async function delayedGreet(channel, name: string) { await channel.send({ content: `Hey ${name}, starting up...` }); await sleep(2000); // wait 2 seconds await channel.send({ content: "Ready! π" });}async function delayedGreet(channel, name) { await channel.send({ content: `Hey ${name}, starting up...` }); await sleep(2000); // wait 2 seconds await channel.send({ content: "Ready π" });}Signature:
function sleep(ms: number): Promise<void>function sleep(ms)randomInt(min, max)
Section titled βrandomInt(min, max)βReturns a random integer in the inclusive range [min, max].
const roll = randomInt(1, 6); // dice roll: 1β6const item = items[randomInt(0, items.length - 1)];const roll = randomInt(1, 6); // dice roll: 1β6const item = items[randomInt(0, items.length - 1)];Signature:
function randomInt(min: number, max: number): numberfunction randomInt(min, max)isSnowflake(value)
Section titled βisSnowflake(value)βReturns true if the string is a valid Discord snowflake (1β20 digit numeric string).
isSnowflake("123456789012345678") // β trueisSnowflake("not-a-snowflake") // β falseisSnowflake("99999999999999999999") // β true (20 digits)isSnowflake("123456789012345678") // β trueisSnowflake("not-a-snowflake") // β falseisSnowflake("99999999999999999999") // β true (20 digits)Signature:
function isSnowflake(value: string): booleanfunction isSnowflake(value)Examples
Section titled βExamplesβValidate user input before fetching
client.on("messageCreate", async (message) => { if (!message.content.startsWith("!user ")) return; const id = message.content.slice(6).trim(); if (!isSnowflake(id)) { return message.reply("Invalid user ID."); } const user = await client.users.fetch(id); await message.reply(`Found: ${user.displayName}`);});client.on("messageCreate", async (message) => { if (!message.content.startsWith("!user ")) return; const id = message.content.slice(6).trim(); if (!isSnowflake(id)) { return message.reply("Invalid user ID."); } const user = await client.users.fetch(id); await message.reply(`Found: ${user.displayName}`);});Rate-limit friendly bulk operation with delay
async function sendAll(channels: Channel[], content: string) { for (const channel of channels) { await channel.send({ content }); await sleep(500); // 500ms between each send to avoid rate limits }}async function sendAll(channels , content) { for (const channel of channels) { await channel.send({ content }); await sleep(500); // 500ms between each send to avoid rate limits }}Random pick from a list
const jokes = ["Why do bees hum? Because they don't know the words. π΅", "What do you call a bee that can't make up its mind? A maybee! π"];const joke = jokes[randomInt(0, jokes.length - 1)];await channel.send({ content: joke });const jokes = ["Why do bees hum? Because they don't know the words. π΅", "What do you call a bee that can't make up its mind? A maybee π"];const joke = jokes[randomInt(0, jokes.length - 1)];await channel.send({ content: joke });