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.)

  • Sk1ll_Issue@feddit.nlOP
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    1 month ago

    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! :)

    • VioneT@lemmy.worldM
      link
      fedilink
      English
      arrow-up
      2
      ·
      edit-2
      1 month ago

      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’.