Storage Rules

Storage Rules #

Storage rules allow you to define redirection rules when the bot uploads files to storage, so that saved files are automatically organized.

See: #28

Send /rules to view the current mode and rules, enable or disable rule matching, and delete rules. Add rules and import presets with the commands below.

Currently supported rule types:

  1. FILENAME-REGEX
  2. MESSAGE-REGEX
  3. IS-ALBUM

Basic syntax for adding rules:

“RuleType RuleContent StorageID|StorageName Path”

A numeric storage argument is treated as a storage ID (shown on the Storages page of the web console); anything else is treated as a storage name. Use an ID when the name contains spaces, or quote the name. Below is an example of a valid rule command:

/rules add FILENAME-REGEX (?i)\.(mp4|mkv|ts|avi|flv)$ 3 /videos
/rules add FILENAME-REGEX (?i)\.(mp4|mkv|ts|avi|flv)$ "My Alist" /videos

In addition, if CHOSEN is used as the storage in the rule, it means files will be stored under the path of the storage you selected by clicking the inline button.

You can also toggle whether rules are applied with /rules switch. When rule mode is off, all files go to the default storage.

Preset Rules #

Manually writing regex rules for common file types is tedious, so the bot ships a built-in set of preset categories (video, image, audio, document, archive) that you can import in one command:

/rules preset <storage_id|storage_name> [base_path]

Parameters:

  • storage: Target storage ID or name (must exist and be accessible to you)
  • base_path: Optional. Each preset category’s subdirectory is created under this path. If omitted, the default category directory names are used directly.

Examples:

# Import preset rules into "MyAlist" with the default directory layout
/rules preset MyAlist

# Import preset rules with a custom base path "downloads/sorted"
/rules preset MyAlist downloads/sorted

This will create FILENAME-REGEX rules for each category, routing matched files to the corresponding subdirectory under base_path:

CategoryMatched extensionsDefault directory
videomp4, mkv, ts, avi, flv, mov, webm, wmv, rmvb, m2ts视频
imagejpg, jpeg, png, gif, webp, bmp图片
audiomp3, flac, wav, aac, m4a, ogg音频
documentpdf, doc, docx, xls, xlsx, ppt, pptx, txt, md, csv, epub, mobi, azw3, chm文档
archivezip, rar, 7z, tar, gz, bz2, xz, …压缩包
Preset rules are regular `FILENAME-REGEX` rules once imported. You can view or delete them individually from `/rules` like any other rule.

Rule types:

FILENAME-REGEX #

Matches based on filename regex. The rule content must be a valid regular expression, such as:

FILENAME-REGEX (?i)\.(mp4|mkv|ts|avi|flv)$ MyAlist /videos

This means files with extensions mp4, mkv, ts, avi, flv will be saved to the /videos directory in the storage named MyAlist (also affected by the base_path in storage settings).

MESSAGE-REGEX #

Similar to the above, but matches based on the text content of the message itself.

IS-ALBUM #

Matches album messages (media groups). Rule content can only be true or false.

If the path in the rule uses NEW-FOR-ALBUM, the bot will create a new folder for each media group and store all files of that group there. See: https://github.com/krau/SaveAny-Bot/issues/87

For example:

IS-ALBUM true MyWebdav NEW-FOR-ALBUM

This will save media-group messages to the storage named MyWebdav, creating a new folder (generated from the first file) for each album.