mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-06-07 16:05:01 +00:00
Added Discord attachment link reaction, fixed emoji configuration and moved util functions
This commit is contained in:
parent
a4c4b17a14
commit
592d037148
173 changed files with 1540 additions and 1170 deletions
146
backend/src/plugins/Common/CommonPlugin.ts
Normal file
146
backend/src/plugins/Common/CommonPlugin.ts
Normal file
|
@ -0,0 +1,146 @@
|
|||
import {
|
||||
ChatInputCommandInteraction,
|
||||
Message,
|
||||
MessageCreateOptions,
|
||||
MessageMentionOptions,
|
||||
ModalSubmitInteraction,
|
||||
TextBasedChannel,
|
||||
User,
|
||||
} from "discord.js";
|
||||
import { PluginOptions } from "knub";
|
||||
import { logger } from "../../logger";
|
||||
import { isContextInteraction, makeIoTsConfigParser, sendContextResponse } from "../../pluginUtils";
|
||||
import { errorMessage, successMessage } from "../../utils";
|
||||
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
|
||||
import { getErrorEmoji, getSuccessEmoji } from "./functions/getEmoji";
|
||||
import { CommonPluginType, ConfigSchema } from "./types";
|
||||
|
||||
const defaultOptions: PluginOptions<CommonPluginType> = {
|
||||
config: {
|
||||
success_emoji: "✅",
|
||||
error_emoji: "❌",
|
||||
},
|
||||
};
|
||||
|
||||
export const CommonPlugin = zeppelinGuildPlugin<CommonPluginType>()({
|
||||
name: "common",
|
||||
showInDocs: false,
|
||||
info: {
|
||||
prettyName: "Common",
|
||||
},
|
||||
|
||||
dependencies: () => [],
|
||||
configParser: makeIoTsConfigParser(ConfigSchema),
|
||||
defaultOptions,
|
||||
public: {
|
||||
getSuccessEmoji(pluginData) {
|
||||
return () => getSuccessEmoji(pluginData);
|
||||
},
|
||||
|
||||
getErrorEmoji(pluginData) {
|
||||
return () => getErrorEmoji(pluginData);
|
||||
},
|
||||
|
||||
sendSuccessMessage(pluginData) {
|
||||
return async (
|
||||
context: TextBasedChannel | Message | User | ChatInputCommandInteraction,
|
||||
body: string,
|
||||
allowedMentions?: MessageMentionOptions,
|
||||
responseInteraction?: ModalSubmitInteraction,
|
||||
ephemeral = true,
|
||||
): Promise<Message | undefined> => {
|
||||
const emoji = getSuccessEmoji(pluginData);
|
||||
const formattedBody = successMessage(body, emoji);
|
||||
const content: MessageCreateOptions = allowedMentions
|
||||
? { content: formattedBody, allowedMentions }
|
||||
: { content: formattedBody };
|
||||
|
||||
if (responseInteraction) {
|
||||
await responseInteraction
|
||||
.editReply({ content: formattedBody, embeds: [], components: [] })
|
||||
.catch((err) => logger.error(`Interaction reply failed: ${err}`));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isContextInteraction(context)) {
|
||||
// noinspection TypeScriptValidateJSTypes
|
||||
return sendContextResponse(context, { ...content }) // Force line break
|
||||
.catch((err) => {
|
||||
const channelInfo =
|
||||
"guild" in context && context.guild ? `${context.id} (${context.guild.id})` : context.id;
|
||||
|
||||
logger.warn(`Failed to send success message to ${channelInfo}): ${err.code} ${err.message}`);
|
||||
|
||||
return undefined;
|
||||
});
|
||||
}
|
||||
|
||||
const replyMethod = context.replied ? "followUp" : "reply";
|
||||
|
||||
return context[replyMethod]({
|
||||
content: formattedBody,
|
||||
embeds: [],
|
||||
components: [],
|
||||
fetchReply: true,
|
||||
ephemeral,
|
||||
}).catch((err) => {
|
||||
logger.error(`Context reply failed: ${err}`);
|
||||
|
||||
return undefined;
|
||||
}) as Promise<Message>;
|
||||
};
|
||||
},
|
||||
|
||||
sendErrorMessage(pluginData) {
|
||||
return async (
|
||||
context: TextBasedChannel | Message | User | ChatInputCommandInteraction,
|
||||
body: string,
|
||||
allowedMentions?: MessageMentionOptions,
|
||||
responseInteraction?: ModalSubmitInteraction,
|
||||
ephemeral = false,
|
||||
): Promise<Message | undefined> => {
|
||||
const emoji = getErrorEmoji(pluginData);
|
||||
const formattedBody = errorMessage(body, emoji);
|
||||
const content: MessageCreateOptions = allowedMentions
|
||||
? { content: formattedBody, allowedMentions }
|
||||
: { content: formattedBody };
|
||||
|
||||
if (responseInteraction) {
|
||||
await responseInteraction
|
||||
.editReply({ content: formattedBody, embeds: [], components: [] })
|
||||
.catch((err) => logger.error(`Interaction reply failed: ${err}`));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isContextInteraction(context)) {
|
||||
// noinspection TypeScriptValidateJSTypes
|
||||
return sendContextResponse(context, { ...content }) // Force line break
|
||||
.catch((err) => {
|
||||
const channelInfo =
|
||||
"guild" in context && context.guild ? `${context.id} (${context.guild.id})` : context.id;
|
||||
|
||||
logger.warn(`Failed to send error message to ${channelInfo}): ${err.code} ${err.message}`);
|
||||
|
||||
return undefined;
|
||||
});
|
||||
}
|
||||
|
||||
const replyMethod = context.replied ? "followUp" : "reply";
|
||||
|
||||
return context[replyMethod]({
|
||||
content: formattedBody,
|
||||
embeds: [],
|
||||
components: [],
|
||||
fetchReply: true,
|
||||
ephemeral,
|
||||
}).catch((err) => {
|
||||
logger.error(`Context reply failed: ${err}`);
|
||||
|
||||
return undefined;
|
||||
}) as Promise<Message>;
|
||||
};
|
||||
},
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue