3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-07-06 18:47:20 +00:00

feat: Add custom add_message and remove_message support to role buttons

This commit is contained in:
seeyebe 2025-06-08 01:08:38 +03:00
parent 3ef89246ba
commit 04b8914531
2 changed files with 28 additions and 12 deletions

View file

@ -1,10 +1,13 @@
import { GuildMember } from "discord.js"; import { GuildMember } from "discord.js";
import { guildPluginEventListener } from "knub"; import { guildPluginEventListener } from "knub";
import { SECONDS } from "../../../utils.js"; import { SECONDS } from "../../../utils.js";
import { renderRecursively } from "../../../utils.js";
import { parseCustomId } from "../../../utils/parseCustomId.js"; import { parseCustomId } from "../../../utils/parseCustomId.js";
import { RoleManagerPlugin } from "../../RoleManager/RoleManagerPlugin.js"; import { RoleManagerPlugin } from "../../RoleManager/RoleManagerPlugin.js";
import { getAllRolesInButtons } from "../functions/getAllRolesInButtons.js"; import { getAllRolesInButtons } from "../functions/getAllRolesInButtons.js";
import { RoleButtonsPluginType, TRoleButtonOption } from "../types.js"; import { RoleButtonsPluginType, TRoleButtonOption } from "../types.js";
import { renderTemplate, TemplateSafeValueContainer } from "../../../templateFormatter.js";
import { memberToTemplateSafeMember, roleToTemplateSafeRole, userToTemplateSafeUser } from "../../../utils/templateSafeObjects.js";
const ROLE_BUTTON_CD = 5 * SECONDS; const ROLE_BUTTON_CD = 5 * SECONDS;
@ -31,7 +34,6 @@ export const onButtonInteraction = guildPluginEventListener<RoleButtonsPluginTyp
ephemeral: true, ephemeral: true,
content: "Invalid option selected", content: "Invalid option selected",
}) })
// tslint:disable-next-line no-console
.catch((err) => console.trace(err.message)); .catch((err) => console.trace(err.message));
return; return;
} }
@ -53,14 +55,25 @@ export const onButtonInteraction = guildPluginEventListener<RoleButtonsPluginTyp
const rolesToRemove: string[] = []; const rolesToRemove: string[] = [];
const rolesToAdd: string[] = []; const rolesToAdd: string[] = [];
const renderTemplateText = async (str: string) =>
renderTemplate(
str,
new TemplateSafeValueContainer({
user: member ? memberToTemplateSafeMember(member) : userToTemplateSafeUser(args.interaction.user),
role: role ? roleToTemplateSafeRole(role) : new TemplateSafeValueContainer({ name: roleName, id: option.role_id }),
}),
);
if (member.roles.cache.has(option.role_id)) { if (member.roles.cache.has(option.role_id)) {
rolesToRemove.push(option.role_id); rolesToRemove.push(option.role_id);
const messageTemplate = config.buttons[name].remove_message || `The role **${roleName}** will be removed shortly!`;
const formatted = typeof messageTemplate === "string"
? await renderTemplateText(messageTemplate)
: await renderRecursively(messageTemplate, renderTemplateText);
args.interaction args.interaction
.reply({ .reply({ ephemeral: true, ...(typeof formatted === "string" ? { content: formatted } : formatted) })
ephemeral: true,
content: `The role **${roleName}** will be removed shortly!`,
})
// tslint:disable-next-line no-console
.catch((err) => console.trace(err.message)); .catch((err) => console.trace(err.message));
} else { } else {
rolesToAdd.push(option.role_id); rolesToAdd.push(option.role_id);
@ -73,12 +86,13 @@ export const onButtonInteraction = guildPluginEventListener<RoleButtonsPluginTyp
} }
} }
const messageTemplate = config.buttons[name].add_message || `You will receive the **${roleName}** role shortly!`;
const formatted = typeof messageTemplate === "string"
? await renderTemplateText(messageTemplate)
: await renderRecursively(messageTemplate, renderTemplateText);
args.interaction args.interaction
.reply({ .reply({ ephemeral: true, ...(typeof formatted === "string" ? { content: formatted } : formatted) })
ephemeral: true,
content: `You will receive the **${roleName}** role shortly!`,
})
// tslint:disable-next-line no-console
.catch((err) => console.trace(err.message)); .catch((err) => console.trace(err.message));
} }

View file

@ -44,6 +44,8 @@ const zRoleButtonsConfigItem = z
content: zMessageContent, content: zMessageContent,
}), }),
]), ]),
add_message: zMessageContent.optional(),
remove_message: zMessageContent.optional(),
options: z.array(zRoleButtonOption).max(25), options: z.array(zRoleButtonOption).max(25),
exclusive: z.boolean().default(false), exclusive: z.boolean().default(false),
}) })