refactor(randomWord): update macro registration to use new SillyTavern API

This commit is contained in:
2026-01-03 19:38:48 -06:00
parent cfef4fd73d
commit 07f6124f99
2 changed files with 96 additions and 97 deletions

162
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -2,27 +2,26 @@ import { useStore } from '@/store'
import type { generationData } from '@/types/SillyTavern'
const registerRandomWordMacros = (dictName: string) => {
const { registerMacro } = SillyTavern.getContext()
const { macros } = SillyTavern.getContext()
const words = useStore.getState().wordLists[dictName]
if (words === undefined || words.length === 0) {
// If the word list is empty or undefined, don't register the macro
return
}
registerMacro(
`randomWord::${dictName}`,
() => {
macros.register(`randomWord::${dictName}`, {
category: 'random',
handler: () => {
const words = useStore.getState().wordLists[dictName] as string[]
return words[Math.floor(Math.random() * words.length)] as string
},
`Generates a random word from the word list '${dictName}'`
)
registerMacro(
`placeholder::randomWord::${dictName}`,
() => {
}
})
macros.register(`placeholder::randomWord::${dictName}`, {
category: 'random',
handler: () => {
return `%%randomWord::${dictName}%%`
},
`Returns a placeholder for a random word from the word list '${dictName}'`
)
}
})
}
export const setupRandomWordMacros = () => {
@@ -38,10 +37,10 @@ export const setupRandomWordMacros = () => {
const deletedWordLists = oldWordListsSet.difference(wordListsSet)
const newWordLists = wordListsSet.difference(oldWordListsSet)
const { unregisterMacro } = SillyTavern.getContext()
const { macros } = SillyTavern.getContext()
for (const wordList of deletedWordLists) {
unregisterMacro(`randomWord::${wordList}`)
unregisterMacro(`placeholder::randomWord::${wordList}`)
macros.registry.unregisterMacro(`randomWord::${wordList}`)
macros.registry.unregisterMacro(`placeholder::randomWord::${wordList}`)
}
for (const wordList of newWordLists) {