mrmBot-Matrix/commands/tags/tags.js

171 lines
6.9 KiB
JavaScript

import database from "../../utils/database.js";
import paginator from "../../utils/pagination/pagination.js";
import { random } from "../../utils/misc.js";
import Command from "../../classes/command.js";
const blacklist = ["create", "add", "edit", "remove", "delete", "list", "random", "own", "owner"];
class TagsCommand extends Command {
static category = "tags";
// todo: attempt to not make this file the worst thing that human eyes have ever seen
async run() {
this.success = false;
const cmd = this.type === "classic" ? (this.args[0] ?? "").toLowerCase() : this.optionsArray[0].name;
if (!cmd || !cmd.trim()) return "You need to provide the name of the tag you want to view!";
const tagName = this.type === "classic" ? this.args.slice(1)[0] : (this.optionsArray[0].options[0] ?? {}).value;
if (cmd === "create" || cmd === "add") {
if (!tagName || !tagName.trim()) return "You need to provide the name of the tag you want to add!";
if (blacklist.includes(tagName)) return "You can't make a tag with that name!";
const getResult = await database.getTag(this.channel, tagName);
if (getResult) return "This tag already exists!";
const result = await database.setTag(tagName, { content: this.type === "classic" ? this.args.slice(2).join(" ") : this.optionsArray[0].options[1].value, author: this.message.sender }, this.channel);
this.success = true;
if (result) return result;
return { html: `The tag <font color=\"#ff00ff\">${tagName}</font> has been <b>added</b>!` };
} else if (cmd === "delete" || cmd === "remove") {
if (!tagName || !tagName.trim()) return "You need to provide the name of the tag you want to delete!";
const getResult = await database.getTag(this.channel, tagName);
if (!getResult) return "This tag doesn't exist!";
const owners = process.env.OWNER.split(",");
if (getResult.author !== this.author && !owners.includes(this.author)) return "You don't own this tag!";
await database.removeTag(tagName, this.channel);
this.success = true;
return { html: `The tag <font color=\"#ff00ff\">${tagName}</font> has been <b>deleted</b>!` };
} else if (cmd === "edit") {
if (!tagName || !tagName.trim()) return "You need to provide the name of the tag you want to edit!";
const getResult = await database.getTag(this.channel, tagName);
if (!getResult) return "This tag doesn't exist!";
const owners = process.env.OWNER.split(",");
if (getResult.author !== this.author && !owners.includes(this.author)) return "You don't own this tag!";
await database.editTag(tagName, { content: this.type === "classic" ? this.args.slice(2).join(" ") : this.optionsArray[0].options[1].value, author: this.message.sender }, this.channel);
this.success = true;
return { html: `The tag <font color=\"#ff00ff\">${tagName}</font> has been <b>edited</b>!` };
} else if (cmd === "own" || cmd === "owner") {
if (!tagName || !tagName.trim()) return "You need to provide the name of the tag you want to check the owner of!";
const getResult = await database.getTag(this.channel, tagName);
if (!getResult) return "This tag doesn't exist!";
const user = this.client.users.get(getResult.author);
this.success = true;
if (!user) {
try {
const restUser = await this.client.rest.users.get(getResult.author);
return `This tag is owned by **${restUser.username}#${restUser.discriminator}** (\`${getResult.author}\`).`;
} catch {
return `I couldn't find exactly who owns this tag, but I was able to get their ID: \`${getResult.author}\``;
}
} else {
return `This tag is owned by **${user.username}#${user.discriminator}** (\`${getResult.author}\`).`;
}
} else if (cmd === "list") {
const tagList = await database.getTags(this.channel);
const embeds = [];
const groups = Object.keys(tagList).map((item, index) => {
return index % 15 === 0 ? Object.keys(tagList).slice(index, index + 15) : null;
}).filter((item) => {
return item;
});
let output = JSON.stringify(groups.entries());
// for (const [i, value] of groups.entries()) {
// // embeds.push({
// // embeds: [{
// // title: "Tag List",
// // color: 16711680,
// // footer: {
// // text: `Page ${i + 1} of ${groups.length}`
// // },
// // description: value.join("\n"),
// // author: {
// // name: this.author.username,
// // iconURL: this.author.avatarURL()
// // }
// // }]
// // });
// }
if (embeds.length === 0) return "I couldn't find any tags!";
this.success = true;
return output;
// return paginator(this.client, { type: this.type, message: this.message, interaction: this.interaction, channel: this.channel, author: this.author }, embeds);
} else {
let getResult;
if (cmd === "random") {
const tagList = await database.getTags(this.channel);
getResult = tagList[random(Object.keys(tagList))];
} else {
getResult = await database.getTag(this.channel, this.type === "classic" ? cmd : tagName);
}
if (!getResult) return "This tag doesn't exist!";
this.success = true;
if (getResult.content.length > 2000) {
return {
embeds: [{
color: 16711680,
description: getResult.content
}],
};
}
return { html: getResult.content };
}
}
static description = "Manage tags";
static aliases = ["t", "tag", "ta"];
static arguments = {
default: ["[name]"],
add: ["[name]", "[content]"],
delete: ["[name]"],
edit: ["[name]", "[content]"],
owner: ["[name]"]
};
static subArgs = [{
name: "name",
type: 3,
description: "The name of the tag",
required: true
}, {
name: "content",
type: 3,
description: "The content of the tag",
required: true
}];
static flags = [{
name: "add",
type: 1,
description: "Adds a new tag",
options: this.subArgs
}, {
name: "delete",
type: 1,
description: "Deletes a tag",
options: [this.subArgs[0]]
}, {
name: "edit",
type: 1,
description: "Edits an existing tag",
options: this.subArgs
}, {
name: "get",
type: 1,
description: "Gets a tag",
options: [this.subArgs[0]]
}, {
name: "list",
type: 1,
description: "Lists every tag in this server"
}, {
name: "owner",
type: 1,
description: "Gets the owner of a tag",
options: [this.subArgs[0]]
}, {
name: "random",
type: 1,
description: "Gets a random tag"
}];
static directAllowed = false;
static dbRequired = true;
}
export default TagsCommand;