From 9e9e3e9555693f8eb5da999e05bb792de602f296 Mon Sep 17 00:00:00 2001 From: Jesusaves Date: Sat, 15 May 2021 22:16:22 -0300 Subject: Refactor @discord command. --- db/constants.conf | 1 + npc/commands/discord.txt | 92 ++++++++++++++++++++++++++++++++++++++++++------ sql-files/main.sql | 4 ++- 3 files changed, 86 insertions(+), 11 deletions(-) diff --git a/db/constants.conf b/db/constants.conf index c5b6f099a..b103b5f5e 100644 --- a/db/constants.conf +++ b/db/constants.conf @@ -1519,6 +1519,7 @@ constants_db: { /** evol constants **/ comment__: "API codes" + API_DISCORD: 301 API_PINCODE: 302 API_SENDMAIL: 501 API_FLUSHVAULT: 607 diff --git a/npc/commands/discord.txt b/npc/commands/discord.txt index 2f7feb494..0f4f7e8c7 100644 --- a/npc/commands/discord.txt +++ b/npc/commands/discord.txt @@ -16,38 +16,109 @@ OnCall: close; } - if (!validatepin()) - 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; - .@nb = query_sql("select `discord_name` from `discord` WHERE `account_id`='"+getcharid(3)+"'", .@discord$); + .@link=true; + + // Search on cache + .@key$=str(getcharid(3)); + .@m$ = htget(.discmem$, .@key$, "Not found"); + + if (.@m$ == "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$); + } + // Doublecheck it worked properly + if (.@discord$ == "") { + .@discord$="Not Linked"; + .@link=false; + } + do { mesn "Lawn Cable"; mesq l("Current linked Discord account: @@", .@discord$); next; select - l("Change Linked Discord Account"), - l("Disconnect"), + 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$; - .@discord$=escape_sql(.@discord$); + if (.@discord$ == "") close; mes ""; - query_sql("UPDATE `discord` SET `discord_name` = '"+.@discord$+"' WHERE `account_id`='"+getcharid(3)+"'"); - query_sql("UPDATE `discord` SET `verified` = '0' WHERE `account_id`='"+getcharid(3)+"'"); - query_sql("UPDATE `discord` SET `discord_id` = '' WHERE `account_id`='"+getcharid(3)+"'"); - mesc l("You must send ##B/verify##b to Discord bot for the linking be complete."), 1; + + // Run SQL query + 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; } @@ -56,6 +127,7 @@ OnCall: OnInit: bindatcmd "discord", "@discord::OnCall", 0, 0, 1; + .discmem$ = htnew; end; } diff --git a/sql-files/main.sql b/sql-files/main.sql index e2280d225..3aebdaba1 100644 --- a/sql-files/main.sql +++ b/sql-files/main.sql @@ -311,7 +311,9 @@ CREATE TABLE IF NOT EXISTS `discord` ( `account_id` INT(11) UNSIGNED NOT NULL DEFAULT '0', `discord_id` VARCHAR(255) NOT NULL DEFAULT '', `discord_name` VARCHAR(255) NOT NULL DEFAULT '', - `verified` ENUM('0', '1') NOT NULL DEFAULT '0' + `verified` ENUM('0', '1') NOT NULL DEFAULT '0', + PRIMARY KEY (`discord_name`), + UNIQUE KEY `account_id` (`account_id`) ) ENGINE=MyISAM; -- -- cgit v1.2.3-60-g2f50