mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-15 05:41:51 +00:00
feat: use a standard custom ID format in role buttons
This commit is contained in:
parent
784c54b22a
commit
b64611dd01
4 changed files with 30 additions and 3 deletions
|
@ -3,16 +3,22 @@ import { RoleButtonsPluginType, TRoleButtonOption } from "../types";
|
||||||
import { RoleManagerPlugin } from "../../RoleManager/RoleManagerPlugin";
|
import { RoleManagerPlugin } from "../../RoleManager/RoleManagerPlugin";
|
||||||
import { GuildMember } from "discord.js";
|
import { GuildMember } from "discord.js";
|
||||||
import { getAllRolesInButtons } from "../functions/getAllRolesInButtons";
|
import { getAllRolesInButtons } from "../functions/getAllRolesInButtons";
|
||||||
|
import { parseCustomId } from "../../../utils/parseCustomId";
|
||||||
|
|
||||||
export const onButtonInteraction = typedGuildEventListener<RoleButtonsPluginType>()({
|
export const onButtonInteraction = typedGuildEventListener<RoleButtonsPluginType>()({
|
||||||
event: "interactionCreate",
|
event: "interactionCreate",
|
||||||
async listener({ pluginData, args }) {
|
async listener({ pluginData, args }) {
|
||||||
if (!args.interaction.isButton() || !args.interaction.customId.startsWith("roleButtons:")) {
|
if (!args.interaction.isButton()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { namespace, data } = parseCustomId(args.interaction.customId);
|
||||||
|
if (namespace !== "roleButtons") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const config = pluginData.config.get();
|
const config = pluginData.config.get();
|
||||||
const [, name, optionIndex] = args.interaction.customId.split(":");
|
const { name, index: optionIndex } = data;
|
||||||
// For some reason TS's type inference fails here so using a type annotation
|
// For some reason TS's type inference fails here so using a type annotation
|
||||||
const buttons = config.buttons[name];
|
const buttons = config.buttons[name];
|
||||||
const option: TRoleButtonOption | undefined = buttons?.options[optionIndex];
|
const option: TRoleButtonOption | undefined = buttons?.options[optionIndex];
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { LogsPlugin } from "../../Logs/LogsPlugin";
|
||||||
import { Message, MessageButton, MessageEditOptions, MessageOptions, Snowflake } from "discord.js";
|
import { Message, MessageButton, MessageEditOptions, MessageOptions, Snowflake } from "discord.js";
|
||||||
import { RoleButtonsItem } from "../../../data/entities/RoleButtonsItem";
|
import { RoleButtonsItem } from "../../../data/entities/RoleButtonsItem";
|
||||||
import { splitButtonsIntoRows } from "./splitButtonsIntoRows";
|
import { splitButtonsIntoRows } from "./splitButtonsIntoRows";
|
||||||
|
import { buildCustomId } from "../../../utils/buildCustomId";
|
||||||
|
|
||||||
const channelMessageRegex = new RegExp(`^(${snowflakeRegex.source})-(${snowflakeRegex.source})$`);
|
const channelMessageRegex = new RegExp(`^(${snowflakeRegex.source})-(${snowflakeRegex.source})$`);
|
||||||
|
|
||||||
|
@ -107,7 +108,7 @@ export async function applyRoleButtons(
|
||||||
const button = new MessageButton()
|
const button = new MessageButton()
|
||||||
.setLabel(opt.label ?? "")
|
.setLabel(opt.label ?? "")
|
||||||
.setStyle(opt.style ?? "PRIMARY")
|
.setStyle(opt.style ?? "PRIMARY")
|
||||||
.setCustomId(`roleButtons:${configItem.name}:${index}:${Math.round(Date.now() / 1000)}`);
|
.setCustomId(buildCustomId("roleButtons", { name: configItem.name, index }));
|
||||||
|
|
||||||
if (opt.emoji) {
|
if (opt.emoji) {
|
||||||
const emo = pluginData.client.emojis.resolve(opt.emoji as Snowflake) ?? opt.emoji;
|
const emo = pluginData.client.emojis.resolve(opt.emoji as Snowflake) ?? opt.emoji;
|
||||||
|
|
3
backend/src/utils/buildCustomId.ts
Normal file
3
backend/src/utils/buildCustomId.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export function buildCustomId(namespace: string, data: any = {}) {
|
||||||
|
return `${namespace}:${Date.now()}:${JSON.stringify(data)}`;
|
||||||
|
}
|
17
backend/src/utils/parseCustomId.ts
Normal file
17
backend/src/utils/parseCustomId.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
const customIdFormat = /^([^:]+):\d+:(.*)$/;
|
||||||
|
|
||||||
|
export function parseCustomId(customId: string): { namespace: string; data: any } {
|
||||||
|
const parts = customId.match(customIdFormat);
|
||||||
|
if (!parts) {
|
||||||
|
return {
|
||||||
|
namespace: "",
|
||||||
|
data: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
namespace: parts[1],
|
||||||
|
// Skipping timestamp
|
||||||
|
data: JSON.parse(parts[2]),
|
||||||
|
};
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue