bc.math, bc.vars, bc.lock, bc.forum, bc.activity
These helpers give a JavaScript bot the same tools as the BDFD functions of the same name, with the same results.
| JavaScript | BDFD |
|---|---|
bc.math.floorDiv(a, b) and bc.math.mod(a, b) |
$floorDiv, $mod (rounded down, the remainder is never negative: mod(-3, 8) is 5) |
bc.math.hash(text, 'fnv1a' \| 'crc32' \| 'sha256', modulo?) |
$hash |
bc.math.seededRandom(seed, min, max) |
$seededRandom (same seed, same number) |
await bc.vars.increment(scope, key, n?, id?, { returnValue: false }?) and decrement (the new value is returned unless returnValue is false) |
$increment, $decrement (atomic: two clicks at the same time never lose a write) |
await bc.vars.compareAndSet(scope, key, expected, next, id?) |
$compareAndSetVar |
await bc.vars.listAddUnique(scope, key, item, id?) |
adds to a ,a,b, list without duplicates, atomically |
await bc.vars.list(prefix, limit?) |
$listVars |
bc.lock.tryLock(name, seconds?) and bc.lock.unlock(name) |
$tryLock, $unlock |
await bc.oneTime.run(type, fn) |
$onlyOneTime: runs fn once per 'user' (all servers), 'server' or 'global', for the running command. Returns { ran: true, value }, or { ran: false } when it was already taken (you send the message). The claim is taken before fn; if fn throws, it is given back. |
await bc.oneTime.reset(type?, {userId?, command?}) |
$resetOneTime: re-arms. Without type, the types claimed earlier in the run (user otherwise). command is the name of the command to target. |
await bc.forum.createPost(forumId, title, message, {tags, archiveMinutes, files}) |
$createForumPost |
await bc.forum.tags(forumId) and await bc.forum.setTags(threadId, tags) |
$forumTags, $setThreadTags |
bc.activity.messageCount(channelId, minutes?, humansOnly?) |
$channelMessageCount |
await bc.entitlements.has(skuId, { userId?, guildId? }) |
$hasEntitlement: an active Discord entitlement (subscription or purchase) exists for the user and/or the server. Without a target, the author of the call. |
await bc.entitlements.count(target?), await bc.entitlements.skuIds(target?) |
$entitlementCount, $entitlementSKUs |
await bc.entitlements.endsAt(skuId, target?) |
$entitlementEndsAt: Unix seconds, 'never' with no end date, null with no active right |
await bc.entitlements.startsAt(skuId, target?), await bc.entitlements.type(skuId, target?) |
$entitlementStartsAt (Unix seconds, null without start date or right), $entitlementType (Discord type number, null without right) |
await bc.entitlements.list({ userId?, guildId?, skuId?, includeEnded? }) |
the rights (id, skuId, userId, guildId, type, startsAt, endsAt, deleted, consumed, active) |
await bc.entitlements.skus(), await bc.entitlements.skuName(skuId) |
$skuIDs, $skuName |
await bc.entitlements.hasAny(skuIds, target?), await bc.entitlements.hasAll(skuIds, target?) |
$hasAnyEntitlement, $hasAllEntitlements: at least one / all of these SKUs are active |
await bc.entitlements.consume(skuOrEntitlementId, target?) |
$consumeEntitlement: marks the first active, unconsumed right as consumed (one-time purchases); false otherwise |
bc.presence.set({ status, type?, text?, url? }) |
$setBotStatus: changes the bot status while it runs (online, idle, dnd, invisible; type playing, streaming, listening, watching, competing or custom). Replaces the configured rotation until the bot restarts or is synced again; one change every 15 seconds at most |
await bc.discord.searchMessages(guildId, text, { limit?, channelId?, authorId? }) |
$searchMessages: { total, messages: [{ id, channelId, authorId, content, url }] } (25 results at most) |
await bc.discord.setVoiceStatus(channelId, status?) |
$setVoiceStatus: the status shown under a voice channel name; empty clears it (500 characters at most) |
Scopes are user, guild, channel, message, guildMember and global. Atomic operations are atomic inside one runner process.
const block = bc.math.floorDiv(x, 8);
const cell = bc.math.mod(x, 8);
if (bc.lock.tryLock(`chunk:${block}`)) {
try {
const id = await bc.forum.createPost(forumId, `Ash Town (${x}, ${y})`, 'Founded!', { tags: ['City'] });
} finally {
bc.lock.unlock(`chunk:${block}`);
}
}
Discord monetization: purchases happen at Discord (create the SKU in the developer portal). The entitlementCreate, entitlementUpdate and entitlementDelete events run your script with the right in webhook.payload (same fields as list) and the event name in webhook.path; in BDFD the same values are the entitlement.* variables.
To edit a message with a new image, discord.js already does it: message.edit({ files: [attachment] }).
bc.grid.tiles({ x, y, columns, rows, size, gap, colors }) returns the rectangles of a tile grid ($canvasTileGrid) to draw with the canvas library, or with bc.canvas.drawTiles.