diff --git a/module.json b/module.json index 7dd8eab..04dbb56 100644 --- a/module.json +++ b/module.json @@ -2,7 +2,7 @@ "name": "retroactive-advantage-5e", "title": "Retroactive Advantage DnD5e", "description": "A module which allows rerolling d20 rolls from chat.", - "version": "0.0.1", + "version": "0.0.2", "author": "Andrew Krigline", "authors": [ { @@ -20,6 +20,12 @@ "styles": [ "./styles/retroactive-advantage-5e.css" ], + "dependencies": [ + { + "name": "socketlib" + } + ], + "socket": true, "minimumCoreVersion": "9", "compatibleCoreVersion": "9", "url": "https://github.com/ElfFriend-DnD/foundryvtt-retroactive-advantage-5e", diff --git a/scripts/retroactive-advantage-5e.js b/scripts/retroactive-advantage-5e.js index 0c925a6..d414884 100644 --- a/scripts/retroactive-advantage-5e.js +++ b/scripts/retroactive-advantage-5e.js @@ -1,6 +1,7 @@ class RetroAdvantage5e { static MODULE_NAME = "retroactive-advantage-5e"; static MODULE_TITLE = "Retroactive Advantage DnD5e"; + static SOCKET; static log(...args) { if (game.modules.get('_dev-mode')?.api?.getPackageDebugValue(this.MODULE_NAME)) { @@ -8,6 +9,11 @@ class RetroAdvantage5e { } } + static initSocket = () => { + this.SOCKET = socketlib.registerModule(this.MODULE_NAME); + this.SOCKET.register('handleChatButton', this._handleChatButton); + } + /** * Helper method to grab a new d20 result * @returns @@ -113,12 +119,21 @@ class RetroAdvantage5e { return newD20Roll; } + /** + * Requests that the GM execute the message update to bypass permissions issue + * @param {*} action + * @param {*} messageId + */ + static async _handleRequestChatButton(action, messageId) { + return this.SOCKET.executeAsGM(this._handleChatButton, action, messageId); + } + /** * Handles our button clicks from the chat log * @param {string} action * @param {string} messageId */ - static async _handleButtonClick(action, messageId) { + static _handleChatButton = async (action, messageId) => { try { const { DISADVANTAGE, @@ -151,18 +166,20 @@ class RetroAdvantage5e { const newMessageData = await newD20Roll.toMessage({}, { create: false }); delete newMessageData.timestamp; + delete newMessageData.user; const messageUpdate = foundry.utils.mergeObject( chatMessage.toJSON(), newMessageData, ); - + this.log('New stuff d20 roll', { roll: chatMessage.roll, newD20Roll }, { chatMessage, newMessageData, messageUpdate }); + // currently seems broken for players without socket workaround return chatMessage.update(messageUpdate); } catch (err) { console.error('A problem occurred with Retroactive Advantage 5e:', err); @@ -188,7 +205,7 @@ class RetroAdvantage5e { return; } - RetroAdvantage5e._handleButtonClick(action, messageId); + RetroAdvantage5e._handleRequestChatButton(action, messageId); }); }); @@ -196,7 +213,7 @@ class RetroAdvantage5e { * Adorn any chat message with a d20 roll with our buttons */ Hooks.on('renderChatMessage', async (chatMessage, [html]) => { - if (!chatMessage.isRoll || !(chatMessage.roll instanceof CONFIG.Dice.D20Roll)) { + if (!(chatMessage.isAuthor || chatMessage.isOwner) || !chatMessage.isRoll || !(chatMessage.roll instanceof CONFIG.Dice.D20Roll)) { return; } @@ -229,3 +246,5 @@ Hooks.on("init", RetroAdvantage5e.init); Hooks.once('devModeReady', ({ registerPackageDebugFlag }) => { registerPackageDebugFlag(RetroAdvantage5e.MODULE_NAME); }); + +Hooks.once("socketlib.ready", RetroAdvantage5e.initSocket);