3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-07-07 11:07:19 +00:00

feat: add embed support to welcome messages

This commit is contained in:
seeyebe 2025-06-11 15:30:06 +03:00
parent 3ef89246ba
commit 25771f2527
3 changed files with 83 additions and 25 deletions

View file

@ -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;
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 = await renderTemplate(
config.message,
new TemplateSafeValueContainer({
member: memberToTemplateSafeMember(member),
user: userToTemplateSafeUser(member.user),
guild: guildToTemplateSafeGuild(member.guild),
}),
);
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,17 +73,49 @@ 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;
try {
await createChunkedMessage(channel, formatted, {
parse: ["users"],
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)}`,
});
}
}
},
});
});