Skip to content

Commit

Permalink
Merge pull request #767 from hktrpg/fix-undefined-(reading-'match'
Browse files Browse the repository at this point in the history
try
  • Loading branch information
zeteticl authored Nov 18, 2024
2 parents ad4c134 + 598f03a commit 9fb9499
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions modules/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,23 +238,40 @@ const rolldice = async ({
}

function findRollList(mainMsg) {
if (!mainMsg || !mainMsg[0]) return;
mainMsg[0].match(/^\.(\d{1,2})$/) ? mainMsg.shift() : null;
// Return early if mainMsg is null/undefined or empty
if (!mainMsg || !Array.isArray(mainMsg) || mainMsg.length === 0) return;

// Check if first element matches pattern and shift if true
if (mainMsg[0] && mainMsg[0].match(/^\.(\d{1,2})$/)) {
mainMsg.shift();
}

// Set default empty string for mainMsg[1] if undefined
if (!mainMsg[1]) mainMsg[1] = '';

const idList = Object.values(exports);
const findTarget = idList.find(item => {
if (item.prefixs && item.prefixs()) {
return item.prefixs().some(prefix =>
mainMsg && mainMsg[0].match(prefix.first) &&
(mainMsg[1] && mainMsg[1].match(prefix.second) || prefix.second == null)
);
if (item && item.prefixs && typeof item.prefixs === 'function') {
const prefixList = item.prefixs();
if (!Array.isArray(prefixList)) return false;

return prefixList.some(prefix => {
// Check if mainMsg[0] exists and matches first prefix
if (!mainMsg || !mainMsg[0] || !prefix || !prefix.first) return false;
const firstMatch = mainMsg[0].match(prefix.first);
if (!firstMatch) return false;

// Check second prefix if it exists
if (prefix.second === null) return true;
return mainMsg[1] && mainMsg[1].match(prefix.second);
});
}
return false;
});

return findTarget;
}


async function stateText() {
let state = await getState() || '';
if (!Object.keys(state).length || !state.LogTime) return;
Expand Down

0 comments on commit 9fb9499

Please sign in to comment.