2023-04-01 12:58:17 +01:00
|
|
|
import * as t from "io-ts";
|
2022-04-23 16:31:41 +03:00
|
|
|
import { GuildRoleButtons } from "../../data/GuildRoleButtons";
|
2023-04-01 12:58:17 +01:00
|
|
|
import { StrictValidationError, validate } from "../../validatorUtils";
|
|
|
|
import { LogsPlugin } from "../Logs/LogsPlugin";
|
2022-04-23 16:31:41 +03:00
|
|
|
import { RoleManagerPlugin } from "../RoleManager/RoleManagerPlugin";
|
2023-04-01 12:58:17 +01:00
|
|
|
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
|
|
|
|
import { resetButtonsCmd } from "./commands/resetButtons";
|
2022-04-23 16:31:41 +03:00
|
|
|
import { onButtonInteraction } from "./events/buttonInteraction";
|
2023-04-01 12:58:17 +01:00
|
|
|
import { applyAllRoleButtons } from "./functions/applyAllRoleButtons";
|
2022-04-23 18:51:28 +03:00
|
|
|
import { createButtonComponents } from "./functions/createButtonComponents";
|
|
|
|
import { TooManyComponentsError } from "./functions/TooManyComponentsError";
|
2023-04-01 12:58:17 +01:00
|
|
|
import { pluginInfo } from "./info";
|
|
|
|
import { ConfigSchema, RoleButtonsPluginType } from "./types";
|
2022-04-23 16:31:41 +03:00
|
|
|
|
|
|
|
export const RoleButtonsPlugin = zeppelinGuildPlugin<RoleButtonsPluginType>()({
|
|
|
|
name: "role_buttons",
|
2022-04-23 17:30:37 +03:00
|
|
|
info: pluginInfo,
|
|
|
|
showInDocs: true,
|
2022-04-23 16:31:41 +03:00
|
|
|
|
2022-04-23 19:43:11 +03:00
|
|
|
defaultOptions: {
|
|
|
|
config: {
|
|
|
|
buttons: {},
|
|
|
|
can_reset: false,
|
|
|
|
},
|
|
|
|
overrides: [
|
|
|
|
{
|
|
|
|
level: ">=100",
|
|
|
|
config: {
|
|
|
|
can_reset: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
|
2023-04-01 12:58:17 +01:00
|
|
|
configParser(input) {
|
2022-04-23 16:31:41 +03:00
|
|
|
// Auto-fill "name" property for buttons based on the object key
|
|
|
|
const seenMessages = new Set();
|
2023-04-01 12:58:17 +01:00
|
|
|
for (const [name, buttonsConfig] of Object.entries<any>((input as any).buttons ?? {})) {
|
2022-04-23 16:31:41 +03:00
|
|
|
if (name.length > 16) {
|
|
|
|
throw new StrictValidationError(["Name for role buttons can be at most 16 characters long"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buttonsConfig) {
|
|
|
|
buttonsConfig.name = name;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2022-04-23 18:51:28 +03:00
|
|
|
|
|
|
|
if (buttonsConfig.options) {
|
|
|
|
try {
|
|
|
|
createButtonComponents(buttonsConfig);
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TooManyComponentsError) {
|
|
|
|
throw new StrictValidationError(["Too many options; can only have max 5 buttons per row on max 5 rows."]);
|
|
|
|
}
|
|
|
|
throw new StrictValidationError(["Error validating options"]);
|
|
|
|
}
|
|
|
|
}
|
2022-04-23 16:31:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 12:58:17 +01:00
|
|
|
const error = validate(ConfigSchema, input);
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return input as t.TypeOf<typeof ConfigSchema>;
|
2022-04-23 16:31:41 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
dependencies: () => [LogsPlugin, RoleManagerPlugin],
|
|
|
|
|
|
|
|
events: [onButtonInteraction],
|
|
|
|
|
2023-04-01 12:58:17 +01:00
|
|
|
messageCommands: [resetButtonsCmd],
|
2022-04-23 19:43:11 +03:00
|
|
|
|
2022-04-23 16:31:41 +03:00
|
|
|
beforeLoad(pluginData) {
|
|
|
|
pluginData.state.roleButtons = GuildRoleButtons.getGuildInstance(pluginData.guild.id);
|
|
|
|
},
|
|
|
|
|
|
|
|
async afterLoad(pluginData) {
|
|
|
|
await applyAllRoleButtons(pluginData);
|
|
|
|
},
|
|
|
|
});
|