mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-06-08 00:05:01 +00:00
refactor: don't create 'name' property in config dynamically
This commit is contained in:
parent
0810dd3f0e
commit
395a750e9d
19 changed files with 86 additions and 154 deletions
|
@ -6,26 +6,26 @@ import { applyRoleButtons } from "./applyRoleButtons.js";
|
|||
export async function applyAllRoleButtons(pluginData: GuildPluginData<RoleButtonsPluginType>) {
|
||||
const savedRoleButtons = await pluginData.state.roleButtons.getSavedRoleButtons();
|
||||
const config = pluginData.config.get();
|
||||
for (const buttons of Object.values(config.buttons)) {
|
||||
for (const [configName, configItem] of Object.entries(config.buttons)) {
|
||||
// Use the hash of the config to quickly check if we need to update buttons
|
||||
const hash = createHash("md5").update(JSON.stringify(buttons)).digest("hex");
|
||||
const savedButtonsItem = savedRoleButtons.find((bt) => bt.name === buttons.name);
|
||||
const hash = createHash("md5").update(JSON.stringify(configItem)).digest("hex");
|
||||
const savedButtonsItem = savedRoleButtons.find((bt) => bt.name === configName);
|
||||
if (savedButtonsItem?.hash === hash) {
|
||||
// No changes
|
||||
continue;
|
||||
}
|
||||
|
||||
if (savedButtonsItem) {
|
||||
await pluginData.state.roleButtons.deleteRoleButtonItem(buttons.name);
|
||||
await pluginData.state.roleButtons.deleteRoleButtonItem(configName);
|
||||
}
|
||||
|
||||
const applyResult = await applyRoleButtons(pluginData, buttons, savedButtonsItem ?? null);
|
||||
const applyResult = await applyRoleButtons(pluginData, configItem, configName, savedButtonsItem ?? null);
|
||||
if (!applyResult) {
|
||||
return;
|
||||
}
|
||||
|
||||
await pluginData.state.roleButtons.saveRoleButtonItem(
|
||||
buttons.name,
|
||||
configName,
|
||||
applyResult.channel_id,
|
||||
applyResult.message_id,
|
||||
hash,
|
||||
|
|
|
@ -8,6 +8,7 @@ import { createButtonComponents } from "./createButtonComponents.js";
|
|||
export async function applyRoleButtons(
|
||||
pluginData: GuildPluginData<RoleButtonsPluginType>,
|
||||
configItem: TRoleButtonsConfigItem,
|
||||
configName: string,
|
||||
existingSavedButtons: RoleButtonsItem | null,
|
||||
): Promise<{ channel_id: string; message_id: string } | null> {
|
||||
let message: Message;
|
||||
|
@ -32,7 +33,7 @@ export async function applyRoleButtons(
|
|||
channel.messages.fetch(configItem.message.message_id).catch(() => null));
|
||||
if (!messageCandidate) {
|
||||
pluginData.getPlugin(LogsPlugin).logBotAlert({
|
||||
body: `Message not found for role_buttons/${configItem.name}`,
|
||||
body: `Message not found for role_buttons/${configName}`,
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
@ -45,7 +46,7 @@ export async function applyRoleButtons(
|
|||
: Boolean(configItem.message.content.content?.trim()) || configItem.message.content.embeds?.length;
|
||||
if (!contentIsValid) {
|
||||
pluginData.getPlugin(LogsPlugin).logBotAlert({
|
||||
body: `Invalid message content for role_buttons/${configItem.name}`,
|
||||
body: `Invalid message content for role_buttons/${configName}`,
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
@ -58,7 +59,7 @@ export async function applyRoleButtons(
|
|||
}
|
||||
if (!channel || !channel?.isTextBased()) {
|
||||
pluginData.getPlugin(LogsPlugin).logBotAlert({
|
||||
body: `Text channel not found for role_buttons/${configItem.name}`,
|
||||
body: `Text channel not found for role_buttons/${configName}`,
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
@ -89,7 +90,7 @@ export async function applyRoleButtons(
|
|||
candidateMessage = await channel.send(configItem.message.content as string | MessageCreateOptions);
|
||||
} catch (err) {
|
||||
pluginData.getPlugin(LogsPlugin).logBotAlert({
|
||||
body: `Error while posting message for role_buttons/${configItem.name}: ${String(err)}`,
|
||||
body: `Error while posting message for role_buttons/${configName}: ${String(err)}`,
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
@ -100,16 +101,16 @@ export async function applyRoleButtons(
|
|||
|
||||
if (message.author.id !== pluginData.client.user?.id) {
|
||||
pluginData.getPlugin(LogsPlugin).logBotAlert({
|
||||
body: `Error applying role buttons for role_buttons/${configItem.name}: target message must be posted by Zeppelin`,
|
||||
body: `Error applying role buttons for role_buttons/${configName}: target message must be posted by Zeppelin`,
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
// Apply role buttons
|
||||
const components = createButtonComponents(configItem);
|
||||
const components = createButtonComponents(configItem, configName);
|
||||
await message.edit({ components }).catch((err) => {
|
||||
pluginData.getPlugin(LogsPlugin).logBotAlert({
|
||||
body: `Error applying role buttons for role_buttons/${configItem.name}: ${String(err)}`,
|
||||
body: `Error applying role buttons for role_buttons/${configName}: ${String(err)}`,
|
||||
});
|
||||
return null;
|
||||
});
|
||||
|
|
|
@ -4,7 +4,7 @@ import { TRoleButtonsConfigItem } from "../types.js";
|
|||
import { TooManyComponentsError } from "./TooManyComponentsError.js";
|
||||
import { convertButtonStyleStringToEnum } from "./convertButtonStyleStringToEnum.js";
|
||||
|
||||
export function createButtonComponents(configItem: TRoleButtonsConfigItem): Array<ActionRowBuilder<ButtonBuilder>> {
|
||||
export function createButtonComponents(configItem: TRoleButtonsConfigItem, configName: string): Array<ActionRowBuilder<ButtonBuilder>> {
|
||||
const rows: Array<ActionRowBuilder<ButtonBuilder>> = [];
|
||||
|
||||
let currentRow = new ActionRowBuilder<ButtonBuilder>();
|
||||
|
@ -17,7 +17,7 @@ export function createButtonComponents(configItem: TRoleButtonsConfigItem): Arra
|
|||
const button = new ButtonBuilder()
|
||||
.setLabel(option.label ?? "")
|
||||
.setStyle(convertButtonStyleStringToEnum(option.style) ?? ButtonStyle.Primary)
|
||||
.setCustomId(buildCustomId("roleButtons", { name: configItem.name, index }));
|
||||
.setCustomId(buildCustomId("roleButtons", { name: configName, index }));
|
||||
|
||||
if (option.emoji) {
|
||||
button.setEmoji(option.emoji);
|
||||
|
|
|
@ -34,22 +34,6 @@ export type TRoleButtonOption = z.infer<typeof zRoleButtonOption>;
|
|||
|
||||
const zRoleButtonsConfigItem = z
|
||||
.strictObject({
|
||||
// Typed as "never" because you are not expected to supply this directly.
|
||||
// The transform instead picks it up from the property key and the output type is a string.
|
||||
name: z
|
||||
.never()
|
||||
.optional()
|
||||
.transform((_, ctx) => {
|
||||
const ruleName = String(ctx.path[ctx.path.length - 2]).trim();
|
||||
if (!ruleName) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Role buttons must have names",
|
||||
});
|
||||
return z.NEVER;
|
||||
}
|
||||
return ruleName;
|
||||
}),
|
||||
message: z.union([
|
||||
z.strictObject({
|
||||
channel_id: zSnowflake,
|
||||
|
@ -66,7 +50,7 @@ const zRoleButtonsConfigItem = z
|
|||
.refine(
|
||||
(parsed) => {
|
||||
try {
|
||||
createButtonComponents(parsed);
|
||||
createButtonComponents(parsed, "test"); // We can use any configName here
|
||||
} catch (err) {
|
||||
if (err instanceof TooManyComponentsError) {
|
||||
return false;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue