OK I am back. here is what I have so far: on('ready', function() {
"use strict";
// Ensure the state is initialized
if (!state.motherHandouts) {
state.motherHandouts = {
count: 0,
handouts: []
};
} else {
// Ensure the handouts array is properly initialized
state.motherHandouts.handouts = state.motherHandouts.handouts || [];
}
// Logging function
function logDebug(message) {
log(`DEBUG: ${message}`);
}
// Function to create a new handout
function createHandout(player, handoutName) {
logDebug(`Creating handout with name: ${handoutName}`);
state.motherHandouts.count += 1;
let flaggedHandoutName = `Mother ${('000' + state.motherHandouts.count).slice(-3)} - ${handoutName}`;
let handout = createObj('handout', {
name: flaggedHandoutName,
inplayerjournals: "all",
controlledby: (playerIsGM(player.id) ? '' : player.id)
});
// Ensure the handouts array is not undefined
if (!state.motherHandouts.handouts) {
state.motherHandouts.handouts = [];
}
state.motherHandouts.handouts.push({
id: handout.id,
name: flaggedHandoutName
});
logDebug(`Handout created with ID: ${handout.id}`);
sendChat('Mother', `/w "${player.get('displayname')}" Created handout: [${flaggedHandoutName}](<a href="http://journal.roll20.net/handout/${handout.id})`" rel="nofollow">http://journal.roll20.net/handout/${handout.id})`</a>);
}
// Function to clear the state without removing handouts
function clearAllHandouts(player) {
logDebug(`Resetting state and clearing all stored handouts information.`);
state.motherHandouts = {
count: 0,
handouts: []
};
sendChat('Mother', `/w "${player.get('displayname')}" State has been reset and all stored handouts information has been cleared from state.`);
logDebug(`State reset and all stored handouts information cleared from state.`);
}
// Function to update the last handout with text, simulating typing
function updateHandout(player, text) {
logDebug(`Updating last handout with text: ${text}`);
let handoutInfo = state.motherHandouts.handouts[state.motherHandouts.handouts.length - 1];
if (handoutInfo) {
let handout = getObj('handout', handoutInfo.id);
if (handout) {
handout.get('notes', function(notes) {
let currentNotes = notes || '';
let characters = text.split('');
let index = 0;
let interval = setInterval(() => {
if (index < characters.length) {
let delay = Math.floor(Math.random() * (2000 - 100 + 1)) + 100;
currentNotes += characters[index++];
let styledNotes = `<div style="background-color: #000000; color: #00FF00;">${currentNotes}</div>`;
handout.set('notes', styledNotes);
if (index === characters.length) {
clearInterval(interval);
sendChat('Mother', `/w "${player.get('displayname')}" Updated handout: ${handout.get('name')}`);
logDebug(`Handout updated with new text`);
}
}
}, 100);
});
} else {
sendChat('Mother', `/w "${player.get('displayname')}" No handout found to update.`);
logDebug(`No handout found to update`);
}
} else {
sendChat('Mother', `/w "${player.get('displayname')}" No handout found to update.`);
logDebug(`No handout found to update`);
}
}
// Function to send the main menu
function sendMainMenu(playerid) {
logDebug(`Sending main menu to player ID: ${playerid}`);
let menu = `&{template:default} {{name=Mother Handouts Menu}}`
+ `{{[Create Handout](~!mother --name|?{What should the handout be named?})}}`
+ `{{[Clear All Handouts](~!mother --clear-All)}}`
+ `{{[Update Last Handout](~!mother --text|?{What text should be added to the last handout?})}}`;
sendChat('Mother', `/w "${getObj('player', playerid).get('displayname')}" ${menu}`);
logDebug(`Main menu sent`);
}
// Main command listener
on('chat:message', function(msg) {
if (msg.type === 'api') {
let player = getObj('player', msg.playerid);
if (!player) {
logDebug(`No player found with ID: ${msg.playerid}`);
return;
}
logDebug(`API message received: ${msg.content}`);
if (msg.content.indexOf('!mother --name|') !== -1) {
let handoutName = msg.content.replace('!mother --name|', '').trim();
logDebug(`Received command to create handout with name: ${handoutName}`);
createHandout(player, handoutName);
} else if (msg.content === '!mother --clear-All') {
logDebug(`Received command to clear all handouts`);
clearAllHandouts(player);
} else if (msg.content.indexOf('!mother --text|') !== -1) {
let text = msg.content.replace('!mother --text|', '').trim();
logDebug(`Received command to update handout with text: ${text}`);
updateHandout(player, text);
} else if (msg.content === '!mother --main') {
logDebug(`Received command to send main menu`);
sendMainMenu(msg.playerid);
}
}
});
});
there are a few things I can't figure out. 1. when I use the button to make a new handout I get this message: No ability was found for %{!mother --name|?{What should the handout be named?} but I can still create the handout 2. when I edit the text within the handout I get this message: No ability was found for %{!mother --text|?{What text should be added to the last handout?} but I can still edit the text and it inputs with a random delay of up to 2000ms. 3. When I try to clear the state of all stored handouts and reset the incremental number back to 001 I get this message: TypeError: Cannot read properties of undefined (reading 'substring') and the state isn't reset.