feat: random word macros

This commit is contained in:
2025-08-14 18:18:00 -06:00
parent 9121bb287c
commit a3c6c9fa76
3 changed files with 52 additions and 0 deletions

View File

@@ -1,3 +1,5 @@
import renderSettingsGui from '@/gui'
import initializeMacros from '@/macros/initializeMacros'
renderSettingsGui()
initializeMacros()

View File

@@ -0,0 +1,7 @@
import randomWord from '@/macros/randomWord'
const initializeMacros = () => {
randomWord()
}
export default initializeMacros

43
src/macros/randomWord.ts Normal file
View File

@@ -0,0 +1,43 @@
import { useStore } from '@/store'
const randomWord = () => {
const { registerMacro, unregisterMacro } = SillyTavern.getContext()
useStore.subscribe(
(state) => Object.keys(state.wordLists),
(wordLists, oldWordLists) => {
const wordListsSet = new Set(wordLists)
const oldWordListsSet = new Set(oldWordLists)
const deletedWordLists = oldWordListsSet.difference(wordListsSet)
const newWordLists = wordListsSet.difference(oldWordListsSet)
for (const wordList of deletedWordLists) {
unregisterMacro(`rndWord::${wordList}`)
}
for (const wordList of newWordLists) {
registerMacro(
`rndWord::${wordList}`,
() => {
const words = useStore.getState().wordLists[wordList] as string[]
return words[Math.floor(Math.random() * words.length)] as string
},
`Generates a random word from the word list '${wordList}'`
)
}
}
)
for (const wordList of Object.keys(useStore.getState().wordLists)) {
registerMacro(
`rndWord::${wordList}`,
() => {
const words = useStore.getState().wordLists[wordList] as string[]
return words[Math.floor(Math.random() * words.length)] as string
},
`Generates a random word from the word list '${wordList}'`
)
}
}
export default randomWord