Skip to content

Commit 41a7315

Browse files
authored
Update Admin tool - Verify.js
trying out qwen 3.6
1 parent d4b8f37 commit 41a7315

1 file changed

Lines changed: 83 additions & 49 deletions

File tree

Parsers/Admin tool - Verify.js

Lines changed: 83 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
activation_example:!verify-admin @Astrid, !verify-admin -help
2+
activation_example:!verify-admin @Astrid, !unverify-admin @Astrid, !verify-admin -help
33
regex:^!(?:un)?verify-admin\u0020?
44
flags:gi
55
*/
@@ -16,73 +16,107 @@ if (
1616
var verificationStatus = null;
1717
var slacker = new x_snc_slackerbot.Slacker();
1818

19+
// Determine the base command to set default verification status
20+
// If it starts with !unverify, default is false. Otherwise (verify), default is true.
21+
var isUnverifyCommand = current.text.toLowerCase().indexOf("!unverify-admin") === 0;
22+
var defaultVerificationStatus = isUnverifyCommand ? false : true;
23+
1924
// Grab user ID and then prep invocation if User-visible info provided
2025
var invocation = current.text
2126
.replace(/^!(?:un)?verify-admin\u0020?/gi, "")
2227
.trim();
2328
var paramArr = invocation.split(" ");
2429

25-
if (paramArr.length == 0) {
30+
// 1. Handle -help flag
31+
if (paramArr.length == 1 && paramArr[0] == "-help") {
2632
messageBody =
27-
"!verify-admin must be called with a user tag, followed by an optional parameter and optional description. For example: `!verify-admin @Astrid -unv Is an Impasta`\n\nThe full list of accepted triggers can be found by sending `!verify-admin -help`";
33+
"*Admin Tool - Verify User*\n" +
34+
"A parser for setting user verification and descriptions. \n" +
35+
"*Note:* This parser can only be triggered from admin channels.\n\n" +
36+
"*Usage:*\n" +
37+
"`!verify-admin @username [optional description]`\n" +
38+
"`!unverify-admin @username [optional description]`\n\n" +
39+
"*Examples:*\n" +
40+
"`!verify-admin @Astrid Is an Impasta`\n" +
41+
"`!unverify-admin @Astrid Is an Impasta`\n" +
42+
"`!verify-admin @Astrid Some Role, Some Company`\n" +
43+
"`!verify-admin @Astrid -v`\n\n" +
44+
"*Supported flags:*\n" +
45+
"`-v`: Verify the user (default for !verify-admin)\n" +
46+
"`-unv`: Unverify the user (default for !unverify-admin)\n" +
47+
"`-help`: Show this message";
48+
49+
slacker.send_chat(current, messageBody, true);
50+
return; // CRITICAL: Stop execution here
2851
}
2952

30-
if (paramArr.length == 1 && paramArr[0] == "-help") {
53+
// 2. Validate that a user tag is provided
54+
if (paramArr.length == 0 || !/^<@.*?>$/.test(paramArr[0])) {
3155
messageBody =
32-
"Admin Tool - Verify user\nA parser for setting user verification and descriptions. *Note:* This parser can only be triggered from admin channels specified.\n\nUsage: `!verify-admin @username [-v|-unv] [message]`\nExamples:\n`!verify-admin @Astrid -unv Is an Impasta`\n`!verify-admin @Astrid Some Role, Some Company`\n`!verify-admin @Astrid -v`\n\nSupported flags:\n-v: Verify the user\n-unv: Remove verification from user\n-help: Show this message";
56+
"!verify-admin must be called with a user tag, followed by an optional parameter and optional description. For example: `!verify-admin @Astrid -unv Is an Impasta`\n\nThe full list of accepted triggers can be found by sending `!verify-admin -help`";
57+
slacker.send_chat(current, messageBody, true);
58+
return; // Stop execution
3359
}
3460

35-
if (/^<@.*?>$/.test(paramArr[0])) {
36-
messageBody = paramArr[0];
37-
userId = paramArr[0].replace(/[<>@]/g, "");
38-
paramArr.shift();
39-
} else {
40-
messageBody =
41-
"!verify-admin must be called with a user tag, followed by an optional parameter and optional description. For example: `!verify-admin @Astrid -unv Is an Impasta`\n\nThe full list of accepted triggers can be found by sending `!verify-admin -help`";
42-
slacker.send_chat(current, messageBody, true);
43-
}
44-
45-
if (paramArr.indexOf("-v") > -1 && paramArr.indexOf("-unv") > -1) {
61+
// 3. Extract User ID
62+
userId = paramArr[0].replace(/[<>@]/g, "");
63+
paramArr.shift(); // Remove the user tag from the array
64+
65+
// 4. Validate Flags
66+
var hasV = paramArr.indexOf("-v") > -1;
67+
var hasUnv = paramArr.indexOf("-unv") > -1;
68+
69+
if (hasV && hasUnv) {
4670
messageBody =
4771
"Please only provide one verify parameter in your message. Verification message ignored.";
4872
slacker.send_chat(current, messageBody, true);
49-
} else {
50-
if (/^!unverify/.test(current.text)) {
51-
verificationStatus = false;
52-
} else if (paramArr.indexOf("-v") > -1) {
53-
verificationStatus = true;
54-
} else if (paramArr.indexOf("-unv") > -1) {
55-
verificationStatus = false;
56-
} else if (verificationStatus == null) {
57-
verificationStatus = true;
58-
}
73+
return; // Stop execution
74+
}
5975

60-
description = paramArr.join(" ");
61-
var grUser = new GlideRecord("x_snc_slackerbot_user");
62-
if (grUser.get("user_id", userId)) {
63-
verificationStatus != null
64-
? grUser.setValue("verified", verificationStatus)
65-
: grUser.getValue("verified");
66-
if (description.length > 0) {
67-
grUser.setValue("user_info", description);
68-
} else {
69-
description = grUser.getValue("user_info");
70-
}
76+
// 5. Determine Verification Status
77+
// Flags override the default based on the command name
78+
if (hasUnv) {
79+
verificationStatus = false;
80+
} else if (hasV) {
81+
verificationStatus = true;
82+
} else {
83+
// No flag provided, use the default based on command name
84+
verificationStatus = defaultVerificationStatus;
85+
}
7186

72-
grUser.update();
87+
// 6. Extract Description
88+
// Filter out the flags from the description
89+
var descriptionArr = paramArr.filter(function(item) {
90+
return item !== "-v" && item !== "-unv";
91+
});
92+
description = descriptionArr.join(" ");
7393

74-
messageBody =
75-
"Updated <@" +
76-
userId +
77-
">'s verification information with the following info:\n";
78-
messageBody +=
79-
"Verification status: " +
80-
(verificationStatus ? "*Verified*" : "*Unverified*") +
81-
"\n";
82-
messageBody += "User information:\n>" + description;
94+
// 7. Update User Record
95+
var grUser = new GlideRecord("x_snc_slackerbot_user");
96+
if (grUser.get("user_id", userId)) {
97+
grUser.setValue("verified", verificationStatus);
98+
99+
if (description.length > 0) {
100+
grUser.setValue("user_info", description);
83101
} else {
84-
messageBody += `I'm afraid I can't do that as the ~limit~ user (<@${userId}>) does not exist.`;
102+
// If no new description, keep the old one
103+
description = grUser.getValue("user_info");
85104
}
86-
slacker.send_chat(current, messageBody, true);
105+
106+
grUser.update();
107+
108+
messageBody =
109+
"Updated <@" +
110+
userId +
111+
">'s verification information:\n";
112+
messageBody +=
113+
"Verification status: " +
114+
(verificationStatus ? "*Verified*" : "*Unverified*") +
115+
"\n";
116+
messageBody += "User information:\n>" + description;
117+
} else {
118+
messageBody = "I'm afraid I can't do that as the user (<@" + userId + ">) does not exist.";
87119
}
120+
121+
slacker.send_chat(current, messageBody, true);
88122
}

0 commit comments

Comments
 (0)