[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


Messages In This Thread
How to make an auto-reply chat bot - by DavidBilla - 15.11.2014, 18:14
Re: How to make an auto-reply chat bot - by Glossy42O - 15.11.2014, 18:17
Re: How to make an auto-reply chat bot - by sammp - 15.11.2014, 18:18
Re: How to make an auto-reply chat bot - by DavidBilla - 15.11.2014, 18:25
Re: How to make an auto-reply chat bot - by sammp - 15.11.2014, 19:10
Re: How to make an auto-reply chat bot - by Pravin - 16.11.2014, 02:57
Re: How to make an auto-reply chat bot - by Emmet_ - 16.11.2014, 03:10
Re: How to make an auto-reply chat bot - by DavidBilla - 16.11.2014, 07:22
Re: How to make an auto-reply chat bot - by Kyance - 16.11.2014, 14:22
Re: How to make an auto-reply chat bot - by DavidBilla - 16.11.2014, 14:32

Forum Jump:


Users browsing this thread: 3 Guest(s)