mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-17 15:15:02 +00:00
Button role improvements, proper validation
This commit is contained in:
parent
7bf5e1f3c6
commit
653d6c1dc2
5 changed files with 166 additions and 19 deletions
|
@ -4,6 +4,7 @@ import { LogType } from "../../../data/LogType";
|
|||
import { LogsPlugin } from "../../../plugins/Logs/LogsPlugin";
|
||||
import { ReactionRolesPluginType, TButtonPairOpts } from "../types";
|
||||
import { generateStatelessCustomId } from "./buttonCustomIdFunctions";
|
||||
import { splitButtonsIntoRows } from "./splitButtonsIntoRows";
|
||||
|
||||
export async function handleOpenMenu(
|
||||
pluginData: GuildPluginData<ReactionRolesPluginType>,
|
||||
|
@ -12,14 +13,29 @@ export async function handleOpenMenu(
|
|||
context,
|
||||
) {
|
||||
const menuButtons: MessageButton[] = [];
|
||||
if (group.button_menus == null) {
|
||||
await int.reply({
|
||||
content: `A configuration error was encountered, please contact the Administrators!`,
|
||||
ephemeral: true,
|
||||
});
|
||||
pluginData
|
||||
.getPlugin(LogsPlugin)
|
||||
.log(
|
||||
LogType.BOT_ALERT,
|
||||
`**A configuration error occured** on buttons for message ${int.message.id}, no menus found in config`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
for (const menuButton of Object.values(group.button_menus[context.roleOrMenu])) {
|
||||
const customId = await generateStatelessCustomId(pluginData, context.groupName, menuButton.role_or_menu);
|
||||
|
||||
const btn = new MessageButton()
|
||||
.setLabel(menuButton.label)
|
||||
.setLabel(menuButton.label ?? "")
|
||||
.setStyle("PRIMARY")
|
||||
.setType("BUTTON")
|
||||
.setCustomID(customId);
|
||||
.setCustomID(customId)
|
||||
.setDisabled(menuButton.disabled ?? false);
|
||||
|
||||
if (menuButton.emoji) {
|
||||
const emo = pluginData.client.emojis.resolve(menuButton.emoji) ?? menuButton.emoji;
|
||||
|
@ -41,9 +57,9 @@ export async function handleOpenMenu(
|
|||
);
|
||||
return;
|
||||
}
|
||||
const row = new MessageActionRow().addComponents(menuButtons);
|
||||
const rows = splitButtonsIntoRows(menuButtons, Object.values(group.button_menus[context.roleOrMenu])); // new MessageActionRow().addComponents(menuButtons);
|
||||
|
||||
int.reply({ content: `Click to add/remove a role`, components: [row], ephemeral: true });
|
||||
int.reply({ content: `Click to add/remove a role`, components: rows, ephemeral: true });
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -69,12 +85,26 @@ export async function handleModifyRole(
|
|||
}
|
||||
|
||||
const member = await pluginData.guild.members.fetch(int.user.id);
|
||||
if (member.roles.cache.has(role.id)) {
|
||||
await member.roles.remove(role, `Button Roles on message ${int.message.id}`);
|
||||
await int.reply({ content: `Role **${role.name}** removed`, ephemeral: true });
|
||||
} else {
|
||||
await member.roles.add(role, `Button Roles on message ${int.message.id}`);
|
||||
await int.reply({ content: `Role **${role.name}** added`, ephemeral: true });
|
||||
try {
|
||||
if (member.roles.cache.has(role.id)) {
|
||||
await member.roles.remove(role, `Button Roles on message ${int.message.id}`);
|
||||
await int.reply({ content: `Role **${role.name}** removed`, ephemeral: true });
|
||||
} else {
|
||||
await member.roles.add(role, `Button Roles on message ${int.message.id}`);
|
||||
await int.reply({ content: `Role **${role.name}** added`, ephemeral: true });
|
||||
}
|
||||
} catch (e) {
|
||||
await int.reply({
|
||||
content: "A configuration error was encountered, please contact the Administrators!",
|
||||
ephemeral: true,
|
||||
});
|
||||
pluginData
|
||||
.getPlugin(LogsPlugin)
|
||||
.log(
|
||||
LogType.BOT_ALERT,
|
||||
`**A configuration error occured** on buttons for message ${int.message.id}, error: ${e}. We might be missing permissions!`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
import { MessageActionRow, MessageButton } from "discord.js";
|
||||
import { TButtonOpts } from "../types";
|
||||
|
||||
export function splitButtonsIntoRows(actualButtons: MessageButton[], configButtons: TButtonOpts[]): MessageActionRow[] {
|
||||
const rows: MessageActionRow[] = [];
|
||||
let curRow = new MessageActionRow();
|
||||
let consecutive = 0;
|
||||
|
||||
for (let i = 0; i < actualButtons.length; i++) {
|
||||
const aBtn = actualButtons[i];
|
||||
const cBtn = configButtons[i];
|
||||
|
||||
curRow.addComponents(aBtn);
|
||||
if (((consecutive + 1) % 5 === 0 || cBtn.end_row) && i + 1 < actualButtons.length) {
|
||||
rows.push(curRow);
|
||||
curRow = new MessageActionRow();
|
||||
consecutive = 0;
|
||||
} else {
|
||||
consecutive++;
|
||||
}
|
||||
}
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
||||
export function getRowCount(configButtons: TButtonOpts[]): number {
|
||||
let count = 1;
|
||||
let consecutive = 0;
|
||||
for (let i = 0; i < configButtons.length; i++) {
|
||||
const cBtn = configButtons[i];
|
||||
|
||||
if (((consecutive + 1) % 5 === 0 || cBtn.end_row) && i + 1 < configButtons.length) {
|
||||
count++;
|
||||
consecutive = 0;
|
||||
} else {
|
||||
consecutive++;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue