Update djs & knub ()

* update pkgs

Signed-off-by: GitHub <noreply@github.com>

* new knub typings

Signed-off-by: GitHub <noreply@github.com>

* more pkg updates

Signed-off-by: GitHub <noreply@github.com>

* more fixes

Signed-off-by: GitHub <noreply@github.com>

* channel typings

Signed-off-by: GitHub <noreply@github.com>

* more message utils typings fixes

Signed-off-by: GitHub <noreply@github.com>

* migrate permissions

Signed-off-by: GitHub <noreply@github.com>

* fix: InternalPoster webhookables

Signed-off-by: GitHub <noreply@github.com>

* djs typings: Attachment & Util

Signed-off-by: GitHub <noreply@github.com>

* more typings

Signed-off-by: GitHub <noreply@github.com>

* fix: rename permissionNames

Signed-off-by: GitHub <noreply@github.com>

* more fixes

Signed-off-by: GitHub <noreply@github.com>

* half the number of errors

* knub commands => messageCommands

Signed-off-by: GitHub <noreply@github.com>

* configPreprocessor => configParser

Signed-off-by: GitHub <noreply@github.com>

* fix channel.messages

Signed-off-by: GitHub <noreply@github.com>

* revert automod any typing

Signed-off-by: GitHub <noreply@github.com>

* more configParser typings

Signed-off-by: GitHub <noreply@github.com>

* revert

Signed-off-by: GitHub <noreply@github.com>

* remove knub type params

Signed-off-by: GitHub <noreply@github.com>

* fix more MessageEmbed / MessageOptions

Signed-off-by: GitHub <noreply@github.com>

* dumb commit for @almeidx to see why this is stupid

Signed-off-by: GitHub <noreply@github.com>

* temp disable custom_events

Signed-off-by: GitHub <noreply@github.com>

* more minor typings fixes - 23 err left

Signed-off-by: GitHub <noreply@github.com>

* update djs dep

* +debug build method (revert this)

Signed-off-by: GitHub <noreply@github.com>

* Revert "+debug build method (revert this)"

This reverts commit a80af1e729b742d1aad1097df538d224fbd32ce7.

* Redo +debug build (Revert this)

Signed-off-by: GitHub <noreply@github.com>

* uniform before/after Load shorthands

Signed-off-by: GitHub <noreply@github.com>

* remove unused imports & add prettier plugin

Signed-off-by: GitHub <noreply@github.com>

* env fixes for web platform hosting

Signed-off-by: GitHub <noreply@github.com>

* feat: knub v32-next; related fixes

* fix: allow legacy keys in change_perms action

* fix: request Message Content intent

* fix: use Knub's config validation logic in API

* fix(dashboard): fix error when there are no message and/or slash commands in a plugin

* fix(automod): start_thread action thread options

* fix(CustomEvents): message command types

* chore: remove unneeded type annotation

* feat: add forum channel icon; use thread icon for news threads

* chore: make tslint happy

* chore: fix formatting

---------

Signed-off-by: GitHub <noreply@github.com>
Co-authored-by: almeidx <almeidx@pm.me>
Co-authored-by: Dragory <2606411+Dragory@users.noreply.github.com>
This commit is contained in:
Tiago R 2023-04-01 12:58:17 +01:00 committed by GitHub
parent 293115af22
commit 06877e90cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
476 changed files with 2965 additions and 3251 deletions

View file

@ -1,20 +1,19 @@
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
import { ConfigSchema, RoleButtonsPluginType } from "./types";
import { mapToPublicFn } from "../../pluginUtils";
import { LogsPlugin } from "../Logs/LogsPlugin";
import { applyAllRoleButtons } from "./functions/applyAllRoleButtons";
import * as t from "io-ts";
import { GuildRoleButtons } from "../../data/GuildRoleButtons";
import { StrictValidationError, validate } from "../../validatorUtils";
import { LogsPlugin } from "../Logs/LogsPlugin";
import { RoleManagerPlugin } from "../RoleManager/RoleManagerPlugin";
import { StrictValidationError } from "../../validatorUtils";
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
import { resetButtonsCmd } from "./commands/resetButtons";
import { onButtonInteraction } from "./events/buttonInteraction";
import { pluginInfo } from "./info";
import { applyAllRoleButtons } from "./functions/applyAllRoleButtons";
import { createButtonComponents } from "./functions/createButtonComponents";
import { TooManyComponentsError } from "./functions/TooManyComponentsError";
import { resetButtonsCmd } from "./commands/resetButtons";
import { pluginInfo } from "./info";
import { ConfigSchema, RoleButtonsPluginType } from "./types";
export const RoleButtonsPlugin = zeppelinGuildPlugin<RoleButtonsPluginType>()({
name: "role_buttons",
configSchema: ConfigSchema,
info: pluginInfo,
showInDocs: true,
@ -33,11 +32,10 @@ export const RoleButtonsPlugin = zeppelinGuildPlugin<RoleButtonsPluginType>()({
],
},
configPreprocessor(options) {
configParser(input) {
// Auto-fill "name" property for buttons based on the object key
const buttonsArray = Array.isArray(options.config?.buttons) ? options.config.buttons : [];
const seenMessages = new Set();
for (const [name, buttonsConfig] of Object.entries(options.config?.buttons ?? {})) {
for (const [name, buttonsConfig] of Object.entries<any>((input as any).buttons ?? {})) {
if (name.length > 16) {
throw new StrictValidationError(["Name for role buttons can be at most 16 characters long"]);
}
@ -67,14 +65,19 @@ export const RoleButtonsPlugin = zeppelinGuildPlugin<RoleButtonsPluginType>()({
}
}
return options;
const error = validate(ConfigSchema, input);
if (error) {
throw error;
}
return input as t.TypeOf<typeof ConfigSchema>;
},
dependencies: () => [LogsPlugin, RoleManagerPlugin],
events: [onButtonInteraction],
commands: [resetButtonsCmd],
messageCommands: [resetButtonsCmd],
beforeLoad(pluginData) {
pluginData.state.roleButtons = GuildRoleButtons.getGuildInstance(pluginData.guild.id);

View file

@ -1,10 +1,10 @@
import { typedGuildCommand } from "knub";
import { RoleButtonsPluginType } from "../types";
import { guildPluginMessageCommand } from "knub";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils";
import { applyAllRoleButtons } from "../functions/applyAllRoleButtons";
import { RoleButtonsPluginType } from "../types";
export const resetButtonsCmd = typedGuildCommand<RoleButtonsPluginType>()({
export const resetButtonsCmd = guildPluginMessageCommand<RoleButtonsPluginType>()({
trigger: "role_buttons reset",
description:
"In case of issues, you can run this command to have Zeppelin 'forget' about specific role buttons and re-apply them. This will also repost the message, if not targeting an existing message.",

View file

@ -1,11 +1,11 @@
import { typedGuildEventListener } from "knub";
import { RoleButtonsPluginType, TRoleButtonOption } from "../types";
import { RoleManagerPlugin } from "../../RoleManager/RoleManagerPlugin";
import { GuildMember } from "discord.js";
import { getAllRolesInButtons } from "../functions/getAllRolesInButtons";
import { guildPluginEventListener } from "knub";
import { parseCustomId } from "../../../utils/parseCustomId";
import { RoleManagerPlugin } from "../../RoleManager/RoleManagerPlugin";
import { getAllRolesInButtons } from "../functions/getAllRolesInButtons";
import { RoleButtonsPluginType, TRoleButtonOption } from "../types";
export const onButtonInteraction = typedGuildEventListener<RoleButtonsPluginType>()({
export const onButtonInteraction = guildPluginEventListener<RoleButtonsPluginType>()({
event: "interactionCreate",
async listener({ pluginData, args }) {
if (!args.interaction.isButton()) {
@ -28,6 +28,7 @@ export const onButtonInteraction = typedGuildEventListener<RoleButtonsPluginType
ephemeral: true,
content: "Invalid option selected",
})
// tslint:disable-next-line no-console
.catch((err) => console.trace(err.message));
return;
}
@ -46,6 +47,7 @@ export const onButtonInteraction = typedGuildEventListener<RoleButtonsPluginType
ephemeral: true,
content: `The role **${roleName}** will be removed shortly!`,
})
// tslint:disable-next-line no-console
.catch((err) => console.trace(err.message));
} else {
rolesToAdd.push(option.role_id);
@ -63,6 +65,7 @@ export const onButtonInteraction = typedGuildEventListener<RoleButtonsPluginType
ephemeral: true,
content: `You will receive the **${roleName}** role shortly!`,
})
// tslint:disable-next-line no-console
.catch((err) => console.trace(err.message));
}

View file

@ -1,6 +1,6 @@
import { createHash } from "crypto";
import { GuildPluginData } from "knub";
import { RoleButtonsPluginType } from "../types";
import { createHash } from "crypto";
import { applyRoleButtons } from "./applyRoleButtons";
export async function applyAllRoleButtons(pluginData: GuildPluginData<RoleButtonsPluginType>) {

View file

@ -1,14 +1,10 @@
import { Message, MessageCreateOptions, MessageEditOptions } from "discord.js";
import { GuildPluginData } from "knub";
import { RoleButtonsPluginType, TRoleButtonsConfigItem } from "../types";
import { isSnowflake, snowflakeRegex } from "../../../utils";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { Message, MessageButton, MessageEditOptions, MessageOptions, Snowflake } from "discord.js";
import { RoleButtonsItem } from "../../../data/entities/RoleButtonsItem";
import { buildCustomId } from "../../../utils/buildCustomId";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { RoleButtonsPluginType, TRoleButtonsConfigItem } from "../types";
import { createButtonComponents } from "./createButtonComponents";
const channelMessageRegex = new RegExp(`^(${snowflakeRegex.source})-(${snowflakeRegex.source})$`);
export async function applyRoleButtons(
pluginData: GuildPluginData<RoleButtonsPluginType>,
configItem: TRoleButtonsConfigItem,
@ -19,7 +15,7 @@ export async function applyRoleButtons(
// Remove existing role buttons, if any
if (existingSavedButtons?.channel_id) {
const existingChannel = await pluginData.guild.channels.fetch(configItem.message.channel_id).catch(() => null);
const existingMessage = await (existingChannel?.isText() &&
const existingMessage = await (existingChannel?.isTextBased() &&
existingChannel.messages.fetch(existingSavedButtons.message_id).catch(() => null));
if (existingMessage && existingMessage.components.length) {
await existingMessage.edit({
@ -32,7 +28,7 @@ export async function applyRoleButtons(
if ("message_id" in configItem.message) {
// channel id + message id: apply role buttons to existing message
const channel = await pluginData.guild.channels.fetch(configItem.message.channel_id).catch(() => null);
const messageCandidate = await (channel?.isText() &&
const messageCandidate = await (channel?.isTextBased() &&
channel.messages.fetch(configItem.message.message_id).catch(() => null));
if (!messageCandidate) {
pluginData.getPlugin(LogsPlugin).logBotAlert({
@ -55,10 +51,12 @@ export async function applyRoleButtons(
}
const channel = await pluginData.guild.channels.fetch(configItem.message.channel_id).catch(() => null);
if (channel && (!channel.isText || typeof channel.isText !== "function")) {
if (channel && (!channel.isTextBased || typeof channel.isTextBased !== "function")) {
// FIXME: Probably not relevant anymore?
// tslint:disable-next-line no-console
console.log("wtf", pluginData.guild?.id, configItem.message.channel_id);
}
if (!channel || !channel?.isText?.()) {
if (!channel || !channel?.isTextBased()) {
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `Text channel not found for role_buttons/${configItem.name}`,
});
@ -88,7 +86,7 @@ export async function applyRoleButtons(
if (!candidateMessage) {
try {
candidateMessage = await channel.send(configItem.message.content as string | MessageOptions);
candidateMessage = await channel.send(configItem.message.content as string | MessageCreateOptions);
} catch (err) {
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `Error while posting message for role_buttons/${configItem.name}: ${String(err)}`,

View file

@ -0,0 +1,17 @@
import { ButtonStyle } from "discord.js";
import { TRoleButtonOption } from "../types.js";
export function convertButtonStyleStringToEnum(input: TRoleButtonOption["style"]): ButtonStyle | null | undefined {
switch (input) {
case "PRIMARY":
return ButtonStyle.Primary;
case "SECONDARY":
return ButtonStyle.Secondary;
case "SUCCESS":
return ButtonStyle.Success;
case "DANGER":
return ButtonStyle.Danger;
default:
return input;
}
}

View file

@ -1,23 +1,22 @@
import { MessageActionRow, MessageButton, Snowflake } from "discord.js";
import { chunkArray } from "../../../utils";
import { RoleButtonsPluginType, TRoleButtonOption, TRoleButtonsConfigItem } from "../types";
import { ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
import { buildCustomId } from "../../../utils/buildCustomId";
import { GuildPluginData } from "knub";
import { TRoleButtonsConfigItem } from "../types";
import { convertButtonStyleStringToEnum } from "./convertButtonStyleStringToEnum.js";
import { TooManyComponentsError } from "./TooManyComponentsError";
export function createButtonComponents(configItem: TRoleButtonsConfigItem): MessageActionRow[] {
const rows: MessageActionRow[] = [];
export function createButtonComponents(configItem: TRoleButtonsConfigItem): Array<ActionRowBuilder<ButtonBuilder>> {
const rows: Array<ActionRowBuilder<ButtonBuilder>> = [];
let currentRow = new MessageActionRow();
let currentRow = new ActionRowBuilder<ButtonBuilder>();
for (const [index, option] of configItem.options.entries()) {
if (currentRow.components.length === 5 || (currentRow.components.length > 0 && option.start_new_row)) {
rows.push(currentRow);
currentRow = new MessageActionRow();
currentRow = new ActionRowBuilder<ButtonBuilder>();
}
const button = new MessageButton()
const button = new ButtonBuilder()
.setLabel(option.label ?? "")
.setStyle(option.style ?? "PRIMARY")
.setStyle(convertButtonStyleStringToEnum(option.style) ?? ButtonStyle.Primary)
.setCustomId(buildCustomId("roleButtons", { name: configItem.name, index }));
if (option.emoji) {

View file

@ -1,5 +1,6 @@
import { trimPluginDescription } from "../../utils";
import { ZeppelinGuildPluginBlueprint } from "../ZeppelinPluginBlueprint";
import { ConfigSchema } from "./types";
export const pluginInfo: ZeppelinGuildPluginBlueprint["info"] = {
prettyName: "Role buttons",
@ -77,4 +78,5 @@ export const pluginInfo: ZeppelinGuildPluginBlueprint["info"] = {
... # See above for examples for options
~~~
`),
configSchema: ConfigSchema,
};

View file

@ -1,7 +1,8 @@
import { ButtonStyle } from "discord.js";
import * as t from "io-ts";
import { BasePluginType } from "knub";
import { tMessageContent, tNullable } from "../../utils";
import { GuildRoleButtons } from "../../data/GuildRoleButtons";
import { tMessageContent, tNullable } from "../../utils";
const RoleButtonOption = t.type({
role_id: t.string,
@ -10,6 +11,12 @@ const RoleButtonOption = t.type({
// https://discord.js.org/#/docs/discord.js/v13/typedef/MessageButtonStyle
style: tNullable(
t.union([
t.literal(ButtonStyle.Primary),
t.literal(ButtonStyle.Secondary),
t.literal(ButtonStyle.Success),
t.literal(ButtonStyle.Danger),
// The following are deprecated
t.literal("PRIMARY"),
t.literal("SECONDARY"),
t.literal("SUCCESS"),