import { callPopup } from "../../../script.js"; import { getContext } from "../../extensions.js"; import { registerSlashCommand } from "../../slash-commands.js"; export { MODULE_NAME }; const MODULE_NAME = 'dice'; const UPDATE_INTERVAL = 1000; async function doDiceRoll(customDiceFormula) { let value = typeof customDiceFormula === 'string' ? customDiceFormula.trim() : $(this).data('value'); if (value == 'custom') { value = await callPopup('Enter the dice formula:
(for example, 2d6)', 'input'); } if (!value) { return; } const isValid = droll.validate(value); if (isValid) { const result = droll.roll(value); const context = getContext(); context.sendSystemMessage('generic', `${context.name1} rolls a ${value}. The result is: ${result.total} (${result.rolls})`, { isSmallSys: true }); } else { toastr.warning('Invalid dice formula'); } } function addDiceRollButton() { const buttonHtml = `
Roll Dice
`; const dropdownHtml = `
`; $('#extensionsMenu').prepend(buttonHtml); $(document.body).append(dropdownHtml) $('#dice_dropdown li').on('click', doDiceRoll); const button = $('#roll_dice'); const dropdown = $('#dice_dropdown'); dropdown.hide(); button.hide(); let popper = Popper.createPopper(button.get(0), dropdown.get(0), { placement: 'top', }); $(document).on('click touchend', function (e) { const target = $(e.target); if (target.is(dropdown)) return; if (target.is(button) && !dropdown.is(":visible")) { e.preventDefault(); dropdown.fadeIn(250); popper.update(); } else { dropdown.fadeOut(250); } }); } async function moduleWorker() { $('#roll_dice').toggle(getContext().onlineStatus !== 'no_connection'); } jQuery(function () { addDiceRollButton(); moduleWorker(); setInterval(moduleWorker, UPDATE_INTERVAL); registerSlashCommand('roll', (_, value) => doDiceRoll(value), ['r'], "(dice formula) – roll the dice. For example, /roll 2d6", false, true); });