Skip to main content
The fluxer_util crate contains helper utilities that are independent of the client or structs.

Permissions

Permissions is a bitflags struct for working with Fluxer access permissions. Based on the bitflags 2 crate.
use fluxer_util::permissions::{Permissions, parse_permissions, permissions_to_string};

Flags

ConstantBitHexDescription
CREATE_INSTANT_INVITE00x1Create invites.
KICK_MEMBERS10x2Kick members.
BAN_MEMBERS20x4Ban members.
ADMINISTRATOR30x8All permissions, bypasses channel overwrites.
MANAGE_CHANNELS40x10Manage channels.
MANAGE_GUILD50x20Manage guild settings.
ADD_REACTIONS60x40Add reactions.
VIEW_AUDIT_LOG70x80View audit log.
PRIORITY_SPEAKER80x100Priority speaker in voice channels.
STREAM90x200Screen share (Go Live).
VIEW_CHANNEL100x400View channel.
SEND_MESSAGES110x800Send messages.
SEND_TTS_MESSAGES120x1000Send TTS messages.
MANAGE_MESSAGES130x2000Delete and pin others’ messages.
EMBED_LINKS140x4000Embed links.
ATTACH_FILES150x8000Attach files.
READ_MESSAGE_HISTORY160x10000Read message history.
MENTION_EVERYONE170x20000Mention @everyone and @here.
USE_EXTERNAL_EMOJIS180x40000Use external emojis.
CONNECT200x100000Connect to voice channels.
SPEAK210x200000Speak in voice channels.
MUTE_MEMBERS220x400000Mute members.
DEAFEN_MEMBERS230x800000Deafen members.
MOVE_MEMBERS240x1000000Move members between channels.
USE_VAD250x2000000Use voice activity detection.
CHANGE_NICKNAME260x4000000Change own nickname.
MANAGE_NICKNAMES270x8000000Change other members’ nicknames.
MANAGE_ROLES280x10000000Manage roles.
MANAGE_WEBHOOKS290x20000000Manage webhooks.
MANAGE_EXPRESSIONS300x40000000Manage emojis and stickers.
USE_EXTERNAL_STICKERS-0x2000000000Use external stickers.
MODERATE_MEMBERS-0x10000000000Time out members.
CREATE_EXPRESSIONS-0x80000000000Create emojis and stickers.
PIN_MESSAGES-0x8000000000000Pin messages.
BYPASS_SLOWMODE-0x10000000000000Bypass slowmode.
UPDATE_RTC_REGION-0x20000000000000Change voice region.

parse_permissions

pub fn parse_permissions(s: &str) -> Permissions
Parses a bitfield string from the API into Permissions. Returns an empty set on parse error.
let s = "2147483647"; // string from the guild.permissions field
let perms = parse_permissions(s);

if perms.contains(Permissions::ADMINISTRATOR) {
    println!("Bot is an administrator");
}

permissions_to_string

pub fn permissions_to_string(p: Permissions) -> String
Converts Permissions back into a bitfield string for passing to the API.

ALL_PERMISSIONS

pub const ALL_PERMISSIONS: Permissions
A set containing all known flags. Equivalent to Permissions::all().

Flag Operations

Permissions supports standard bitflags operations:
OperationExampleDescription
Check presenceperms.contains(Permissions::BAN_MEMBERS)true if the flag is set.
Unionperms | Permissions::KICK_MEMBERSAdd flags.
Intersectionperms & Permissions::MANAGE_ROLESKeep only common flags.
Inversion!permsFlip all flags.
Differenceperms - Permissions::SEND_MESSAGESRemove a flag.
use fluxer_util::permissions::{parse_permissions, Permissions};

// member.permissions - string from the API
if let Some(perms_str) = &guild.permissions {
    let perms = parse_permissions(perms_str);

    if perms.contains(Permissions::ADMINISTRATOR) {
        println!("Administrator - all permissions available");
    } else {
        let can_ban = perms.contains(Permissions::BAN_MEMBERS);
        let can_kick = perms.contains(Permissions::KICK_MEMBERS);
        println!("Ban: {can_ban}, Kick: {can_kick}");
    }
}
use fluxer_util::permissions::{permissions_to_string, Permissions};

let allow = Permissions::VIEW_CHANNEL | Permissions::SEND_MESSAGES;
let deny  = Permissions::MANAGE_MESSAGES | Permissions::MENTION_EVERYONE;

let body = serde_json::json!({
    "allow": permissions_to_string(allow),
    "deny":  permissions_to_string(deny),
    "type": 0
});

channel.edit_permission(&rest, "ROLE_ID", &body).await?;

SnowflakeUtil

Utilities for working with Snowflake IDs. Fluxer epoch: 1 420 070 400 000 ms (January 1, 2015, 00:00:00 UTC).
use fluxer_util::snowflake::SnowflakeUtil;

date_from_snowflake

pub fn date_from_snowflake(id: &str) -> Option<SystemTime>
Extracts the creation date of an object from its Snowflake ID. Returns None if the ID is not a number.

timestamp_ms_from_snowflake

pub fn timestamp_ms_from_snowflake(id: &str) -> Option<u64>
Returns the Unix timestamp in milliseconds encoded in the Snowflake ID.

snowflake_from_timestamp

pub fn snowflake_from_timestamp(ms: u64) -> String
Generates the smallest possible Snowflake for a given Unix timestamp in milliseconds. Used for time-based pagination in API requests.

is_valid

pub fn is_valid(id: &str) -> bool
Returns true if the string is a valid Snowflake (non-empty string, parseable as u64).
use fluxer_util::snowflake::SnowflakeUtil;
use std::time::SystemTime;

if let Some(created_at) = SnowflakeUtil::date_from_snowflake(&user.id) {
    let age = SystemTime::now()
        .duration_since(created_at)
        .unwrap_or_default();

    let days = age.as_secs() / 86400;
    println!("Account created {} days ago", days);
}
use fluxer_util::snowflake::SnowflakeUtil;
use std::time::{SystemTime, UNIX_EPOCH, Duration};

// Fetch messages from the last 7 days
let seven_days_ago = SystemTime::now()
    .duration_since(UNIX_EPOCH)
    .unwrap()
    .as_millis() as u64
    - (7 * 24 * 3600 * 1000);

let after_id = SnowflakeUtil::snowflake_from_timestamp(seven_days_ago);

let messages = channel
    .fetch_messages(&rest, Some(100), None, Some(&after_id))
    .await?;

CDN

URL-building functions for Fluxer media resources. Exported from fluxer_core::util::cdn.
use fluxer_core::util::cdn::{self, CdnOptions};

Base URLs

ConstantValueUsage
CDN_URLhttps://fluxerusercontent.comAvatars, banners, icons (user-generated content).
STATIC_CDN_URLhttps://fluxerstatic.comDefault avatars and static resources.

Functions

FunctionDescription
cdn_avatar_url(user_id, hash, opts)User avatar URL. None if hash is None.
cdn_display_avatar_url(user_id, hash, opts)Avatar URL with a fallback to the default. Always returns a string.
cdn_banner_url(resource_id, hash, opts)Banner URL (user or guild).
cdn_guild_icon_url(guild_id, hash, opts)Guild icon URL.
cdn_guild_splash_url(guild_id, hash, opts)Guild invite splash URL.
cdn_member_avatar_url(guild_id, user_id, hash, opts)Member server avatar URL.
cdn_member_banner_url(guild_id, user_id, hash, opts)Member server banner URL.
cdn_emoji_url(emoji_id, animated)Custom emoji image URL.
cdn_sticker_url(sticker_id, animated)Sticker image URL.
cdn_default_avatar_url(user_id)Default avatar URL by user ID (index 0–5).

File Extension Logic

  • If the hash starts with a_ - the extension is always gif (animated resource).
  • Otherwise CdnOptions.extension is used, or "png" by default.
use fluxer_core::util::cdn::{cdn_avatar_url, cdn_default_avatar_url, CdnOptions};

let opts = CdnOptions {
    size: Some(128),
    extension: Some("webp".to_string()),
};

let url = cdn_avatar_url(&user.id, user.avatar.as_deref(), &opts)
    .unwrap_or_else(|| cdn_default_avatar_url(&user.id));

println!("Avatar: {url}");
use fluxer_core::util::cdn::cdn_emoji_url;

// animated = true if the emoji is animated
let url = cdn_emoji_url("EMOJI_ID", false);
println!("Emoji: {url}");
// → https://fluxerusercontent.com/emojis/EMOJI_ID.png

Formatters

Text utilities from fluxer_util::formatters.
use fluxer_util::formatters::{truncate, escape_markdown, format_color, format_timestamp};

truncate

pub fn truncate(s: &str, max_len: usize) -> String
Truncates a string to max_len characters. If the string is longer - the last character is replaced with (U+2026).
let short = truncate("A very long string", 10);
// → "A very lo…"
Use truncate before passing user-provided data into MessagePayload::content or EmbedBuilder fields to avoid panics when exceeding limits.

escape_markdown

pub fn escape_markdown(s: &str) -> String
Escapes Markdown characters: *, _, ~, `, |, >, #.
let safe = escape_markdown("**bold** and _italic_");
// → "\\*\\*bold\\*\\* and \\_italic\\_"

format_color

pub fn format_color(color: u32) -> String
Formats an RGB integer into a hex string of the form #RRGGBB. Clamps values above 0xFFFFFF.
let hex = format_color(0x5865F2);
// → "#5865F2"

format_timestamp

pub fn format_timestamp(unix_secs: u64, style: Option<char>) -> String
Formats a Unix timestamp (in seconds) into a Fluxer time tag <t:timestamp:style>.
StyleCharacterDisplay Example
Short timet12:00
Long timeT12:00:00
Short dated01/15/2024
Long dateDJanuary 15, 2024
Date and timefJanuary 15, 2024, 12:00 (default)
Full date and timeFMonday, January 15, 2024, 12:00
Relative timeR3 minutes ago
use std::time::{SystemTime, UNIX_EPOCH};

let now = SystemTime::now()
    .duration_since(UNIX_EPOCH)
    .unwrap()
    .as_secs();

let tag = format_timestamp(now, Some('R'));
// → "<t:1705312800:R>"

let default = format_timestamp(now, None);
// → "<t:1705312800>"
use fluxer_util::formatters::format_timestamp;
use fluxer_builders::EmbedBuilder;

let event_ts: u64 = 1705312800;

let embed = EmbedBuilder::new()
    .title("Event")
    .description(format!(
        "Start: {}\nIn: {}",
        format_timestamp(event_ts, Some('f')),
        format_timestamp(event_ts, Some('R')),
    ))
    .color(0x5865F2)
    .build();

Tenor

Utilities for working with Tenor GIF links from fluxer_util::tenor.
use fluxer_util::tenor::{is_tenor_url, extract_tenor_id, tenor_media_url, resolve_tenor_to_image_url};

is_tenor_url

pub fn is_tenor_url(url: &str) -> bool
Returns true if the URL is a Tenor link. Recognizes the following formats:
  • https://tenor.com/view/...
  • https://tenor.com/embed/...
  • https://media.tenor.com/...

extract_tenor_id

pub fn extract_tenor_id(url: &str) -> Option<String>
Extracts the numeric GIF ID from a Tenor link.
URL FormatExampleResult
tenor.com/view/name-12345.../funny-cat-12345"12345"
tenor.com/embed/12345.../embed/12345"12345"
Returns None if the ID is not found or is not a number.

tenor_media_url

pub fn tenor_media_url(gif_id: &str) -> String
Builds a direct link to the GIF file by ID:
https://media.tenor.com/images/{gif_id}/tenor.gif

resolve_tenor_to_image_url

pub fn resolve_tenor_to_image_url(url: &str) -> Option<String>
Combines extract_tenor_id and tenor_media_url. Accepts a Tenor share/embed URL and returns a direct link to the GIF. Returns None if the URL is not recognized.

Resolvers

Helper functions from fluxer_util::resolvers for working with colors.
use fluxer_util::{resolve_color, resolve_color_rgb};

resolve_color

pub fn resolve_color(hex: &str) -> Option<u32>
Parses a hex color string into an RGB integer. Accepts "#RRGGBB" and "RRGGBB" formats.
let color = resolve_color("#5865F2"); // → Some(0x5865F2)
let color = resolve_color("5865F2");  // → Some(0x5865F2)
let color = resolve_color("xyz");     // → None

resolve_color_rgb

pub fn resolve_color_rgb(r: u8, g: u8, b: u8) -> u32
Converts R, G, B components into an RGB integer.
let color = resolve_color_rgb(88, 101, 242); // → 0x5865F2

Emoji

Utilities from fluxer_util::emoji for emoji formatting.
use fluxer_util::emoji::{format_emoji, parse_emoji_str};

format_emoji

pub fn format_emoji(name: &str, id: Option<&str>, animated: bool) -> String
Formats an emoji into a string for passing to API routes.
ParametersResultUsage
name = "👍", id = None"👍"Unicode emoji.
name = "fluxer", id = Some("123"), animated = false"fluxer:123"Custom static.
name = "fluxer", id = Some("123"), animated = true"a_fluxer:123"Custom animated.

parse_emoji_str

pub fn parse_emoji_str(s: &str) -> (String, Option<String>, bool)
Parses an emoji string into its components (name, id, animated).
Input StringResult
"👍"("👍", None, false)
"fluxer:123"("fluxer", Some("123"), false)
"a_fluxer:123"("fluxer", Some("123"), true)