3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-17 15:15:02 +00:00

chore: remove old button roles implementation

This commit is contained in:
Dragory 2022-04-23 16:38:24 +03:00
parent 3fe71b3e27
commit 05334a772f
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
12 changed files with 50 additions and 450 deletions

View file

@ -1,7 +1,6 @@
import { Snowflake, TextChannel } from "discord.js";
import { GuildPluginData } from "knub";
import { ReactionRole } from "../../../data/entities/ReactionRole";
import { LogType } from "../../../data/LogType";
import { isDiscordAPIError, sleep } from "../../../utils";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { ReactionRolesPluginType } from "../types";

View file

@ -1,94 +0,0 @@
import { MessageButton, MessageComponentInteraction, Snowflake } from "discord.js";
import { GuildPluginData } from "knub";
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>,
int: MessageComponentInteraction,
group: TButtonPairOpts,
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).logBotAlert({
body: `**A configuration error occurred** 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 ?? "")
.setStyle("PRIMARY")
.setCustomId(customId)
.setDisabled(menuButton.disabled ?? false);
if (menuButton.emoji) {
const emo = pluginData.client.emojis.resolve(menuButton.emoji as Snowflake) ?? menuButton.emoji;
btn.setEmoji(emo);
}
menuButtons.push(btn);
}
if (menuButtons.length === 0) {
await int.reply({
content: `A configuration error was encountered, please contact the Administrators!`,
ephemeral: true,
});
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `**A configuration error occurred** on buttons for message ${int.message.id}, menu **${context.roleOrMenu}** not found in config`,
});
return;
}
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: rows, ephemeral: true });
}
export async function handleModifyRole(
pluginData: GuildPluginData<ReactionRolesPluginType>,
int: MessageComponentInteraction,
group: TButtonPairOpts,
context,
) {
const role = await pluginData.guild.roles.fetch(context.roleOrMenu);
if (!role) {
await int.reply({
content: `A configuration error was encountered, please contact the Administrators!`,
ephemeral: true,
});
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `**A configuration error occurred** on buttons for message ${int.message.id}, role **${context.roleOrMenu}** not found on server`,
});
return;
}
const member = await pluginData.guild.members.fetch(int.user.id);
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).logBotAlert({
body: `**A configuration error occurred** on buttons for message ${int.message.id}, error: ${e}. We might be missing permissions!`,
});
}
}

View file

@ -1,45 +0,0 @@
import { Snowflake } from "discord.js";
import { GuildPluginData } from "knub";
import { ReactionRolesPluginType } from "../types";
import { ButtonMenuActions } from "./buttonMenuActions";
export const BUTTON_CONTEXT_SEPARATOR = ":rb:";
export async function getButtonAction(pluginData: GuildPluginData<ReactionRolesPluginType>, roleOrMenu: string) {
if (await pluginData.guild.roles.fetch(roleOrMenu as Snowflake).catch(() => false)) {
return ButtonMenuActions.MODIFY_ROLE;
} else {
return ButtonMenuActions.OPEN_MENU;
}
}
export async function generateStatelessCustomId(
pluginData: GuildPluginData<ReactionRolesPluginType>,
groupName: string,
roleOrMenu: string,
) {
let id = groupName + BUTTON_CONTEXT_SEPARATOR;
id += `${await getButtonAction(pluginData, roleOrMenu)}${BUTTON_CONTEXT_SEPARATOR}${roleOrMenu}`;
return id;
}
export async function resolveStatefulCustomId(pluginData: GuildPluginData<ReactionRolesPluginType>, id: string) {
const button = await pluginData.state.buttonRoles.getForButtonId(id);
if (button) {
const group = pluginData.config.get().button_groups[button.button_group];
if (!group) return null;
const cfgButton = group.default_buttons[button.button_name];
return {
groupName: button.button_group,
action: await getButtonAction(pluginData, cfgButton.role_or_menu),
roleOrMenu: cfgButton.role_or_menu,
stateless: false,
};
} else {
return null;
}
}

View file

@ -1,4 +0,0 @@
export enum ButtonMenuActions {
OPEN_MENU = "goto",
MODIFY_ROLE = "grant",
}

View file

@ -1,42 +0,0 @@
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++;
}
}
if (curRow.components.length >= 1) rows.push(curRow);
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;
}