@grammyjs/commands
Version:
grammY Commands Plugin
53 lines (52 loc) • 1.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isAdmin = isAdmin;
exports.isMiddleware = isMiddleware;
exports.isCommandOptions = isCommandOptions;
exports.matchesPattern = matchesPattern;
const deps_node_js_1 = require("../deps.node.js");
function isAdmin(ctx) {
return ctx
.getAuthor()
.then((author) => ["administrator", "creator"].includes(author.status));
}
function isMiddleware(obj) {
if (!obj)
return false;
if (obj instanceof deps_node_js_1.Composer)
return true;
if (Array.isArray(obj))
return obj.every(isMiddleware);
const objType = typeof obj;
switch (objType) {
case "function":
return true;
case "object":
return Object.keys(obj).includes("middleware");
}
return false;
}
function isCommandOptions(obj) {
if (typeof obj !== "object" || !obj)
return false;
const { prefix, matchOnlyAtStart, targetedCommands, ignoreCase } = obj;
if (typeof prefix === "string")
return true;
if (typeof matchOnlyAtStart === "boolean")
return true;
if (targetedCommands &&
["ignored", "optional", "required"].includes(targetedCommands))
return true;
if (typeof ignoreCase === "boolean")
return true;
return false;
}
function matchesPattern(value, pattern, ignoreCase = false) {
const transformedValue = ignoreCase ? value.toLowerCase() : value;
const transformedPattern = pattern instanceof RegExp && ignoreCase && !pattern.flags.includes("i")
? new RegExp(pattern, pattern.flags + "i")
: pattern;
return typeof transformedPattern === "string"
? transformedValue === transformedPattern
: transformedPattern.test(transformedValue);
}