diff --git a/CHANGELOG.md b/CHANGELOG.md index 70b53d4..993089a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +## Version 1.3 +- New command: `rtRefresh` +Allows any owner to instantly refresh all reactions + +The bot now removes and re-applies all reactions it manages automatically every 30 minutes and on restart. +In case you want to immediately refresh, you can use the `rtRefresh` command which advances the next check so it happens right away. + +Please read the [command documentation](https://github.com/MMPlugins/ReactionThreads#commands) to learn how to use this new command! + ## Version 1.2 - New command: `rtList` Allows any owner to list all reactions the bot currently has registered diff --git a/README.md b/README.md index 6928e19..91d8950 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ ## A plugin for [Dragory's ModMail](https://github.com/dragory/modmailbot) that allows users to open threads with reactions -**Currently on Version 1.2** +**Currently on Version 1.3** A full [changelog can be found here](https://github.com/MMPlugins/ReactionThreads/blob/main/CHANGELOG.md). Plugin written and maintained by [DarkView](https://github.com/DarkView) (Dark#1010 on Discord) @@ -13,6 +13,9 @@ Table of Contents: - [Commands](#commands) - [Adding/Registering a reaction](#addingregistering-a-reaction) - [Removing/De-Registering a reaction](#removingde-registering-a-reaction) + - [Adding/Removing/Displaying custom responses](#addingremovingdisplaying-custom-responses) + - [Listing all reactions](#listing-all-reactions) + - [Refreshing all reactions](#refreshing-all-reactions) ## Setup Make sure you are running at least v3.3.0 of Modmail. @@ -73,9 +76,14 @@ If you want to completely remove a response, pass one of the following without a This command and its functionality was made by [YetAnotherConnor](https://github.com/YetAnotherConnor). ### Listing all reactions -Signature: Signature: `!rtList [Any ID]` +Signature: `!rtList [Any ID]` This will return a list of all current reactions that match the ID filter if one is given. -- `Any ID` can be any of the IDs available for reactions: `Channel ID`, `Message ID` and `Category ID`. If this parameter is passed, only reactions that are on that message or in that channel etc. get listed. +- `Any ID` can be any of the IDs available for reactions: `Channel ID`, `Message ID` and `Category ID`. If this parameter is passed, only reactions that are on that message or in that channel etc. get listed. + +### Refreshing all reactions +Signature: `!rtRefresh` +This will refresh all reactions this plugin manages. +This happens every 30 minutes automatically, and this command advances the next refresh to happen immediately. Table of Contents: @@ -89,6 +97,7 @@ Table of Contents: - [Adding/Registering a reaction](#addingregistering-a-reaction) - [Removing/De-Registering a reaction](#removingde-registering-a-reaction) - [Adding/Removing/Displaying custom responses](#addingremovingdisplaying-custom-responses) - - [Listing all reactions](#listing-all-reactions) + - [Listing all reactions](#listing-all-reactions) + - [Refreshing all reactions](#refreshing-all-reactions) Plugin written and maintained by [DarkView](https://github.com/DarkView) (Dark#1010 on Discord) diff --git a/reactionThreads.js b/reactionThreads.js index 6b5d8e8..b3da4d3 100644 --- a/reactionThreads.js +++ b/reactionThreads.js @@ -1,9 +1,10 @@ module.exports = function ({ bot, config, commands, knex, threads }) { const fs = require("fs"); - const pluginVersion = "1.2"; + const pluginVersion = "1.3"; const changelogUrl = "=> https://daark.de/RTCL <="; let reactions = []; const emptyResponse = ["none", "nothing", "empty", "null", "-"]; + let refreshTimeout = null; // Check if ownerId is specified in the config, warn otherwise const ownerId = config["reactionThreads-ownerId"]; @@ -246,24 +247,51 @@ module.exports = function ({ bot, config, commands, knex, threads }) { } }; + //#region reaction refresh loop + const refreshReactions = async (msg = null) => { + clearTimeout(refreshTimeout); + if (msg) { + if (!isOwner(msg)) return; + msg = await msg.channel.createMessage(`Refreshing all reactions...`); + } + + for (const react of reactions) { + if (react.channelId === "version") continue; + const emoji = react.emoji.replace(">", ""); + + await bot.removeMessageReaction(react.channelId, react.messageId, emoji, bot.user.id).catch(() => {}); + await bot.addMessageReaction(react.channelId, react.messageId, emoji); + } + + if (msg) { + await msg.edit(`Done!`); + } + refreshTimeout = setTimeout(() => { + refreshReactions(); + }, 1000 * 60 * 30); //Refresh reactions every 30 minutes + } + + refreshReactions(); + //#endregion + //#region versioncheck // Check the plugin version and notify of any updates that happened - let foundVersion = null; + let reactVersion = null; for (const reaction of reactions) { if (reaction.channelId == "version") { - foundVersion = reaction.messageId; + reactVersion = reaction; break; } } - if (foundVersion != null && foundVersion != pluginVersion) { + if (reactVersion.messageId != null && reactVersion.messageId != pluginVersion) { console.info( `[ReactionThreads] Plugin updated to version ${pluginVersion}, please read the changelog at ${changelogUrl} as there may be important or breaking changes!`, ); - reactions.splice(reactions.indexOf({ channelId: "version", messageId: foundVersion }), 1); + reactions.splice(reactions.indexOf(reactVersion), 1); reactions.push({ channelId: "version", messageId: pluginVersion }); saveReactions(); - } else if (foundVersion == null) { + } else if (reactVersion == null) { reactions.push({ channelId: "version", messageId: pluginVersion }); saveReactions(); } @@ -317,6 +345,8 @@ module.exports = function ({ bot, config, commands, knex, threads }) { commands.addInboxServerCommand("rtList", [{ name: "anyId", type: "string", required: false }], listReactionsCmd); + commands.addInboxServerCommand("rtRefresh", [], refreshReactions); + bot.on("messageReactionAdd", onReactionAdd); //#endregion };