mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-07-06 10:37:19 +00:00
Merge 25771f2527
into 3e18e6c124
This commit is contained in:
commit
c36a1e5688
3 changed files with 83 additions and 25 deletions
|
@ -1,6 +1,12 @@
|
|||
import { Snowflake, TextChannel } from "discord.js";
|
||||
import { MessageCreateOptions, PermissionsBitField, Snowflake, TextChannel } from "discord.js";
|
||||
import { TemplateParseError, TemplateSafeValueContainer, renderTemplate } from "../../../templateFormatter.js";
|
||||
import { createChunkedMessage, verboseChannelMention, verboseUserMention } from "../../../utils.js";
|
||||
import {
|
||||
createChunkedMessage,
|
||||
renderRecursively,
|
||||
verboseChannelMention,
|
||||
verboseUserMention
|
||||
} from "../../../utils.js";
|
||||
import { hasDiscordPermissions } from "../../../utils/hasDiscordPermissions.js";
|
||||
import { sendDM } from "../../../utils/sendDM.js";
|
||||
import {
|
||||
guildToTemplateSafeGuild,
|
||||
|
@ -28,17 +34,20 @@ export const SendWelcomeMessageEvt = welcomeMessageEvt({
|
|||
|
||||
pluginData.state.sentWelcomeMessages.add(member.id);
|
||||
|
||||
let formatted;
|
||||
|
||||
try {
|
||||
formatted = await renderTemplate(
|
||||
config.message,
|
||||
new TemplateSafeValueContainer({
|
||||
const templateValues = new TemplateSafeValueContainer({
|
||||
member: memberToTemplateSafeMember(member),
|
||||
user: userToTemplateSafeUser(member.user),
|
||||
guild: guildToTemplateSafeGuild(member.guild),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
const renderMessageText = (str: string) => renderTemplate(str, templateValues);
|
||||
|
||||
let formatted: string | MessageCreateOptions;
|
||||
|
||||
try {
|
||||
formatted = typeof config.message === "string"
|
||||
? await renderMessageText(config.message)
|
||||
: ((await renderRecursively(config.message, renderMessageText)) as MessageCreateOptions);
|
||||
} catch (e) {
|
||||
if (e instanceof TemplateParseError) {
|
||||
pluginData.getPlugin(LogsPlugin).logBotAlert({
|
||||
|
@ -46,7 +55,6 @@ export const SendWelcomeMessageEvt = welcomeMessageEvt({
|
|||
});
|
||||
return;
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
|
||||
|
@ -65,15 +73,47 @@ export const SendWelcomeMessageEvt = welcomeMessageEvt({
|
|||
const channel = meta.args.member.guild.channels.cache.get(config.send_to_channel as Snowflake);
|
||||
if (!channel || !(channel instanceof TextChannel)) return;
|
||||
|
||||
if (
|
||||
!hasDiscordPermissions(
|
||||
channel.permissionsFor(pluginData.client.user!.id),
|
||||
PermissionsBitField.Flags.SendMessages | PermissionsBitField.Flags.ViewChannel,
|
||||
)
|
||||
) {
|
||||
pluginData.getPlugin(LogsPlugin).logBotAlert({
|
||||
body: `Missing permissions to send welcome message in ${verboseChannelMention(channel)}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
typeof formatted === "object" && formatted.embeds && formatted.embeds.length > 0 &&
|
||||
!hasDiscordPermissions(
|
||||
channel.permissionsFor(pluginData.client.user!.id),
|
||||
PermissionsBitField.Flags.EmbedLinks,
|
||||
)
|
||||
) {
|
||||
pluginData.getPlugin(LogsPlugin).logBotAlert({
|
||||
body: `Missing permissions to send welcome message **with embeds** in ${verboseChannelMention(channel)}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (typeof formatted === "string") {
|
||||
await createChunkedMessage(channel, formatted, {
|
||||
parse: ["users"],
|
||||
});
|
||||
} else {
|
||||
await channel.send({
|
||||
...formatted,
|
||||
allowedMentions: {
|
||||
parse: ["users"],
|
||||
},
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
pluginData.getPlugin(LogsPlugin).logBotAlert({
|
||||
body: `Failed send a welcome message for ${verboseUserMention(member.user)} to ${verboseChannelMention(
|
||||
channel,
|
||||
)}`,
|
||||
body: `Failed to send welcome message for ${verboseUserMention(member.user)} to ${verboseChannelMention(channel)}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
import { BasePluginType, guildPluginEventListener } from "knub";
|
||||
import z from "zod/v4";
|
||||
import { GuildLogs } from "../../data/GuildLogs.js";
|
||||
import { zMessageContent } from "../../utils.js";
|
||||
|
||||
export const zWelcomeMessageConfig = z.strictObject({
|
||||
send_dm: z.boolean().default(false),
|
||||
send_to_channel: z.string().nullable().default(null),
|
||||
message: z.string().nullable().default(null),
|
||||
message: zMessageContent.nullable().default(null),
|
||||
});
|
||||
|
||||
export interface WelcomeMessagePluginType extends BasePluginType {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { MessagePayload, User } from "discord.js";
|
||||
import { MessagePayload, User, MessageCreateOptions } from "discord.js";
|
||||
import { logger } from "../logger.js";
|
||||
import { HOURS, createChunkedMessage, isDiscordAPIError } from "../utils.js";
|
||||
import Timeout = NodeJS.Timeout;
|
||||
|
@ -16,7 +16,11 @@ export class DMError extends Error {}
|
|||
|
||||
const error20026 = "The bot cannot currently send DMs";
|
||||
|
||||
export async function sendDM(user: User, content: string | MessagePayload, source: string) {
|
||||
export async function sendDM(
|
||||
user: User,
|
||||
content: string | MessagePayload | MessageCreateOptions,
|
||||
source: string
|
||||
) {
|
||||
if (dmsDisabled) {
|
||||
throw new DMError(error20026);
|
||||
}
|
||||
|
@ -26,9 +30,23 @@ export async function sendDM(user: User, content: string | MessagePayload, sourc
|
|||
try {
|
||||
if (typeof content === "string") {
|
||||
await createChunkedMessage(user, content);
|
||||
} else if (content instanceof MessagePayload) {
|
||||
await user.send(content);
|
||||
} else {
|
||||
if (content.embeds && content.embeds.length > 0) {
|
||||
await user.send({
|
||||
...content,
|
||||
allowedMentions: {
|
||||
parse: ["users"],
|
||||
...content.allowedMentions
|
||||
}
|
||||
});
|
||||
} else if (content.content) {
|
||||
await createChunkedMessage(user, content.content, content.allowedMentions);
|
||||
} else {
|
||||
await user.send(content);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
if (isDiscordAPIError(e) && e.code === 20026) {
|
||||
logger.warn(`Received error code 20026: ${e.message}`);
|
||||
|
@ -36,7 +54,6 @@ export async function sendDM(user: User, content: string | MessagePayload, sourc
|
|||
disableDMs(1 * HOURS);
|
||||
throw new DMError(error20026);
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue