Closed ghost closed 11 years ago
You can look at my bot and see that I use an array of text options. Then get a random number and speak that index number in the array. I have several array examples in my bot.js
config file
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var phrases = ['a', 'b', 'c', 'd', 'e', 'f'];
var randomPhrase = phrases[getRandomInt(0, phrases.length - 1)];
console.log(randomPhrase);
I want my bot to say a random phrase in the chat box whenever someone mentions his name.
Yep, so you'll need to regex the data.text onSpeak to look for the bot's name. Then have an array of options that you want for the responses and do what both @alaingilbert and I have posted for speaking that random response.
ok, thanks
Another example... magic eight ball
global.randomItem = function (list) {
return list[Math.floor(Math.random() * list.length)];
};
var botuserid = "123456789012345678901234";
//magic 8ball array
var eightBallList = [
"It is certain", "It is decidedly so", "Without a doubt", "Yes – definitely",
"You may rely on it", "As I see it, yes", "Most likely", "Outlook good",
"Yes", "Signs point to yes", "Reply hazy, try again", "Ask again later",
"Better not tell you now", "Cannot predict now", "Concentrate and ask again",
"Don't count on it", "My reply is no", "My sources say no", "Outlook not so good",
"Very doubtful" ];
bot.on('speak', function(data) {
// "8ball" command
if( data.text.match(/8ball/i) && (data.userid != botuserid)) {
bot.speak(":8ball: Says: " + randomItem(eightBallList));
}
});
^ I put in the code into my bot and It spammed all of the 8ball phrases into the TT chat multiple times instead of choosing a random phrase from the list and Cloud 9 said that randomItem was a undeclared variable.
@Turntablelover you have to fix up this line of code for your bot:
if( data.text.match(/8ball/i) && (data.userid != botuserid)) {
The part on the right makes sure your bot doesn't respond to itself :) and spam the chat.
Declare this variable in your code and set it equal to your bot's id (or replace it with one you have already)
var botuserid = "123456789012345678901234";
ok, thanks for the help
How do you make a bot say random phrases in the chat without making every phrase a separate / command.