diff --git a/docs/configuration.md b/docs/configuration.md index c766605..84cec7c 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -313,6 +313,14 @@ URL to use for attachment and log links. Defaults to `http://IP:PORT`. **Default:** `off` If enabled, mod replies will use their nicknames (on the inbox server) instead of their usernames +#### notifyOnMainServerLeave +**Default:** `on` +If enabled, a system message will be posted into any open threads if the user leaves a main server + +#### notifyOnMainServerJoin +**Default:** `on` +If enabled, a system message will be posted into any open threads if the user joins a main server + #### anonymizeChannelName **Default:** `off` If enabled, channel names will be the user's name and discriminator salted with the current time, then hashed to protect the user's privacy diff --git a/src/data/cfg.jsdoc.js b/src/data/cfg.jsdoc.js index 1e1658e..200971a 100644 --- a/src/data/cfg.jsdoc.js +++ b/src/data/cfg.jsdoc.js @@ -51,6 +51,8 @@ * @property {boolean} [reactOnSeen=false] * @property {string} [reactOnSeenEmoji="📨"] * @property {boolean} [createThreadOnMention=false] + * @property {boolean} [notifyOnMainServerLeave=true] + * @property {boolean} [notifyOnMainServerJoin=true] * @property {number} [port=8890] * @property {string} [url] * @property {array} [extraIntents=[]] diff --git a/src/data/cfg.schema.json b/src/data/cfg.schema.json index 8aa5ba7..1678530 100644 --- a/src/data/cfg.schema.json +++ b/src/data/cfg.schema.json @@ -296,6 +296,16 @@ "default": false }, + "notifyOnMainServerLeave": { + "$ref": "#/definitions/customBoolean", + "default": true + }, + + "notifyOnMainServerJoin": { + "$ref": "#/definitions/customBoolean", + "default": true + }, + "port": { "type": "number", "maximum": 65535, diff --git a/src/main.js b/src/main.js index b1316a9..70d11e8 100644 --- a/src/main.js +++ b/src/main.js @@ -28,6 +28,7 @@ const version = require("./modules/version"); const newthread = require("./modules/newthread"); const idModule = require("./modules/id"); const alert = require("./modules/alert"); +const joinLeaveNotification = require("./modules/joinLeaveNotification"); const {ACCIDENTAL_THREAD_MESSAGES} = require("./data/constants"); @@ -304,7 +305,8 @@ async function initPlugins() { version, newthread, idModule, - alert + alert, + joinLeaveNotification ]; const plugins = [...builtInPlugins]; diff --git a/src/modules/joinLeaveNotification.js b/src/modules/joinLeaveNotification.js new file mode 100644 index 0000000..970a2f9 --- /dev/null +++ b/src/modules/joinLeaveNotification.js @@ -0,0 +1,31 @@ +const config = require("../cfg"); +const threads = require("../data/threads"); +const utils = require("../utils"); + +module.exports = ({ bot }) => { + // Join Notification: Post a message in the thread if the user joins a main server + if (config.notifyOnMainServerJoin) { + bot.on("guildMemberAdd", async (guild, member) => { + const mainGuilds = utils.getMainGuilds(); + if (! mainGuilds.find(gld => gld.id === guild.id)) return; + + const thread = await threads.findOpenThreadByUserId(member.id); + if (thread != null) { + await thread.postSystemMessage(`***The user joined the guild ${guild.name}.***`); + } + }); + } + + // Leave Notification: Post a message in the thread if the user leaves a main server + if (config.notifyOnMainServerLeave) { + bot.on("guildMemberRemove", async (guild, member) => { + const mainGuilds = utils.getMainGuilds(); + if (! mainGuilds.find(gld => gld.id === guild.id)) return; + + const thread = await threads.findOpenThreadByUserId(member.id); + if (thread != null) { + await thread.postSystemMessage(`***The user left the guild ${guild.name}.***`); + } + }); + } +};