mrmBot-Matrix/commands/general/base64.js

49 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-03-15 14:09:09 +00:00
import Command from "../../classes/command.js";
import { clean } from "../../utils/misc.js";
class Base64Command extends Command {
2023-03-19 05:10:48 +00:00
static category = "general"
2023-03-15 14:09:09 +00:00
async run() {
this.success = false;
if (this.type === "classic" && this.args.length === 0) return "You need to provide whether you want to encode or decode the text!";
const command = this.type === "classic" ? this.args[0].toLowerCase() : this.optionsArray[0].name.toLowerCase();
if (command !== "decode" && command !== "encode") return "You need to provide whether you want to encode or decode the text!";
const string = this.options.text ?? this.args.slice(1).join(" ");
if (!string || !string.trim()) return `You need to provide a string to ${command}!`;
this.success = true;
if (command === "decode") {
const b64Decoded = Buffer.from(string, "base64").toString("utf8");
return { html: `<pre><code>${await clean(b64Decoded)}</pre></code>` };
2023-03-15 14:09:09 +00:00
} else if (command === "encode") {
const b64Encoded = Buffer.from(string, "utf8").toString("base64");
return { html: `<pre><code>${b64Encoded}</pre></code>` };
2023-03-15 14:09:09 +00:00
}
}
static flags = [{
name: "decode",
type: 1,
description: "Decodes a Base64 string",
options: [{
name: "text",
type: 3,
description: "The text to decode",
required: true
}]
}, {
name: "encode",
type: 1,
description: "Encodes a Base64 string",
options: [{
name: "text",
type: 3,
description: "The text to encode",
required: true
}]
}];
static description = "Encodes/decodes a Base64 string";
static arguments = ["[encode/decode]", "[text]"];
}
export default Base64Command;