Greetings command

Hello again. I am tried create greetings command, but with using translate nickname.

$(eval n=‘$(user)’; $(urlfetch https://pastebin.com/raw/xxxxxxxx))

Pastebin:

{
n.toLocaleLowerCase().replace(/v/g, ‘в’).replace(/i/g, ‘и’).replace(/s/g, ‘с’).replace(/l/g, ‘л’).replace(/e/g, ‘е’).replace(/k/g, ‘к’).replace(/a/g, ‘а’).replace(/b/g, ‘б’).replace(/c/g, ‘с’).replace(/d/g, ‘д’).replace(/e/g, ‘е’).replace(/f/g, ‘ф’).replace(/h/g, ‘х’).replace(/j/g, ‘ж’).replace(/m/g, ‘м’).replace(/o/g, ‘о’).replace(/p/g, ‘п’).replace(/q/g, ‘к’).replace(/r/g, ‘р’).replace(/o/g, ‘о’);
}

but I don’t understand, after the next addition of replace I get an error: Unexpected identifier.

Hey @VillsEK!

So you’re saying that before you add .replace(/o/g, 'о') the code works fine? And if you remove it, the code works again, right?
That’s probably because you already do that operation 4 steps before.

You also do the same thing for .replace(/e/g, 'е'): 5th and 11th step.

Side note: I think I’d prefer using .toLowerCase() instead of .toLocaleLowerCase(), the bot doesn’t have the same locale as you do, better be safe and normalize the output to something consistent.

Hi Emily, i am tried replace “o” on other letters:

{ 1. n.toLocaleLowerCase().replace(/v/g, ‘в’).replace(/i/g, ‘и’).replace(/l/g, ‘л’).replace(/s/g, ‘с’).replace(/e/g, ‘е’).replace(/k/g, ‘к’).replace(/k/g, ‘к’).replace(/a/g, ‘а’).replace(/b/g, ‘б’).replace(/c/g, ‘с’).replace(/d/g, ‘д’).replace(/e/g, ‘е’).replace(/f/g, ‘ф’).replace(/g/g, ‘г’).replace(/h/g, ‘х’).replace(/j/g, ‘ж’).replace(/m/g, ‘м’).replace(/n/g, ‘н’).replace(/o/g, ‘о’).replace(/p/g, ‘п’); }

but if i am will remove last letter(.replace(/p/g, ‘п’)), then work.
image
I tried add other letter, but return error:
image

I noticed that if you add 20th time .replace, then it will not work, but with 19th .replace - it work.
P.S. thank with letter “e”, I have corrected.

maybe there are other options how it would be possible to replace the letters?

Now you have a double .replace(/k/g, 'к'): 6th and 7th position.
Can’t you do the .replace() in alphabetical order to prevent duplicates?
а, б, в, г, д, е, ё...


In testing I manage to go over 20 .replace() just fine, it works as expected:

n.toLowerCase().replace(/a/g, 'а').replace(/b/g, 'б').replace(/v/g, 'в').replace(/g/g, 'г').replace(/d/g, 'д').replace(/e/g, 'е').replace(/j/g, 'ж').replace(/z/g, 'з').replace(/i/g, 'и').replace(/k/g, 'к').replace(/q/g, 'к').replace(/c/g, 'к').replace(/l/g, 'л').replace(/m/g, 'м').replace(/n/g, 'н').replace(/o/g, 'о').replace(/p/g, 'п').replace(/r/g, 'р').replace(/s/g, 'с').replace(/t/g, 'т').replace(/u/g, 'у').replace(/f/g, 'ф').replace(/h/g, 'х');

I ignored some letters like ё, ц, ч, ш, я..., since their sounds equivalent are multiple English letters.
If you wish to replace those, you’ll have to put the .replace() first in the chain, for example .replace(/io/g, 'ё') and then followed by all the single letters .replace():

n.toLowerCase().replace(/io/g, 'ё').replace(/a/g, 'а') ...

hm, i am copy and paste ur code in my pastebin, but return error yet…
image

$(eval n=‘$(user)’;$(urlfetch https://pastebin.com/raw/wbZb1MmC))

My pastebin:


https://pastebin.com/wbZb1MmC
Error:
image

what am I doing wrong?

Remove the curly brackets on line 1 and 3, this isn’t JSON, but JavaScript, or JS for short.


image

Same

Ah, I missed it, add json in your $(urlfetch), it’s a trick to grab more than the maximum length the variable allows you to:

$(urlfetch json URL)

Thank a lot Emily, I made a lot of mistakes, but I am so grateful to you for helping me. I hope in the future I can do it myself :slight_smile:

1 Like

No worries, it’s by making mistakes that we learn, perseverance is what will allow you to do it by yourself in the future, I started like you. :wink:

1 Like

Emily, one more question: how i can add commands of two word, example: ‘hi all’:
image
Return nothing:
image
But i am using on my language:
“Всем привет”
if i create only “Всем” - it meaning “all” - there may be an error, because it may be in a different context;
If i create only “привет” - it meaning “hi or hello” - then when he writes “Всем привет” - it will not work.
I’m trying to find from the created topics, maybe I’m not looking well.

This should help:


I see you’re already using an $(eval), note that you only need one per command.

If it’s the same command on which we’ve been working, this is how to set it up:

!addcom всем $(eval n='$(touser)'; '$(query)'.toLowerCase().includes('привет') ? $(urlfetch json https://pastebin.com/raw/XXXXXXXX) : ' ';)

I know a bit of Russian. :wink:

Hi Emily, return error:
image

I removed ; in pastebin and replaced touser with user
but now just when you type “всем” - return it:
image

I also wanted to add other options to includes such as: хай(hey), ку(qq),
separated by commas, but the result is the same as “всем”

I’m already thinking about leaving everything as it was without “всем” :slight_smile:

or maybe somehow nighbot can answer a word “привет” in message in chat? exceptions: if reply message user in chat twitch

Ah yes, good catch, I forgot to tell you to remove it, otherwise it breaks the ternary operator.

For that you need to do things a bit differently, .includes() only takes in one string, adding commas will break the test, and therefore that’s why it works with всем by itself, this should work:

!addcom всем $(eval n='$(touser)'; ['привет', 'хай', 'ку'].includes('$(query)'.toLowerCase()) ? $(urlfetch json https://pastebin.com/raw/XXXXXXXX) : ' ';)

Thanks a lot Emily, it work. But i am use “user” at the beginning of the code which the original name without translate, and if just write: “всем”, it return original nickname:
image

i use more eval than one which used random greetings and random smile(i.e 2 eval):

$(eval n=‘$(touser)’; [‘привет’, ‘хай’, ‘ку’].includes(‘$(query)’.toLowerCase()) ? $(urlfetch json https://pastebin.com/raw/XXXXXXXX) : ’ '; ) $(eval const r= [‘greetings1’,‘greetings2’]) r[Math.floor(Math.random() * r.length)]; ) $(eval const s=$(urlfetch json https://pastebin.com/raw/XXXXXXXX); s[Math.floor(Math.random() * s.length)]; )

Second pastebin has:

[‘smile1’, ‘smile2’,‘smile3’,‘smile4’ etc…]

Because i am return this:
image

I’m still thinking how to simplify the code.

may be to do this under a single pastebin? but still, to tag someone who is greetings seems to remain impossible.

I think I made it difficult for myself and for you :frowning:

That’s what I did

let out = ‘’;
{
let name = n;
let name2 = n.toLowerCase().replace(/aya/g, ‘ая’).replace(/ay/g, ‘ай’).replace(/ey/g, ‘ей’).replace(/yo/g, ‘йо’).replace(/cia/g, ‘ция’).replace(/sh/g, ‘ш’).replace(/ight/g, ‘айт’).replace(/ch/g, ‘ч’).replace(/tch/g, ‘тч’).replace(/a/g, ‘а’).replace(/b/g, ‘б’).replace(/v/g, ‘в’).replace(/g/g, ‘г’).replace(/d/g, ‘д’).replace(/e/g, ‘е’).replace(/j/g, ‘ж’).replace(/z/g, ‘з’).replace(/i/g, ‘и’).replace(/k/g, ‘к’).replace(/q/g, ‘к’).replace(/c/g, ‘к’).replace(/l/g, ‘л’).replace(/m/g, ‘м’).replace(/n/g, ‘н’).replace(/o/g, ‘о’).replace(/p/g, ‘п’).replace(/r/g, ‘р’).replace(/s/g, ‘с’).replace(/t/g, ‘т’).replace(/u/g, ‘у’).replace(/f/g, ‘ф’).replace(/h/g, ‘х’).replace(/w/g, ‘в’);
out = ${name} ${name2};
}
out

I tried to do as with the weather:

but all in vain…

I’ll try to fix your code at once, but if you keep adding stuff and don’t really understand what you’re doing, you’re only making it more difficult for yourself, and I don’t have time to teach you JavaScript; and by feeding me the information bits by bits, you’re making this longer than it needs to be.

  • You only need a single $(eval) variable per command where you use JS, the moment you add a second one, you’re doing it wrong.
  • I don’t understand why you keep using the curly brackets {} to contain your .replace() code, it’s not needed.
  • Since you’re doing a lot more than just a simple operation in case the user entered the correct sentence, we can’t use the ternary operator ?: anymore, so we’re gonna use an if() instead.
  • You don’t really need the out variable with Nightbot, I just replace it with a template literal instead.
  • Since you’re using a lot of “smiles,” and you’re likely to add more “greetings,” we’ll combine everything in the Pastebin; that’s also one less $(urlfetch) call which will reduce the code execution time.

!addcom всем $(eval const name = '$(user)'; if (['привет', 'хай', 'ку'].includes('$(query)'.toLowerCase())) { $(urlfetch json https://pastebin.com/raw/XXXXXXXX) } else { ' '; })

And then in your Pastebin have the following code:

const random = (array) => array[Math.floor(Math.random() * array.length)];

const greetings = ['greeting1', 'greeting2'];
const smiles = ['smile1', 'smile2', 'smile3', 'smile4'];

const nameEdited = name.toLowerCase().replace(/aya/g, 'ая').replace(/ay/g, 'ай').replace(/ey/g, 'ей').replace(/yo/g, 'йо').replace(/cia/g, 'ция').replace(/sh/g, 'ш').replace(/ight/g, 'айт').replace(/ch/g, 'ч').replace(/tch/g, 'тч').replace(/a/g, 'а').replace(/b/g, 'б').replace(/v/g, 'в').replace(/g/g, 'г').replace(/d/g, 'д').replace(/e/g, 'е').replace(/j/g, 'ж').replace(/z/g, 'з').replace(/i/g, 'и').replace(/k/g, 'к').replace(/q/g, 'к').replace(/c/g, 'к').replace(/l/g, 'л').replace(/m/g, 'м').replace(/n/g, 'н').replace(/o/g, 'о').replace(/p/g, 'п').replace(/r/g, 'р').replace(/s/g, 'с').replace(/t/g, 'т').replace(/u/g, 'у').replace(/f/g, 'ф').replace(/h/g, 'х').replace(/w/g, 'в');

`${name} ${nameEdited} ${random(greetings)} ${random(smiles)}`;

  • on the first line, random() is a function, I decided to add it in order to not repeat the same bit of code twice, the goal is to keep the code DRY.

  • the last line could have been written like so:

    name + ' ' + nameEdited + ' ' + random(greetings) + ' ' + random(smiles);
    

Thanks a lot Emily it work, sorry for wasting time and nerves. :frowning:
But now I know more how to write this kind of codes, I hope in the future I can do it myself(
I repeat myself…) :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.