summaryrefslogtreecommitdiff
path: root/npc/commands/discord.txt
blob: a5773af77d7503f8425954a4d9790ca1f9caf3a2 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// 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() && !is_admin()) {
        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;
                if (strtolower(.@discord$) == "username#0000") { break; }
                .@i = explode(.@d$, .@discord$, "#");
                if (.@i != 2) { mesc l("Invalid Discord ID."); next; break; }
                if (getstrlen(.@d$[1]) != 4) { mesc l("Invalid Discord ID."); 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) {
                    if ($@HAS_API) {
                        apiasync("SQL", sprintf("UPDATE `discord` SET `discord_name` = '?1', `verified` = '0', `discord_id` = '?2' WHERE `account_id`='%d'", getcharid(3)));
                        apiasync("SAD1", .@discord$);
                        apiasync("DISCORDID2", .@discord$);
                        apiasync("SQLRUN", "");
                    } else {
                        query_sql(sprintf("UPDATE `discord` SET `discord_name` = '%s', `verified` = '0', `discord_id` = '' WHERE `account_id`='%d'",
                                      escape_sql(.@discord$), getcharid(3)));
                    }
                } else {
                    if ($@HAS_API) {
                        apiasync("SQL", sprintf("INSERT INTO `discord` (`discord_name`, `verified`, `discord_id`, `account_id`) VALUES ('?1', '0', '?2', '%d')", getcharid(3)));
                        apiasync("SAD1", .@discord$);
                        apiasync("DISCORDID2", .@discord$);
                        apiasync("SQLRUN", "");
                    } 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", escape_sql(.@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)+1;
                mesc l("Linking requested."), 1;
                mesc l("This setting can only be changed every %d days.", 1), 1;
                break;
            /////////////////////////////////////////////////////////////////////
            case 2:
                if ($@HAS_API) {
                        apiasync("SQL", sprintf("DELETE FROM `discord` WHERE `account_id`='%d'", getcharid(3)));
                        apiasync("SQLRUN", "");
                } else {
                    query_sql("DELETE FROM `discord` WHERE `account_id`='"+getcharid(3)+"'");
                    // Prevent setting a new linking right away
                    DISCTRL=gettimeparam(GETTIME_DAYOFMONTH)+1;
                }
                .@discord$="";
                // 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;
}