diff --git a/backend/src/plugins/WelcomeMessage/WelcomeMessagePlugin.ts b/backend/src/plugins/WelcomeMessage/WelcomeMessagePlugin.ts new file mode 100644 index 00000000..b34e5659 --- /dev/null +++ b/backend/src/plugins/WelcomeMessage/WelcomeMessagePlugin.ts @@ -0,0 +1,29 @@ +import { zeppelinPlugin } from "../ZeppelinPluginBlueprint"; +import { PluginOptions } from "knub"; +import { WelcomeMessagePluginType, ConfigSchema } from "./types"; +import { GuildLogs } from "src/data/GuildLogs"; +import { GuildMemberAddEvt } from "./events/GuildMemberAddEvt"; + +const defaultOptions: PluginOptions = { + config: { + send_dm: false, + send_to_channel: null, + message: "", + }, +}; + +export const WelcomeMessagePlugin = zeppelinPlugin()("welcome_message", { + configSchema: ConfigSchema, + defaultOptions, + + // prettier-ignore + events: [ + GuildMemberAddEvt, + ], + + onLoad(pluginData) { + const { state, guild } = pluginData; + + state.logs = new GuildLogs(guild.id); + }, +}); diff --git a/backend/src/plugins/WelcomeMessage/events/GuildMemberAddEvt.ts b/backend/src/plugins/WelcomeMessage/events/GuildMemberAddEvt.ts new file mode 100644 index 00000000..8e33080e --- /dev/null +++ b/backend/src/plugins/WelcomeMessage/events/GuildMemberAddEvt.ts @@ -0,0 +1,51 @@ +import { welcomeEvent } from "../types"; +import { renderTemplate } from "src/templateFormatter"; +import { stripObjectToScalars, createChunkedMessage } from "src/utils"; +import { LogType } from "src/data/LogType"; +import { TextChannel } from "eris"; + +export const GuildMemberAddEvt = welcomeEvent({ + event: "guildMemberAdd", + + async listener(meta) { + const pluginData = meta.pluginData; + const member = meta.args.member; + + const config = pluginData.config.get(); + if (!config.message) return; + if (!config.send_dm && !config.send_to_channel) return; + + const formatted = await renderTemplate(config.message, { + member: stripObjectToScalars(member, ["user"]), + }); + + if (config.send_dm) { + const dmChannel = await member.user.getDMChannel(); + if (!dmChannel) return; + + try { + await createChunkedMessage(dmChannel, formatted); + } catch (e) { + pluginData.state.logs.log(LogType.BOT_ALERT, { + body: `Failed send a welcome DM to {userMention(member)}`, + member: stripObjectToScalars(member), + }); + } + } + + if (config.send_to_channel) { + const channel = meta.args.guild.channels.get(config.send_to_channel); + if (!channel || !(channel instanceof TextChannel)) return; + + try { + await createChunkedMessage(channel, formatted); + } catch (e) { + pluginData.state.logs.log(LogType.BOT_ALERT, { + body: `Failed send a welcome message for {userMention(member)} to {channelMention(channel)}`, + member: stripObjectToScalars(member), + channel: stripObjectToScalars(channel), + }); + } + } + }, +}); diff --git a/backend/src/plugins/WelcomeMessage/types.ts b/backend/src/plugins/WelcomeMessage/types.ts new file mode 100644 index 00000000..eef61be9 --- /dev/null +++ b/backend/src/plugins/WelcomeMessage/types.ts @@ -0,0 +1,20 @@ +import * as t from "io-ts"; +import { BasePluginType, eventListener } from "knub"; +import { tNullable } from "src/utils"; +import { GuildLogs } from "src/data/GuildLogs"; + +export const ConfigSchema = t.type({ + send_dm: t.boolean, + send_to_channel: tNullable(t.string), + message: t.string, +}); +export type TConfigSchema = t.TypeOf; + +export interface WelcomeMessagePluginType extends BasePluginType { + config: TConfigSchema; + state: { + logs: GuildLogs; + }; +} + +export const welcomeEvent = eventListener(); diff --git a/backend/src/plugins/availablePlugins.ts b/backend/src/plugins/availablePlugins.ts index 3e33f936..e868a15f 100644 --- a/backend/src/plugins/availablePlugins.ts +++ b/backend/src/plugins/availablePlugins.ts @@ -2,12 +2,14 @@ import { UtilityPlugin } from "./Utility/UtilityPlugin"; import { LocateUserPlugin } from "./LocateUser/LocateUserPlugin"; import { ZeppelinPluginBlueprint } from "./ZeppelinPluginBlueprint"; import { UsernameSaverPlugin } from "./UsernameSaver/UsernameSaverPlugin"; +import { WelcomeMessagePlugin } from "./WelcomeMessage/WelcomeMessagePlugin"; // prettier-ignore export const guildPlugins: Array> = [ LocateUserPlugin, UsernameSaverPlugin, UtilityPlugin, + WelcomeMessagePlugin, ]; export const globalPlugins = [];