feat: random word macros
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
import renderSettingsGui from '@/gui'
|
import renderSettingsGui from '@/gui'
|
||||||
|
import initializeMacros from '@/macros/initializeMacros'
|
||||||
|
|
||||||
renderSettingsGui()
|
renderSettingsGui()
|
||||||
|
initializeMacros()
|
||||||
|
|||||||
7
src/macros/initializeMacros.ts
Normal file
7
src/macros/initializeMacros.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import randomWord from '@/macros/randomWord'
|
||||||
|
|
||||||
|
const initializeMacros = () => {
|
||||||
|
randomWord()
|
||||||
|
}
|
||||||
|
|
||||||
|
export default initializeMacros
|
||||||
43
src/macros/randomWord.ts
Normal file
43
src/macros/randomWord.ts
Normal 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
|
||||||
Reference in New Issue
Block a user