[Tutorial] How to make an auto-reply chat bot
#1

Introduction

Hello,this is my 2nd tutorial. In this tutorial I'm going to teach you how to make an auto-reply chat bot. We are not going to add any NPCs, but we'll be detecting if a player says the 'imaginary' bot's name and send a reply to them like the bot has sent.

Eg: Let's imagine that the bot's name is Bruce_Lee.
If a player says in the chat, "Hello Bruce_Lee" or anything that has the word Bruce_Lee, they will see a message like
Bruce_Lee[-1]id you just say my name?
The messages are gonna be random.

Restricting the bot's name:

First we are going to let the players know that there is a bot in our server.All of you must be familiar with SendClientMessage, so I'm just gonna add this part under Onplayerconnect

And also we'll restrict players from using the name Bruce_Lee so that they cannot troll other players in chat.

pawn Код:
public OnPlayerConnect(playerid)
{
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid,name,sizeof(name));
    if(!strcmp(name,"Bruce_Lee",true))
    {
        SendClientMessage(playerid,0xFF0000FF,"This username is restricted,please change it");
        SetTimerEx("KickTimer", 1000, false, "i", playerid);
    }
    SendClientMessage(playerid,0xFF0000FF,"This server is haunted with Bruce Lee's spirit");
    SendClientMessage(playerid,0xFF0000FF,"Mention his name in chat and he'll talk to you");
    return 1;
}
We are getting the player's name and using strcmp to see if it matches the bot's name,we send them a message asking them to change it,and we start a timer

pawn Код:
forward KickTimer(playerid);
public KickTimer(playerid)
{
    Kick(playerid);
    return 1;
}
In the function of the timer, we kick the player. We are following this method so that the player will be able to see the message before getting kicked

Making the random message array:

We are first going to create the list of messages that the players are going to receive from Bruce_Lee.

pawn Код:
new BotMessages[][]=
{
    "Did you just say my name?",
    "You are gonna see what real fist of fury is",
    "Let's play the game of death"
};
Now the "BotMessage" is my array name.We have two [][] here, one is for the size of the string and the other is for the number of strings.what comes inside the { } is my string which we will display to the player later.

Detecting the word Bruce_Lee :

We will detect it when the player either says Bruce Lee or Bruce_Lee.

We detect the words under OnPlayerText
pawn Код:
public OnPlayerText(playerid, text[])
{
    if(strfind(text, "Bruce Lee", true) != -1 || strfind(text, "Bruce_Lee", true) != -1)
    {
        new LeeString[128];
        new RandomText=sizeof(BotMessages);
        format(LeeString,sizeof(LeeString),"Bruce_Lee[-1]:%s",BotMessages[random(RandomText)]);
        SendClientMessageToAll(-1,LeeString);
    }
    return 1;
}
Here we use strfind to check if the text contains Bruce_Lee or Bruce Lee,if it does , we declare a new string, then we declare a variable called RandomText and store it with the value of amount of BotMessages we have, then we finally format the string to send a message to all players, like the Bruce_Lee bot has sent it, with a random message.


Credits:
Kalcor for samp
Pravin for teaching me what scripting is....

Note:
Since i wrote the whole code from mobile, i could've done some minor/major mistake, please point it out so that i can correct it.
Reply
#2

Awesome !!!!
Reply
#3

pawn Код:
if(strfind(text,"Bruce",true) != -1) return 1;    else    {        if(strfind(text,"Lee",true) != -1) return 1;        else return 0;    }

Could instead be

pawn Код:
if(strfind(text, "Bruce", true) != -1 || strfind(text, "Lee", true) != -1) return 1;
What if there is someone called Bruce or Lee in the server?

You should just look for the full word instead of sections of the name.
Reply
#4

^^ Sammp
Thanks changed.
In that case, we could restrict player's from using that username. Because most players will be lazy to type Bruce_Lee and instead type Bruce or Lee.
Reply
#5

Also, using a seperate function for checking one name is a waste of memory - unless you start including other names, it's pretty useless for one name.

You should honestly just make an strfind for Bruce Lee together and Bruce_Lee - then again what if people are just making a reference to Bruce Lee? Consider including a command to turn these messages on or off.
Reply
#6

Very good.
Helpful for beginners.
Reply
#7

"bruce26: Hey, leetman247, how are you doing?"
"leetman247: Good, you?"

Both messages will trigger the auto-reply from "Bruce_Lee" so it's best to check for the full name of the bot instead.
Reply
#8

Added what Emmet said

+Restricted players from using the bot's name
+Removed the stock
Reply
#9

Now where have I seen this before...?

Haha, nice! Brings back memories of BF :')
You have given out too much Reputation in the last 24 hours, try again later.
;_;
Reply
#10

Quote:
Originally Posted by Kyance
Посмотреть сообщение
Now where have I seen this before...?

Haha, nice! Brings back memories of BF :')
You have given out too much Reputation in the last 24 hours, try again later.
;_;
Missed BF and Chuck_Norris a lot, that's why thought of doing a tutorial on it.
Reply


Forum Jump:


Users browsing this thread: 6 Guest(s)