summaryrefslogblamecommitdiff
path: root/npc/commands/discord.txt
blob: 0aa56660ee6c042fdb2be3d343fbe4042fdfe96e (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

















                                                                                           




                                                                     
 
















                                                                                                          
               



                             
                                                      
 
                                    


                                                                                                                           
                                                            



                                    
                                                       

     





                                                                 

                                                                                                



                        


                                                                
                                                                                      
                                           
                                 
                                            


                                                                                                 
                       




                                                                 
 
                                                              














                                                                                                                                                    
                                                               
                                                                                             

                                                       
                                                           
                                                
                                                                                
                      
                                                                                 
                   
                                                                                         
                              


                                                           
                                                       

                                                                                                          
                                                                           







                                                     
                     


        
// TMW-2 Script.
// Author:
//    Jesusalva
//    LawnCable
// Notes:
//    Controls `discord` table with @discord command.
//    Only useful for TMW2-Discord integration.

-	script	@discord	32767,{
    end;

OnCall:
    // Anti-Flood System
    if (@discord) {
        mesc l("You already ran this command today. Please try again at a later time."), 1;
        close;
    }

    // Live server only
    if (debug || $@GM_OVERRIDE) {
        dispbottom l("This command cannot be used on test servers.");
        end;
    }

    // Bot cannot (or should not) alter staff data
    if (is_staff()) {
        dispbottom l("Staff is not allowed to use this command.");
        end;
    }

    // Minimum account requeriments
    if (#REG_DATE < (gettimetick(2)+259200) && BaseLevel < 15) {
        dispbottom l("Your account must be at least 72 hours old or have level 15+ to use this command.");
        end;
    }

    // Use this instead of min acc req if desired
    //if (!validatepin())
    //    close;

    // Prevent reusing the command on same session
    @discord=1;
    .@link=true;

    // Search on cache
    .@key$=str(getcharid(3));
    .@discord$ = htget(.discmem, .@key$, "Not found");

    if (.@discord$ == "Not found") {
        // Only do SQL query if not in cache
        .@nb = query_sql("select `discord_name` from `discord` WHERE `account_id`='"+getcharid(3)+"' limit 1", .@discord$);
        // Override default behavior
        if (.@discord$ == "" || .@discord$ == "Not found") {
            .@discord$="Not Linked";
            .@link=false;
        }
        // Add to Cache
        htput(.discmem, str(getcharid(3)), .@discord$);
    }

    do
    {
        mesn "Lawn Cable";
        mesq l("Current linked Discord account: @@", .@discord$);
        next;
        select
            rif(DISCTRL < gettimeparam(GETTIME_DAYOFMONTH), l("Change Linked Discord Account")),
            rif(.@link, l("Disconnect")),
            l("Quit");

        switch (@menu) {
            case 1:
                if (DISCTRL >= gettimeparam(GETTIME_DAYOFMONTH))
                    atcommand("@ban 7d "+strcharinfo(0));

                mesc l("Please insert your Discord ID, on the following format: "), 1;
                mesc l("Username#0000"), 2;
                input .@discord$;
                if (.@discord$ == "") close;
                .@i = explode(.@d$, .@discord$, "#");
                if (.@i != 2) { mesc l("Invalid Discord ID. (L1)"); next; break; }
                if (getstrlen(.@d$[1]) != 4) { mesc l("Invalid Discord ID. (L2)"); next; break; }
                mes "";
                clear;
                mesc l("Linking the following Discord account:");
                mesc .@discord$, 1;
                mesc l("Is this correct?");
                if (askyesno() == ASK_NO) close;

                // Run SQL query (will halt execution on dupe)
                if (.@link) {
                    query_sql(sprintf("UPDATE `discord` SET `discord_name` = '%s', `verified` = '0', `discord_id` = '' WHERE `account_id`='%d'",
                                      escape_sql(.@discord$), getcharid(3)));
                } else {
                    query_sql(sprintf("INSERT INTO `discord` (`discord_name`, `verified`, `discord_id`, `account_id`) VALUES ('%s', '0', '', '%d')",
                                      escape_sql(.@discord$), getcharid(3)));
                }

                // Encode JSON data
                .@p$=json_encode("name", strcharinfo(0),
                                 "accId", getcharid(3),
                                 "disc", .@discord$);

                // Send to API and update cache
                api_send(API_DISCORD, .@p$);
                htput(.discmem, str(getcharid(3)), .@discord$);
                consoleinfo("%s linked discord account \"%s\".", strcharinfo(0), .@discord$);

                // Prevent changing for the next 3 days
                DISCTRL=gettimeparam(GETTIME_DAYOFMONTH)+2;
                mesc l("Linking requested."), 1;
                mesc l("This setting can only be changed every %d days.", 2), 1;
                break;
            /////////////////////////////////////////////////////////////////////
            case 2:
                query_sql("DELETE FROM `discord` WHERE `account_id`='"+getcharid(3)+"'");
                .@discord$="";
                // Prevent setting a new linking right away
                DISCTRL=gettimeparam(GETTIME_DAYOFMONTH)+1;
                // Update Cache
                htput(.discmem, str(getcharid(3)), "");
                // TODO: Remove Adventurer role?
                logmes(sprintf("User %d \"%s\" unlinked Discord account!", getcharid(3), strcharinfo(0)));
                consoleinfo("%s removed Discord account.", strcharinfo(0));
                break;
        }

    } while (@menu != 3);
    close;

OnInit:
    bindatcmd "discord", "@discord::OnCall", 0, 0, 1;
    .discmem = htnew;
    end;
}