I hope this is okay here, since it’s primarily a coding question. I’ve been going around in circles with the coding assistant, trying to get something that will listen for keywords in chat and change the background (and avatar, but I gave up on that for now) automatically. None of the code she’s given me has ever worked.
I did get this from her, which works:
`var backgroundUrls = {
'keyword1': 'https://example.com/background1.png',
'keyword2': 'https://example.com/background2.png',
// Add more background keywords and URLs as needed
};
function changeBackground(keyword) {
let link = backgroundUrls[keyword];
if (link) {
let prevMessage = oc.thread.messages.at(-2);
prevMessage.scene = { 'background': {'url': link} };
oc.thread.messages.pop(); // Remove the command message from the chat
} else {
oc.thread.messages.push({
author: 'system',
content: `<div class="error-message">Error: Invalid background keyword provided. Please use one of the following: ${Object.keys(backgroundUrls).join(', ')}.</div>`
});
}
}
oc.thread.on("MessageAdded", function({message}) {
if (message.author == 'user' && /^\/change-bg/.test(message.content)) {
let keyword = message.content.replace('/change-bg ', '');
changeBackground(keyword);
}
});`
This at least allows you preset keywords and urls so you can just type “/change-bg keyword” instead of “/change-bg url”, so…it’s something, I guess.
But I’d really like it to happen automatically, like if a keyword shows up in the course of chat, the background changes. But nothing I tried ever seemed to be able to change the avatar (but I guess it would only work on the active character? Does custom code execute on imported characters?)
(Edit: Thought I’d be cute and give the coding assistant the ability to look at urls, and pointed her at the custom code pages, but it doesn’t retrieve enough of the page to be useful, I don’t think.)
Here’s a code that might be what you want:
const backgroundUrls = { 'beach': 'https://user-uploads.perchance.org/file/a4c99b37a4d466b9a3fac8c9864d92b7.webp', 'cottages': 'https://user-uploads.perchance.org/file/13a5f82ae06233afcd35df95c5ffe806.webp', // Add more background keywords and URLs as needed }; const avatarUrls = { 'ramon': 'https://user-uploads.perchance.org/file/c986b9d02086c6cf9833761199d2fc52.webp', 'kylie': 'https://user-uploads.perchance.org/file/a6ba77a96dfd17a50101eba227435c07.webp', // Add more avatar keywords as needed }; oc.thread.on("MessageAdded", function({message}) { if (message.author == 'user') { // you can change which reply can trigger the code, currently it is the user triggering the code let words = message.content.split(/[\s+']/gm).map(a => a.toLowerCase()) // just separates the text into words let foundBG = Object.keys(backgroundUrls).find((a) => words.includes(a)); // finds the keyword that is found on the message let foundAVATAR = Object.keys(avatarUrls).find((a) => words.includes(a)); if (foundBG) oc.thread.messages.at(-1).scene = { 'background': {'url': backgroundUrls[foundBG]} }; // if a background keyword is found, change the bg if (foundAVATAR) oc.character.avatar.url = avatarUrls[foundAVATAR]; // if an avatar keyword is found, change the avatar message.expectsReply = false; // this just tells the AI not to reply to the user's message (remove if you need the AI to reply) } });
So, first, it works brilliantly, thanks! One quirk, the keywords don’t seem to like spaces, which is weird. In the code I posted I was able to use “living room” as a keyword, but in your code it wouldn’t trigger until I shortened it to “living”. Any idea why that might be? Still very usable, I just thought it was a bit odd. (I did remove the if (message.author line and the expectsReply line.)
Edit: And I like your VN! :)
It’s because the message is split by the whitespaces, meaning ‘living room’ would be split to ‘living’ and ‘room’. You could remove the
.split(...).map(...)
and just normalize the message with.toLowerCase()
i.e.message.content.toLowerCase()
then it should work for the ‘living room’.Got it, thanks! :D
Oooh, thank you! And thank you for commenting everything, super helpful!