Skip to content

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";

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! 🐝" });
}

Signature:

function sleep(ms: number): Promise<void>

Returns a random integer in the inclusive range [min, max].

const roll = randomInt(1, 6); // dice roll: 1–6
const item = items[randomInt(0, items.length - 1)];

Signature:

function randomInt(min: number, max: number): number

Returns true if the string is a valid Discord snowflake (1–20 digit numeric string).

isSnowflake("123456789012345678") // β†’ true
isSnowflake("not-a-snowflake") // β†’ false
isSnowflake("99999999999999999999") // β†’ true (20 digits)

Signature:

function isSnowflake(value: string): boolean

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}`);
});

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
}
}

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 });