mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-07-06 10:37:19 +00:00
feat: Add custom add_message and remove_message support to role buttons
This commit is contained in:
parent
3ef89246ba
commit
04b8914531
2 changed files with 28 additions and 12 deletions
|
@ -1,10 +1,13 @@
|
|||
import { GuildMember } from "discord.js";
|
||||
import { guildPluginEventListener } from "knub";
|
||||
import { SECONDS } from "../../../utils.js";
|
||||
import { renderRecursively } from "../../../utils.js";
|
||||
import { parseCustomId } from "../../../utils/parseCustomId.js";
|
||||
import { RoleManagerPlugin } from "../../RoleManager/RoleManagerPlugin.js";
|
||||
import { getAllRolesInButtons } from "../functions/getAllRolesInButtons.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;
|
||||
|
||||
|
@ -31,7 +34,6 @@ export const onButtonInteraction = guildPluginEventListener<RoleButtonsPluginTyp
|
|||
ephemeral: true,
|
||||
content: "Invalid option selected",
|
||||
})
|
||||
// tslint:disable-next-line no-console
|
||||
.catch((err) => console.trace(err.message));
|
||||
return;
|
||||
}
|
||||
|
@ -53,14 +55,25 @@ export const onButtonInteraction = guildPluginEventListener<RoleButtonsPluginTyp
|
|||
const rolesToRemove: 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)) {
|
||||
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
|
||||
.reply({
|
||||
ephemeral: true,
|
||||
content: `The role **${roleName}** will be removed shortly!`,
|
||||
})
|
||||
// tslint:disable-next-line no-console
|
||||
.reply({ ephemeral: true, ...(typeof formatted === "string" ? { content: formatted } : formatted) })
|
||||
.catch((err) => console.trace(err.message));
|
||||
} else {
|
||||
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
|
||||
.reply({
|
||||
ephemeral: true,
|
||||
content: `You will receive the **${roleName}** role shortly!`,
|
||||
})
|
||||
// tslint:disable-next-line no-console
|
||||
.reply({ ephemeral: true, ...(typeof formatted === "string" ? { content: formatted } : formatted) })
|
||||
.catch((err) => console.trace(err.message));
|
||||
}
|
||||
|
||||
|
|
|
@ -44,6 +44,8 @@ const zRoleButtonsConfigItem = z
|
|||
content: zMessageContent,
|
||||
}),
|
||||
]),
|
||||
add_message: zMessageContent.optional(),
|
||||
remove_message: zMessageContent.optional(),
|
||||
options: z.array(zRoleButtonOption).max(25),
|
||||
exclusive: z.boolean().default(false),
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue