Raised This Month: $12 Target: $400
 3% 

[ANY] Chat Responder [UPDATED 2/27/16]


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author
headline
SourceMod Moderator
Join Date: Mar 2015
Plugin ID:
5039
Plugin Version:
1.1
Plugin Category:
General Purpose
Plugin Game:
Any
Plugin Dependencies:
    Servers with this Plugin:
    3 
    Plugin Description:
    A plugin that responds to strings of text
    Old 02-11-2016 , 21:46   [ANY] Chat Responder [UPDATED 2/27/16]
    Reply With Quote #1

    [ANY] Chat Responder

    ABOUT:
    This plugin takes triggers and responces from a config file and when a player types a phrase, it'll check to see if there are any triggers in it. If so, it'll print back. It can be configured to detect "exact" or "contains" phrases.

    If I type "Hey whats up!" and "Hey whats up!" is registered as an exact trigger, it'll cause a response.
    If I type "Banana Man yo" and "yo" is registered as a "contains" trigger, then it'll cause a responce.

    The config file looks like this
    Config File


    Where "Hi" will trigger the response and "type" will determine if it's supposed to be an exact or a contain trigger.

    CREDITS:
    Thanks to ThatOneGuy for introducing me to ADT Arrays when I was learning and also shout out to this thread for giving me the idea [link]

    BUGS:
    If there are any bugs please comment below, don't forget to check your error logs!

    SOURCE:
    PHP Code:
    /****************************************************************************
        [ANY] Chat Responder - A friend inside the code                            *
        Copyright (C) 2015  Michael Flaherty                                    *        

        This program is free software: you can redistribute it and/or modify    *
        it under the terms of the GNU General Public License as published by    *
        the Free Software Foundation, either version 3 of the License, or        *
        (at your option) any later version.                                        *

        This program is distributed in the hope that it will be useful,            *
        but WITHOUT ANY WARRANTY; without even the implied warranty of            *
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
        GNU General Public License for more details.                            *

        You should have received a copy of the GNU General Public License        *
        along with this program.  If not, see <http://www.gnu.org/licenses/>    *
    *****************************************************************************/
    #include <sourcemod>
    #include <autoexecconfig>
    #include <colorvariables>

    #pragma semicolon 1
    #pragma newdecls required
    #pragma dynamic 131072 //increase stack space to from 4 kB to 131072 cells (or 512KB, a cell is 4 bytes).

    #define TAG " \x04"
    #define PLUGIN_VERSION "1.3.4"
    #define MAX_TEXT_LENGTH 512

    /* ADT Arrays */
    Handle gh_adtExactTextsResponse;
    Handle gh_adtExactTextsTrigger;
    Handle gh_adtContainTextTrigger;
    Handle gh_adtContainTextResponse;

    /* Bools */
    bool g_bLateLoad;
    bool ga_bCooldown[MAXPLAYERS 1];

    /* Strings */
    char g_sPath[PLATFORM_MAX_PATH];

    /* ConVars */
    ConVar gcv_bPluginEnabled;
    ConVar gcv_sFilePath;
    ConVar gcv_bGlobalReply;
    ConVar gcv_fCoolDownTime;

    public 
    Plugin myinfo =
    {
        
    name "[ANY] Chat Responder",
        
    author "Headline",
        
    description "A simple chat bot",
        
    version PLUGIN_VERSION,
        
    url "http://www.michaelwflaherty.com"
    };

    public 
    APLRes AskPluginLoad2(Handle hMyselfbool bLatechar[] sErrorint err_max)
    {
        
    g_bLateLoad bLate;
        
        return 
    APLRes_Success;
    }

    public 
    void OnPluginStart()
    {
        
    AutoExecConfig_SetFile("hl_chatresponder");
        
    AutoExecConfig_CreateConVar("hl_chatresponder_version"PLUGIN_VERSION"Headline's Chat Responder : Version"FCVAR_NOTIFY|FCVAR_DONTRECORD);
        
        
    gcv_bPluginEnabled AutoExecConfig_CreateConVar("hl_chatresponder_enable""1""Enable the plugin?\n (1 = Yes, 0 = No)"FCVAR_NONEtrue0.0true1.0);    
        
        
    gcv_sFilePath AutoExecConfig_CreateConVar("hl_chatresponder_filepath""configs/hl_chatresponder.txt""File path to parse"FCVAR_NONE);
        
        
    gcv_bGlobalReply AutoExecConfig_CreateConVar("hl_chatresponder_global_reply""1""Make it so the Bot's responses are visible to everyone?\n (1 = Yes, 0 = No)"FCVAR_NONEtrue0.0true1.0);    

        
    gcv_fCoolDownTime AutoExecConfig_CreateConVar("hl_chatresponder_cooldown_time""10.0""Cooldown time for chat responses. (to prevent spam)"FCVAR_NONEtrue0.0true1.0);    

        
    AutoExecConfig_ExecuteFile();
        
    AutoExecConfig_CleanFile();
        
        if(
    g_bLateLoad)
        {
            
    OnConfigsExecuted();
        }
        
        
    AddCommandListener(OnSay"say"); 
        
    AddCommandListener(OnSay"say_team");
    }

    public 
    void OnConfigsExecuted()
    {
        if (
    gcv_bPluginEnabled.BoolValue)
        {
            
    InitializeADTArrays();
        }
    }

    public 
    void OnClientConnected(int client)
    {
        
    ga_bCooldown[client] = false;
    }

    public 
    void OnClientDisconnect(int client)
    {
        
    ga_bCooldown[client] = false;
    }

    public 
    Action OnSay(int client, const char[] commandint args
    {
        if (!
    gcv_bPluginEnabled.BoolValue)
        {
            return 
    Plugin_Continue;
        }
        if (!
    IsValidClient(client))
        {
            return 
    Plugin_Continue;
        }
        else
        {
            
    char sText[4096]; 
            
    GetCmdArgString(sTextsizeof(sText));
            
            
    StripQuotes(sText);
            
            
    Handle hData CreateDataPack();
            
    WritePackCell(hDataclient);
            
    WritePackString(hDatasText);
            
    ResetPack(hData);
            
            
    CreateTimer(0.3Timer_ReplyhDataTIMER_FLAG_NO_MAPCHANGE);
        }
        return 
    Plugin_Continue;
    }
        
    public 
    Action Timer_Reply(Handle hTimerHandle hData)
    {
        
    char sText[4096]; 

        
    int client ReadPackCell(hData);
        
    ReadPackString(hDatasTextsizeof(sText));
        
    CloseHandle(hData);
        if (
    IsValidClient(client))
        {
            
    char sTrigger[128];
            
    char sResponse[MAX_TEXT_LENGTH];

            
    bool bFoundResponse false;
            
    int iArrayIndex FindStringInArray(gh_adtExactTextsTriggersText);
            if (
    iArrayIndex != -1)
            {
                
    GetArrayString(gh_adtExactTextsResponseiArrayIndexsResponsesizeof(sResponse));

                if (
    StrContains(sResponse"{NAME}"))
                {
                    
    char sName[MAX_NAME_LENGTH];
                    
    GetClientName(clientsNamesizeof(sName));
                    
    ReplaceString(sResponsesizeof(sResponse), "{NAME}"sName);
                }
                
                
    int count 0;
                for (
    int j 0sResponse[j]; j++) {
                    if (
    sResponse[j] == '\n')
                        
    count++;
                }

                
    char[][] lines = new char[count+1][MAX_TEXT_LENGTH];
                
    ExplodeString(sResponse"\n"linescount+1MAX_TEXT_LENGTH);

                if (!
    ga_bCooldown[client])
                {
                    for (
    int j 0count+1j++)
                    {
                        if (
    gcv_bGlobalReply.BoolValue)
                        {
                            
    CPrintToChatAll("%s%s"TAGlines[j]);
                        }
                        else
                        {
                            
    CPrintToChat(client"%s%s"TAGlines[j]);
                        }
                    }
                }
                else
                {
                    
    PrintToChat(client"%sDo not spam the chat responder!"TAG);
                }
                
    bFoundResponse true;
            }
            else
            {
                
    bFoundResponse false;
            }
            
            if (!
    bFoundResponse)
            {
                for (
    int i 0GetArraySize(gh_adtContainTextTrigger); i++)
                {
                    
    GetArrayString(gh_adtContainTextTriggerisTriggersizeof(sTrigger));
                    if (
    StrContains(sTextsTriggerfalse) != -1)
                    {
                        
    GetArrayString(gh_adtContainTextResponseisResponsesizeof(sResponse));
                        
                        
    int count 0;
                        for (
    int j 0sResponse[j]; j++) {
                            if (
    sResponse[j] == '\n')
                                
    count++;
                        }

                        
    char[][] lines = new char[count+1][MAX_TEXT_LENGTH];
                        
    ExplodeString(sResponse"\n"linescount+1MAX_TEXT_LENGTH);

                        if (!
    ga_bCooldown[client])
                        {
                            for (
    int j 0count+1j++)
                            {
                                if (
    gcv_bGlobalReply.BoolValue)
                                {
                                    
    CPrintToChatAll("%s%s"TAGlines[j]);
                                }
                                else
                                {
                                    
    CPrintToChat(client"%s%s"TAGlines[j]);
                                }
                            }
                        }
                        else
                        {
                            
    PrintToChat(client"%sDo not spam the chat responder!"TAG);
                        }
                            
                        
    bFoundResponse true;
                        break;
                    }
                }
            }
            if (
    bFoundResponse && gcv_fCoolDownTime.FloatValue 0.1)
            {
                
    CreateTimer(gcv_fCoolDownTime.FloatValueTimer_CooldownGetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
                
    ga_bCooldown[client] = true;
            }
        }
    }

    public 
    Action Timer_Cooldown(Handle hTimerint iUserID)
    {
        
    int client GetClientOfUserId(iUserID);
        if (
    IsValidClient(client))
        {
            if (
    ga_bCooldown[client])
            {
                
    ga_bCooldown[client] = false;
            }
        }
    }

    public 
    void ParseKvFile()
    {
        
    char sPath[PLATFORM_MAX_PATH];
        
    gcv_sFilePath.GetString(sPathsizeof(sPath));
        
    BuildPath(Path_SMg_sPathsizeof(g_sPath), sPath);
        
    Handle hKeyValues CreateKeyValues("Chat Responder");

        if(!
    FileExists(g_sPath))
        {
            
    SetFailState("Configuration file %s not found!"g_sPath);
            return;
        }

        if(!
    FileToKeyValues(hKeyValuesg_sPath))
        {
            
    SetFailState("Improper structure for configuration file %s!"g_sPath);
            return;
        }

        if(
    KvGotoFirstSubKey(hKeyValues))
        {
            do
            {
                
    char sSectionName[128];
                
    char sType[128];
                
    char sText[MAX_TEXT_LENGTH];

                
    KvGetSectionName(hKeyValuessSectionNamesizeof(sSectionName));    //get name of section
                
                
    KvGetString(hKeyValues"response"sTextsizeof(sText));
                
    KvGetString(hKeyValues"type"sTypesizeof(sType));    //call key-value by name of key and store value in variable, with default as "blah" if it doesnt exist

                
    ReplaceString(sTextsizeof(sText), "{new_line}""\n");
                if (
    StrEqual(sType"e"false))
                {
                    
    PushArrayString(gh_adtExactTextsTriggersSectionName);
                    
    PushArrayString(gh_adtExactTextsResponsesText);
                }
                else if (
    StrEqual(sType"c"false))
                {
                    
    PushArrayString(gh_adtContainTextTriggersSectionName);
                    
    PushArrayString(gh_adtContainTextResponsesText);
                }
            }
            while(
    KvGotoNextKey(hKeyValuesfalse));
            
    KvGoBack(hKeyValues);
        }
        else
        {
            
    SetFailState("Can't find first subkey in configuration file %s!"g_sPath);
            
    CloseHandle(hKeyValues);
            return;
        }
        
    CloseHandle(hKeyValues);
    }

    stock void InitializeADTArrays()
    {
        
    gh_adtExactTextsTrigger CreateArray(64);
        
    gh_adtExactTextsResponse CreateArray(MAX_TEXT_LENGTH);
        
    ClearArray(gh_adtExactTextsTrigger);
        
    ClearArray(gh_adtExactTextsResponse);

        
    gh_adtContainTextTrigger CreateArray(64);
        
    gh_adtContainTextResponse CreateArray(MAX_TEXT_LENGTH);
        
    ClearArray(gh_adtContainTextTrigger);
        
    ClearArray(gh_adtContainTextResponse);

        
    ParseKvFile();
    }

    stock bool IsValidClient(int clientbool bAllowBots falsebool bAllowDead true)
    {
        if(!(
    <= client <= MaxClients) || !IsClientInGame(client) || (IsFakeClient(client) && !bAllowBots) || IsClientSourceTV(client) || IsClientReplay(client) || (!bAllowDead && !IsPlayerAlive(client)))
        {
            return 
    false;
        }
        return 
    true;
    }

    /*
        Change log
        1.0
            * Initial release
        1.1
            * Fixed some dumb mistakes
            * Added a cooldown timer to prevent spam
            * Added a few IsValidClient checks to make sure we don't get errors in error logs
            * Implemented some of ThatOneGuy's suggestions
        1.3.3
            * Fixed {new_line}
        1.3.4
            * Fixed issue where client's cooldowns would not be reset
    */ 
    Attached Files
    File Type: zip hl_chatresponder-1.0.zip (15.6 KB, 354 views)
    File Type: zip hl_chatresponder-1.1.zip (16.5 KB, 386 views)
    File Type: zip hl_chatresponder-1.2.zip (16.9 KB, 176 views)
    File Type: zip hl_chatresponder-1.3.2.zip (35.0 KB, 328 views)
    File Type: zip hl_chatresponder-1.3.3.zip (35.5 KB, 443 views)
    File Type: zip hl_chatresponder-1.3.4.zip (34.6 KB, 961 views)

    Last edited by headline; 03-03-2020 at 23:11.
    headline is offline
     



    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off

    Forum Jump


    All times are GMT -4. The time now is 14:44.


    Powered by vBulletin®
    Copyright ©2000 - 2024, vBulletin Solutions, Inc.
    Theme made by Freecode