mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 20:35:02 +00:00
Make message with default buttons stateful
This commit is contained in:
parent
5efdf5ce95
commit
5c7c3c8cba
10 changed files with 158 additions and 47 deletions
|
@ -1,9 +1,20 @@
|
|||
import { MessageActionRow, MessageButton, MessageComponentInteraction } from "discord.js";
|
||||
import moment from "moment";
|
||||
import { LogType } from "src/data/LogType";
|
||||
import { logger } from "src/logger";
|
||||
import { LogsPlugin } from "src/plugins/Logs/LogsPlugin";
|
||||
import { MINUTES } from "src/utils";
|
||||
import { idToTimestamp } from "src/utils/idToTimestamp";
|
||||
import { reactionRolesEvt } from "../types";
|
||||
import {
|
||||
generateStatelessCustomId,
|
||||
resolveStatefulCustomId,
|
||||
BUTTON_CONTEXT_SEPARATOR,
|
||||
} from "../util/buttonCustomIdFunctions";
|
||||
import { ButtonMenuActions } from "../util/buttonMenuActions";
|
||||
import humanizeDuration from "humanize-duration";
|
||||
|
||||
const BUTTON_INVALIDATION_TIME = 15 * MINUTES;
|
||||
|
||||
export const ButtonInteractionEvt = reactionRolesEvt({
|
||||
event: "interaction",
|
||||
|
@ -14,42 +25,60 @@ export const ButtonInteractionEvt = reactionRolesEvt({
|
|||
: null;
|
||||
if (!int) return;
|
||||
const cfg = meta.pluginData.config.get();
|
||||
const split = int.customID.split("::");
|
||||
const [groupName, action, roleOrMenu] = [split[0], split[1], split[2]];
|
||||
const split = int.customID.split(BUTTON_CONTEXT_SEPARATOR);
|
||||
const context = (await resolveStatefulCustomId(meta.pluginData, int.customID)) ?? {
|
||||
groupName: split[0],
|
||||
action: split[1],
|
||||
roleOrMenu: split[2],
|
||||
stateless: true,
|
||||
};
|
||||
|
||||
const group = cfg.button_groups[groupName];
|
||||
if (context.stateless) {
|
||||
const timeSinceCreation = moment.utc().valueOf() - idToTimestamp(int.message.id)!;
|
||||
if (timeSinceCreation >= BUTTON_INVALIDATION_TIME) {
|
||||
sendEphemeralReply(
|
||||
int,
|
||||
`Sorry, but these buttons are invalid because they are older than ${humanizeDuration(
|
||||
BUTTON_INVALIDATION_TIME,
|
||||
)}.\nIf the menu is still available, open it again to assign yourself roles!`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const group = cfg.button_groups[context.groupName];
|
||||
if (!group) {
|
||||
await sendEphemeralReply(int, `A configuration error was encountered, please contact the Administrators!`);
|
||||
meta.pluginData
|
||||
.getPlugin(LogsPlugin)
|
||||
.log(
|
||||
LogType.BOT_ALERT,
|
||||
`**A configuration error occured** on buttons for message ${int.message.id}, group **${groupName}** not found in config`,
|
||||
`**A configuration error occured** on buttons for message ${int.message.id}, group **${context.groupName}** not found in config`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Verify that detected action is known by us
|
||||
if (!(<any>Object).values(ButtonMenuActions).includes(action)) {
|
||||
if (!(<any>Object).values(ButtonMenuActions).includes(context.action)) {
|
||||
await sendEphemeralReply(int, `A internal error was encountered, please contact the Administrators!`);
|
||||
meta.pluginData
|
||||
.getPlugin(LogsPlugin)
|
||||
.log(
|
||||
LogType.BOT_ALERT,
|
||||
`**A internal error occured** on buttons for message ${int.message.id}, action **${action}** is not known`,
|
||||
`**A internal error occured** on buttons for message ${int.message.id}, action **${context.action}** is not known`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (action === ButtonMenuActions.GRANT_ROLE) {
|
||||
const role = await meta.pluginData.guild.roles.fetch(roleOrMenu);
|
||||
if (context.action === ButtonMenuActions.GRANT_ROLE) {
|
||||
const role = await meta.pluginData.guild.roles.fetch(context.roleOrMenu);
|
||||
if (!role) {
|
||||
await sendEphemeralReply(int, `A configuration error was encountered, please contact the Administrators!`);
|
||||
meta.pluginData
|
||||
.getPlugin(LogsPlugin)
|
||||
.log(
|
||||
LogType.BOT_ALERT,
|
||||
`**A configuration error occured** on buttons for message ${int.message.id}, group **${groupName}** not found in config`,
|
||||
`**A configuration error occured** on buttons for message ${int.message.id}, group **${context.groupName}** not found in config`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
@ -66,11 +95,10 @@ export const ButtonInteractionEvt = reactionRolesEvt({
|
|||
return;
|
||||
}
|
||||
|
||||
if (action === ButtonMenuActions.OPEN_MENU) {
|
||||
if (context.action === ButtonMenuActions.OPEN_MENU) {
|
||||
const menuButtons: MessageButton[] = [];
|
||||
for (const menuButton of Object.values(group.button_menus[roleOrMenu])) {
|
||||
let customId = "";
|
||||
customId = `${groupName}::${ButtonMenuActions.GRANT_ROLE}::${menuButton.role}`;
|
||||
for (const menuButton of Object.values(group.button_menus[context.roleOrMenu])) {
|
||||
const customId = await generateStatelessCustomId(meta.pluginData, context.groupName, menuButton.role_or_menu);
|
||||
|
||||
const btn = new MessageButton()
|
||||
.setLabel(menuButton.label)
|
||||
|
@ -91,7 +119,7 @@ export const ButtonInteractionEvt = reactionRolesEvt({
|
|||
.getPlugin(LogsPlugin)
|
||||
.log(
|
||||
LogType.BOT_ALERT,
|
||||
`**A configuration error occured** on buttons for message ${int.message.id}, menu **${roleOrMenu}** not found in config`,
|
||||
`**A configuration error occured** on buttons for message ${int.message.id}, menu **${context.roleOrMenu}** not found in config`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
@ -102,7 +130,7 @@ export const ButtonInteractionEvt = reactionRolesEvt({
|
|||
}
|
||||
|
||||
logger.warn(
|
||||
`Action ${action} on button ${int.customID} (Guild: ${int.guildID}, Channel: ${int.channelID}) is unknown!`,
|
||||
`Action ${context.action} on button ${int.customID} (Guild: ${int.guildID}, Channel: ${int.channelID}) is unknown!`,
|
||||
);
|
||||
await sendEphemeralReply(int, `A internal error was encountered, please contact the Administrators!`);
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue