this post was submitted on 06 Aug 2024
1 points (100.0% liked)

Perchance - Create a Random Text Generator

449 readers
18 users here now

⚄︎ Perchance

This is a Lemmy Community for perchance.org, a platform for sharing and creating random text generators.

Feel free to ask for help, share your generators, and start friendly discussions at your leisure :)

This community is mainly for discussions between those who are building generators. For discussions about using generators, especially the popular AI ones, the community-led Casual Perchance forum is likely a more appropriate venue.

See this post for the Complete Guide to Posting Here on the Community!

Rules

1. Please follow the Lemmy.World instance rules.

2. Be kind and friendly.

  • Please be kind to others on this community (and also in general), and remember that for many people Perchance is their first experience with coding. We have members for whom English is not their first language, so please be take that into account too :)

3. Be thankful to those who try to help you.

  • If you ask a question and someone has made a effort to help you out, please remember to be thankful! Even if they don't manage to help you solve your problem - remember that they're spending time out of their day to try to help a stranger :)

4. Only post about stuff related to perchance.

  • Please only post about perchance related stuff like generators on it, bugs, and the site.

5. Refrain from requesting Prompts for the AI Tools.

  • We would like to ask to refrain from posting here needing help specifically with prompting/achieving certain results with the AI plugins (text-to-image-plugin and ai-text-plugin) e.g. "What is the good prompt for X?", "How to achieve X with Y generator?"
  • See Perchance AI FAQ for FAQ about the AI tools.
  • You can ask for help with prompting at the 'sister' community Casual Perchance, which is for more casual discussions.
  • We will still be helping/answering questions about the plugins as long as it is related to building generators with them.

6. Search through the Community Before Posting.

  • Please Search through the Community Posts here (and on Reddit) before posting to see if what you will post has similar post/already been posted.

founded 1 year ago
MODERATORS
 

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

top 5 comments
sorted by: hot top controversial new old
[–] [email protected] 2 points 3 months ago (2 children)

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)
  }
});

Images are from my unfinished VN: a-trip-to-the-beach :)

[–] [email protected] 1 points 3 months ago

Oooh, thank you! And thank you for commenting everything, super helpful!

[–] [email protected] 1 points 3 months ago* (last edited 3 months ago) (1 children)

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

[–] [email protected] 2 points 3 months ago* (last edited 3 months ago) (1 children)

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

[–] [email protected] 1 points 3 months ago

Got it, thanks! :D