mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-25 10:25:01 +00:00
feat: add internal role manager plugin; add role buttons plugin
This commit is contained in:
parent
42f2c91a40
commit
49aa4d2c5e
23 changed files with 732 additions and 1 deletions
56
backend/src/plugins/RoleButtons/RoleButtonsPlugin.ts
Normal file
56
backend/src/plugins/RoleButtons/RoleButtonsPlugin.ts
Normal file
|
@ -0,0 +1,56 @@
|
|||
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
|
||||
import { ConfigSchema, RoleButtonsPluginType } from "./types";
|
||||
import { mapToPublicFn } from "../../pluginUtils";
|
||||
import { LogsPlugin } from "../Logs/LogsPlugin";
|
||||
import { applyAllRoleButtons } from "./functions/applyAllRoleButtons";
|
||||
import { GuildRoleButtons } from "../../data/GuildRoleButtons";
|
||||
import { RoleManagerPlugin } from "../RoleManager/RoleManagerPlugin";
|
||||
import { StrictValidationError } from "../../validatorUtils";
|
||||
import { onButtonInteraction } from "./events/buttonInteraction";
|
||||
|
||||
export const RoleButtonsPlugin = zeppelinGuildPlugin<RoleButtonsPluginType>()({
|
||||
name: "role_buttons",
|
||||
configSchema: ConfigSchema,
|
||||
|
||||
configPreprocessor(options) {
|
||||
// Auto-fill "name" property for buttons based on the object key
|
||||
const buttonsArray = Array.isArray(options.config?.buttons) ? options.config.buttons : [];
|
||||
const seenMessages = new Set();
|
||||
for (const [name, buttonsConfig] of Object.entries(options.config?.buttons ?? {})) {
|
||||
if (name.length > 16) {
|
||||
throw new StrictValidationError(["Name for role buttons can be at most 16 characters long"]);
|
||||
}
|
||||
|
||||
if (buttonsConfig) {
|
||||
buttonsConfig.name = name;
|
||||
// 5 action rows * 5 buttons
|
||||
if (buttonsConfig.options?.length > 25) {
|
||||
throw new StrictValidationError(["A single message can have at most 25 role buttons"]);
|
||||
}
|
||||
|
||||
if (buttonsConfig.message) {
|
||||
if ("message_id" in buttonsConfig.message) {
|
||||
if (seenMessages.has(buttonsConfig.message.message_id)) {
|
||||
throw new StrictValidationError(["Can't target the same message with two sets of role buttons"]);
|
||||
}
|
||||
seenMessages.add(buttonsConfig.message.message_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return options;
|
||||
},
|
||||
|
||||
dependencies: () => [LogsPlugin, RoleManagerPlugin],
|
||||
|
||||
events: [onButtonInteraction],
|
||||
|
||||
beforeLoad(pluginData) {
|
||||
pluginData.state.roleButtons = GuildRoleButtons.getGuildInstance(pluginData.guild.id);
|
||||
},
|
||||
|
||||
async afterLoad(pluginData) {
|
||||
await applyAllRoleButtons(pluginData);
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue