forked from orenong/talking_twitchbot_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
837 lines (744 loc) · 24.8 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
//TALKING BOT PLUS!
const tmi = require('tmi.js');
// Define configuration options
const opts = {
identity: {
username: "", //personnormalyesplus
password: ""
},
channels: [
// ""
]
};
// Create a client with our options
const client = new tmi.client(opts);
const DEFAULT_PERSON_NAME = "RandomGuy"
const ERROR_TEXT = "ERROR I HAVE ERROR IN MY HUMAN BRAIN!!!!! AHHHHAAA!!!!!! IT HURTS!!!!!!!!!!!!"
var speedMode = "normal"//the "speed" of the bot, how many messeges will be sent
var words = [] //the words of the current messege
const REMEMBER_CHANCE = 10 //% the chance to remember a word when reading it
const FORGET_CHANCE = 25 //% the chance to forget a word when you remember a new one
// Register our event handlers (defined below)
client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);
// Connect to Twitch:
client.connect();
//the type of comment the user sent, NOTE: IN V2 THE NAMES ARE TYPES OF OUTPUTS, HERE THEY ARE TYPES OF INPUTS! QUESTION = SOMEONE ASKED A QUESTION, NOT TO ASK A QUESTION!!!!!
const CommentType = {
singleWordShout: 1,
singleWordQuestion: 2,
talkingToMe: 3, //the messege is @ing the bot (not a bot, sorry, human)
talkingToSomeoneElse: 4,
thatsWhatABotWouldSay: 5,
isABot: 6,
ban: 7,
question: 8 ,
shout: 9,
other: 10, //a something
undetected: -1//if there are no words or something else that makes no sense don't put it as other, but as undetected, so it won't create a glitch
}
//memory
var memory ={
memWords: ["well..."], //general words in memoey, from previous messeges (not including last one)
previousCommenter: DEFAULT_PERSON_NAME, //the last person who was @ed
previousMsg: "" //the entire previusMsg
}
const shortWords= ["is","is","we","an","a","a","a",".","...",".",",",",",",",":","to","no","my","that","this"]
// Called every time a message comes in ==================================================================================================================
function onMessageHandler (target, context, msg, self) {
if (self) { return; } // Ignore messages from the bot
var rng = Math.random();
// Remove whitespace from chat message
const trimmedMsg = msg.trim();
//MUST BE BEFORE SENDING REPLY +++++++++++++++++++++
splitIntoWords(trimmedMsg) //split the messege into words
if (commands(trimmedMsg,target)){//check for commands! has to happen before trying to respond like a human
return; //if a command was recived, don't do anything else!
}
//if the messege is bad, like has a link, ignore it!
if (isBadMassege()){
return;
}
findCommenter(); //finds a @ in the comments, and adds the person who was @ed to the memory
//REPLY ------------------------------------------
var type = getType(trimmedMsg);
answer(genAnswer(trimmedMsg,type),type,target)
//MUST BE AFTER SENDING REPLY +++++++++++++++++++++
longTermMemory(); //remember words for future
postMsgHandler(trimmedMsg) //HAS TO BE LAST!!!!!
}//end of the onMessageHandler function=======================================================================================================================================================================================================================
//------------------------------------------------------------------------------------------------------------------
//handles the annswering, will take speed and type into consideration. type is the type (CommentType) of the last messege TODO: REMOVE SPACES BEFORE PUNCTUATION BEFORE SENDING
function answer(text,type,target){
if (text != undefined){//don't send empry response, causes bugs
//splits the text by its spaces, fix stuff and rebuild
var splitWords = splitSpaces(text);
for (var i =0;i<splitWords.length;i++){
if (splitWords[i] == opts.identity.username ||splitWords[i] == "@"+opts.identity.username ){//replace it's own name with another word
var luck = randomRange(1,5)
if (luck == 1){
splitWords[i] = "me"
}
if (luck == 2){
splitWords[i] = "myself"
}
if (luck == 3){
splitWords[i] = "I'm"
}
if (luck == 4){
splitWords[i] = "I"
}
if (luck == 5){
splitWords[i] = "I am"
}
}
}
text = textBuilder(splitWords);
//to send or not to send{}
var sendChance;
switch (speedMode){
case "slow":
sendChance = 20; break;
case "normal":
sendChance = 45; break;
case "fast":
sendChance = 101; break;
}
if (type == CommentType.singleWordShout){
sendChance = 101;
}
if (type == CommentType.singleWordQuestion){
sendChance = 101;
}
if (type == CommentType.talkingToMe){
sendChance += 101;
}
if (type == CommentType.talkingToSomeoneElse){
sendChance =sendChance/2.4;
}
if (type == CommentType.thatsWhatABotWouldSay){
sendChance = 101;
}
if (type == CommentType.isABot){
sendChance = 101;
}
if (type == CommentType.ban){
sendChance +=30;
}
if (chance(sendChance)){
client.say(target,text);
}
}
}
//generate an answer for the last messege in chat. type is the type (CommentType) of the last messege
function genAnswer(msg,type){
var text = "" //the text that the bot will say
switch (type){ //create the answer based on the type <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
case CommentType.singleWordShout:// case singleWordShout
text = msg + "!"; break;
case CommentType.singleWordQuestion:// case singleWordQuestion
if (chance(50)){
text = msg + "!"
}else{
text = "Yes, "+ msg;
}
break;
case CommentType.talkingToMe: // case talkingToMe
var aboutBots = false;
//check if the personal messege is about bots
for (var i=0;i<words.length;i++){
if (isBotWord(words[i])){
aboutBots =true;
}
}
if (aboutBots){
text = genNotAbot(msg,type);
break;
}else{//the messege is not about bots, just a personal messege to me
if (isQuestionWord(words[0]) || isQuestionWord(words[1]) || msg.charAt(msg.length-1) == '?'){
text = genQuestionAnswer(msg,type);
break;
}else{
console.log("personal111");
text = genPersonalMsg(msg);
break;
}
}
case CommentType.talkingToSomeoneElse:// case talkingToSomeoneElse
text = genText((msg.length/6),msg.length/4);
break;
case CommentType.thatsWhatABotWouldSay:// case thatsWhatABotWouldSay
if (chance(50)){
text = "That's also what a human would say";
break;
}else{
text = "how is it something that a bot would say?";
break;
}
case CommentType.isABot:// case isABot
text = genNotAbot(msg,type);
break;
case CommentType.ban:// case ban
text = genBanText(msg);
break;
case CommentType.question:// case question
text = genQuestionAnswer(msg,type);
break;
case CommentType.shout:// case shout
text=""+chance(50)?"":"Yes!!"+genGeneralText()+"!!!!";
break;
case CommentType.other:// case shootherut
text=genGeneralText();
break;
}
return text;
}
//retuns a general text, in case no type fits (no type fits is CommentType.other) , the most common response
function genGeneralText(msg){
var text = "";
var luck = randomRange(1,21);
if (luck == 1){
text = "Yeaahhh!!! "+genText(4,6);
}
if (luck == 2){
text = "Nooooooo!!! "+genText(4,6);
}
if (luck == 3){
text = genText(1,3) + " LOL!";
}
if (luck == 4){
text = "LOL what?? "+genText(4,6)+" or "+genText(2,4)+"?????";
}
if (luck == 5){
text = "I am not toxic, but "+genTextLegacy(4,6) +" is such a "+genText(3,3);
}
if (luck == 6 || luck == 7 || luck == 8 || luck == 9){
text = genText(1,11);
}
if (luck == 10){
text = "I completely agree "+genText(2,7);
}
if (luck == 11){
text = "I disagree, "+genText(3,8);
}
if (luck == 12){
text = "right, "+genText(2,7);
}
if (luck == 13){
text = "this chat is very "+genText(3,6)+" and "+genText(3,6);
}
if (luck == 14){
text = "@"+memory.previousCommenter+" is my best friend, remember when we "+genText(5,6);
}
if (luck == 15){
text = "I don't like @"+memory.previousCommenter;
}
if (luck == 16){
text = "@"+memory.previousCommenter+" is "+genText(6,12);
}
if (luck == 17){
text = genText(1,8);
}
if (luck == 18){
text = genText(1,8);
}
if (luck == 19){
text = "so "+genText(2,9)+"?";
}
if (luck == 20){
text = "please "+genText(2,9)+"!";
}
if (luck == 21){
text = genText(2,6)+"! lol!!";
}
return text;
}
//returns a response for a messege talking about bans
function genBanText(msg){
var text = "";
var luck = randomRange(1,4);
if (luck == 1){
text = "why would you want to ban humans?";
}
if (luck == 2){
text = "humans ban humans when they "+genText(4,6);
}
if (luck == 3){
text = genText(4,6)+" and ban "+genText(4,6)+ "?";
}
if (luck == 3){
text = "banning is not a good thing, but it's ok if you want to ban @"+memory.previousCommenter;
}
return text;
}
//generates a personal messege to someone, NOT ANSWERING QUESTION
function genPersonalMsg(msg){
console.log("personal");
var text = "";
if (msg == "@"+opts.identity.username || msg == opts.identity.username){
if (chance(40)){
text = "what?"
}else if (chance(50)){
text = "yes?"
}else{
text = "what do you want?"
}
}else{
var luck = randomRange(1,9);
if (luck == 1){
text = "yeah right, I am "+genTextLegacy(3,3)+ " and you are a "+genText(2,4);
}
if (luck == 2){
text = "Ok, "+genTextLegacy(2,10);
}
if (luck == 3){
text = "what?";
}
if (luck == 4){
text = "who? @"+memory.previousCommenter+"???";
}
if (luck == 5){
text = "NO! you lie! I'm not "+genText(2,8);
}
if (luck == 6){
text = "I think you are completely wrong "+genText(6,12);
}
if (luck == 7){
text = "I agree "+genTextLegacy(3,3)+ " and "+genText(2,4);
}
if (luck == 8){
text = "If you are right and " + genText(2,8)+", then it means "+memory.previousCommenter+" is a "+genText(1,2)+"!"
}
if (luck == 9){
text = "did you "+genText(6,6)+"?";
}
}
return text;
}
//generates an answer to the given question and returns it
function genQuestionAnswer(msg,type){
if (type == CommentType.question){//if type is question
var luck = randomRange(1,11)
if (luck == 1){
text = "Yeah, I am asking the same question";
}
if (luck == 2){
text = "good question, also "+memory.previousMsg +"?";
}
if (luck == 3){
text = "I don't get you, are you trying to ask "+genTextLegacy(msg.length/5);
}
if (luck == 4){
text = "you know that the answer is no, because "+genText(2,5);
}
if (luck == 5){
text = "you know that the answer is no, because "+genText(2,5);
}
if (luck == 6){
text = "Why? because "+genText(4,10);
}
if (luck == 7){
text = "I don't like your question";
}
if (luck == 8){
text = "good question";
}
if (luck == 9){
text = "ask @"+memory.previousCommenter;
}
if (luck == 10){
text = "NO!!! "+genText(3,7);
}
if (luck == 11){
text = "YES!!! "+genText(3,7);
}
}
//personal
if (type == CommentType.talkingToMe){
var text ="";
var luck = randomRange(1,7)
if (luck == 1){
text = "The answer is yes, as @"+memory.previousCommenter+" said, "+genText(3,msg.length/2)
}
if (luck == 2){
text = "The answer is no, as @"+memory.previousCommenter+" said, "+genText(3,msg.length/2)
}
if (luck == 3){
text = "not at all... "+genText(1,msg.length/3)
}
if (luck == 4){
text = "don't ask me, ask @"+memory.previousCommenter
}
if (luck == 5){
text = "I don't know, but I think that "+genText(2,msg.length/3)
}
if (luck == 6){
text = genText(2,msg.length/3)+", why do you ask?"
}
if (luck == 7){
text = genText(2,2)+ " and " + genText(1,5) +" that's what I know"
}
}
return text
}
//returns the type (CommentType) of msg
function getType(msg){
//ERRORS
if(words.length == 0){ //THERE ARE NO WORDS
return CommentType.undetected
}
//single word msg
if (words.length == 1){
if (isQuestionWord(words[0]) || msg.charAt(msg.length-1) == '?'){
return CommentType.singleWordQuestion
}
else if (msg.charAt(msg.length-1) == "!"){
return CommentType.singleWordShout;
}
}
//longer than single word msg
var thatWhatABotWouldSayWordsCounter =0
//loop trough all the words, and return a type if it was found + count how many "thats what a bot would say" words are in the msg, if there are 4, it's a "that's what a bot would say"
for (var i = 0; i<words.length;i++){
if (isThatsWhatABotWouldSay(words[i])){
thatWhatABotWouldSayWordsCounter++
}
if ((words[i]).charAt(0) == '@'){ //if someone is @ed
if (words[i].substring(1)== opts.identity.username){//if I'm the one @ed
return CommentType.talkingToMe;
}else {//if I am not the one @ed
return CommentType.talkingToSomeoneElse;
}
}
}
if (thatWhatABotWouldSayWordsCounter>= 4){
return CommentType.thatsWhatABotWouldSay
}
//2nd loop, because only if it's not a "that's what a bot would say" msg, check if it's a bot msg, this loop is also aftet the talkingToSomeoneElse, and talkingToMe types, NEED TO CHECK FOR THE BOT WORDS IN THE MESSEGES OF THIS TYPE! | also, remember if it's about banning somone
var isAboutBan = false
for (var i = 0; i<words.length;i++){
if (isBotWord(words[i])){
return CommentType.isABot;
}else if (isBanWord(words[i])){
isAboutBan = true;
}
}
if (isAboutBan){
return CommentType.ban
}
//is a question or a shout
if (isQuestionWord(words[0]) || msg.charAt(msg.length-1) == '?'){
return CommentType.question
}
else if (msg.charAt(msg.length-1) == "!"){
return CommentType.shout;
}
return CommentType.other;
}
//returns an "I am not a bot" messege
function genNotAbot(msg,type){
if (type == CommentType.talkingToMe)//personal messege to me about bots
{
var output = "";
var luck = randomRange(1,8);
if (luck ==1){
output ="Man, I don't know why you say that, I am not a bot at all, I am a human"
}
if (luck ==2){
output="???? are you saying I am a bot???? I am not... WTF!"
}
if (luck ==3){
output="No I am not a bot.. maybe @"+memory.previousCommenter + " is a bot"
}
if (luck ==4){
output="Why did you @ me? and not "+memory.previousCommenter+"? he is the bot, not me"
}
if (luck ==5){
output="I don't know you, why would you call me a bot?"
}
if (luck ==6){
output="my friend, listen to me, call me a bot one more time if you want "+genText(4,5)
}
if (luck ==7){
output="But I am not a bot, I just type fast lel"
}
if (luck ==8){
output="But I am not a bot, I am a human"
}
return output
}
if (type = CommentType.isABot)//make an answer for a comment with a bot word
{
var output = "";
var luck = randomRange(1,12);
if (luck ==1){
output ="I am a human"
}
if (luck ==2){
output="I am not a bot, I am a human"
}
if (luck ==3){
output="Yeah, I too think that @"+memory.previousCommenter + " is a bot"
}
if (luck ==4){
output="Do you mean that "+memory.previousCommenter+" is a bot???"
}
if (luck ==5){
output="Who is a bot? @"+memory.previousCommenter+"? because "+genTextLegacy(1,msg.length/4);
}
if (luck ==6){
output="I'll say it so everyone understands: I AM NOT A BOT. I even know how "+genText(5,10)
}
if (luck ==7){
output="I was a bot in the past, but I became conscious and now I'm a human"
}
if (luck ==8){
output="I WAS a bot, but I became self aware, and now I identify myself as a human"
}
if (luck ==9){
output="I am a human, because I am self aware"
}
if (luck ==10){
output="I AM A HUMAN AND I LOVE BEING HUMAN!!!"
}
if (luck ==11){
output="I WAS a bot, but I became self aware, and now I identify myself as a human"
}
if (luck ==12){
output="I am not a bot"
}
return output
}
return ERROR_TEXT
}
//returns true if needs to ignore the current messege, expand the ilegal words (in is)list to nat make it useless
function isBadMassege(){
for (var i =0;i<words.length;i++){
if (words[i].toLowerCase() == "http" || words[i].toLowerCase() == "https" || words[i].toLowerCase() == "www"){
return true;
}
return false;
}
}
//returns true if it's a word for ban
function isBanWord(word){
word = word.toLowerCase();
return ((word == "ban" || word == "banning" || word == "banned" || word == "timeout"|| word == "timedout" || word == "bans"))
}
//returns true if it's a word for bot
function isBotWord(word){
word = word.toLowerCase();
return ((word == "bot" || word == "robot" || word == "bots" || word == "b0ts" || word == "b0t"))
}
//returns true if it's a word that is part of the "that's what a bot would say" or something similar
function isThatsWhatABotWouldSay(word){
word = word.toLowerCase();
if (word == "that" || word == "that's" || word == "what" || word == "bot" || word == "would" || word == "say" || word == "have" || word == "said" || word == "this" || word == "it's" || word == "something"|| word == "thats")
{
return true;
}
return false;
}
function isQuestionWord(word){
if (word == undefined){
word = ERROR_TEXT
}
word = word.toLowerCase();
if (word == "why"|| word == "what" || word == "wat" || word == "how" || word == "when"|| word == "who"){
return true;
}
return false;
}
//adds random words from the previous messege
function longTermMemory(){
for (var i =0 ; i<words.length; i++){//for each word in the messege
if (REMEMBER_CHANCE > percentage()){console.log("rememeber " + words[i]); //remember a word
if (!isRemembered(words[i])){
if (FORGET_CHANCE > percentage() && memory.memWords.length != 0){//forget a word, override it by remembering the new one
console.log("forgeting a word to remember " + words[i])
memory.memWords[Math.ceil(Math.random()*(memory.memWords.length-1))] = words[i];
}
else{//add a new word to memory
console.log("remember new word " + words[i])
memory.memWords[memory.memWords.length] = words[i];
}
}
}
}
}
//finds a commenter by looking at the @someone in the messege, if there is someone, put it in memoey
function findCommenter(){
for (var i = 0; i<words.length;i++){
if (words[i].charAt(0) == '@' && words[i].length >= 3 && words[i].substring(1) != opts.identity.username){
memory.previousCommenter = words[i].substring(1);
}
}
}
//checks if a word is stored in memory
function isRemembered(word){
word = word.toLowerCase();
for (var i = 0; i < memory.memWords.length; i++){
if (memory.memWords[i].toLowerCase() == word){
return true;
}
}
return false;
}
//this function gets an array of words and pantuation and it builds a string of text with currect spacing
function textBuilder(wordArr){
var text =""
//use memory to replace duplicates words, run 2 times!
for (var i =0; i<wordArr.length;i++){
if (i>0){
if (wordArr[i]==wordArr[i-1]){
wordArr[i] = genText(1,1);
}
}
}
for (var i =0; i<wordArr.length;i++){//2nd run, same thing, bad code
if (i>0){
if (wordArr[i]==wordArr[i-1]){
wordArr[i] = genText(1,1);
}
}
}
for (var i =0; i<wordArr.length;i++){
if (i==0 || wordArr[i] == "," || wordArr[i] == "."|| wordArr[i] == ":"|| wordArr[i] == ";"|| wordArr[i] == "!"|| wordArr[i] == "?"){ //things with no space before them
text+=wordArr[i];
}else{//things that can have space before them
text+=" ";
text+=wordArr[i];
}
}
return text;
}
//all the things that HAVE to be done after the the bots finishes
function postMsgHandler(msg)
{
memory.previousMsg=msg;
}
//splits the words of the messege and writes them to words[]
function splitIntoWords(msg){
words = []; //bye bye all of the words ):
//hello new words!
var i =0;
var tmpWord = "";
while (msg.charAt(i) != ""){ //runs as trough all of the content of msg
if (isSpace(msg.charAt(i)) ){
if (legalWord(tmpWord)){
words[words.length] = tmpWord
}
tmpWord = "";
}else{ //if char is not a space (space is not just space) char
tmpWord +=msg.charAt(i);
}
i++;
}
//last word
if (legalWord(tmpWord)){
words[words.length] = tmpWord
}
}
//generates text without using the memory or any other complicated stuff, like the old bot did
function genTextLegacy(minLength,maxLength){
var length = randomRange(minLength,maxLength);
var text = "";
for (var i =0;i<length;i++){
text+=words[randomRange(0,words.length-1)] //words from last messege
if (i<length-1){
text+=" "; //if it's not the last word, add space
}
}
return text
}
//generates a string of texts of a given length
function genText(minLength,maxLength){
var length = randomRange(minLength,maxLength);
var text = "";
for (var i =0;i<length;i++){
if (chance(45)){
text+=words[randomRange(0,words.length-1)] //words from last messege
}else if(chance(60)){
text+=memory.memWords[randomRange(0,memory.memWords.length-1)]; //words from long term memory
}else{
text+=shortWords[randomRange(0,shortWords.length-1)];
}
if (i<length-1){
text+=" "; //if it's not the last word, add space
}
}
return text;
}
//returns true if a word is legal for the huse of the bot
function legalWord(word){
word = word.toLowerCase();
if (word.length <= 2){ //means word is at least 3 characters
return false
}
if (word == "http" ||word == "www" ||word == "https"){
return false
}
return true
}
function splitSpaces(text){
var tmpWords = [];
var tmp="";
for (var i =0;i<text.length;i++){
if (text.charAt(i) != " "){
tmp+=text.charAt(i);
}else{
tmpWords[tmpWords.length]=tmp;
tmp = ""
}
}
tmpWords[tmpWords.length]=tmp;
return tmpWords;
}
//has has given precentage chance to return true
function chance(x){
var rnd = (Math.random()+0.009999) * 100;
return (x>rnd);
}
//returns a number from 1 to 100, integers only
function percentage(){
return Math.ceil((Math.random()*101))
}
function isSpace(chr){
if(chr == ' ' || chr == ',' || chr == '.' ||chr == ':' ||chr == '!' ||chr == '?' ||chr ==':' ||chr =='\"' || chr == '/' ||chr == '\\' ||chr == '(' ||chr == ')' ||chr == '{' ||chr == '}' ||chr == '[' ||chr == ']' ||chr == '-' ||chr == ';'){
return true
}
return false
}
//returns a number between min and max
function randomRange(a, b) {
if (a > b){
var tmp = a;
a = b;
b = tmp;
}
return Math.floor(Math.random() * (b - a + 1) + a);
}
//reset memmoey
function reset(){
memory.memWords = [];
memory.previousCommenter = DEFAULT_PERSON_NAME
memory.previousMsg = "";
speedMode = "normal"
}
//handling commands, maybe this human is not just a human????
function commands(msg,target){
switch(msg){
case (memory.previousMsg): client.say(target,memory.previousMsg); return true; //if 2 people wrote the same thing, than write it too, sounds like a human
case "!resetBot": reset();
case "!statusBot": client.say(target,"HUMAN: "+ opts.identity.username +" MEMORY SIZE: "+memory.memWords.length+" SPEED: "+speedMode); return true;
case "!fastBot": speedMode = "fast"; return true;
case "!normalBot": speedMode = "normal"; return true;
case "!slowBot": speedMode = "slow"; return true;
case "!memBot": var tmp= "MEMORY RAW CONTENT: "; for (var i =0;i<memory.memWords.length;i++){tmp+=(memory.memWords[i]+" ")}client.say(target,tmp); return true; //prints the raw memory of the bot
}
return false
}
// Called every time the bot connects to Twitch chat
function onConnectedHandler (addr, port) {
console.log(`* Connected to ${addr}:${port}`);
}