summaryrefslogtreecommitdiff
path: root/npc/commands/discord.txt
blob: 21e675a45161534657f2dc13dcef878a7d8c29ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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 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("Usename#0000"), 2;
                input .@discord$;
                if (.@discord$ == "") close;
                mes "";

                // 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$);

                // Prevent changing for the next 3 days
                DISCTRL=gettimeparam(GETTIME_DAYOFMONTH)+3;
                mesc l("Linking requested."), 1;
                mesc l("This setting can only be changed every %d days.", 3), 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)));
                break;
        }

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

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