-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathAdmin tool - Verify.js
More file actions
123 lines (107 loc) · 4.19 KB
/
Admin tool - Verify.js
File metadata and controls
123 lines (107 loc) · 4.19 KB
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
/*
activation_example:!verify-admin @Astrid, !unverify-admin @Astrid, !verify-admin -help
regex:^!(?:un)?verify-admin\u0020?
flags:gi
*/
(function(){
if (
current.channel == "GD51HTR46" ||
current.channel == "G9LAJG7G8" ||
current.channel == "G7M4AP6U8"
) {
//admin channels on sndevs
var messageBody = "";
var userId = "";
var description = "";
var verificationStatus = null;
var slacker = new x_snc_slackerbot.Slacker();
// Determine the base command to set default verification status
// If it starts with !unverify, default is false. Otherwise (verify), default is true.
var isUnverifyCommand = current.text.toLowerCase().indexOf("!unverify-admin") === 0;
var defaultVerificationStatus = isUnverifyCommand ? false : true;
// Grab user ID and then prep invocation if User-visible info provided
var invocation = current.text
.replace(/^!(?:un)?verify-admin\u0020?/gi, "")
.trim();
var paramArr = invocation.split(" ");
// 1. Handle -help flag
if (paramArr.length == 1 && paramArr[0] == "-help") {
messageBody =
"*Admin Tool - Verify User*\n" +
"A parser for setting user verification and descriptions. \n" +
"*Note:* This parser can only be triggered from admin channels.\n\n" +
"*Usage:*\n" +
"`!verify-admin @username [optional description]`\n" +
"`!unverify-admin @username [optional description]`\n\n" +
"*Examples:*\n" +
"`!verify-admin @Astrid Is an Impasta`\n" +
"`!unverify-admin @Astrid Is an Impasta`\n" +
"`!verify-admin @Astrid Some Role, Some Company`\n" +
"`!verify-admin @Astrid -v`\n\n" +
"*Supported flags:*\n" +
"`-v`: Verify the user (default for !verify-admin)\n" +
"`-unv`: Unverify the user (default for !unverify-admin)\n" +
"`-help`: Show this message";
slacker.send_chat(current, messageBody, true);
return;
}
// 2. Validate that a user tag is provided
if (paramArr.length == 0 || !/^<@.*?>$/.test(paramArr[0])) {
messageBody =
"!verify-admin must be called with a user tag and optional description. For example: `!verify-admin @Astrid Is an Impasta`\n\nThe full list of accepted triggers can be found by sending `!verify-admin -help`";
slacker.send_chat(current, messageBody, true);
return;
}
// 3. Extract User ID
userId = paramArr[0].replace(/[<>@]/g, "");
paramArr.shift();
// 4. Validate Flags
var hasV = paramArr.indexOf("-v") > -1;
var hasUnv = paramArr.indexOf("-unv") > -1;
if (hasV && hasUnv) {
messageBody =
"Please only provide one verify parameter in your message. Verification message ignored.";
slacker.send_chat(current, messageBody, true);
return;
}
// 5. Determine Verification Status
// Flags override the default based on the command name
if (hasUnv) {
verificationStatus = false;
} else if (hasV) {
verificationStatus = true;
} else {
verificationStatus = defaultVerificationStatus;
}
// 6. Extract Description
// Filter out the flags from the description
var descriptionArr = paramArr.filter(function(item) {
return item !== "-v" && item !== "-unv";
});
description = descriptionArr.join(" ");
// 7. Update User Record
var grUser = new GlideRecord("x_snc_slackerbot_user");
if (grUser.get("user_id", userId)) {
grUser.setValue("verified", verificationStatus);
if (description.length > 0) {
grUser.setValue("user_info", description);
} else {
// If no new description, keep the old one
description = grUser.getValue("user_info");
}
grUser.update();
messageBody =
"Updated <@" +
userId +
">'s verification information:\n";
messageBody +=
"Verification status: " +
(verificationStatus ? "*Verified*" : "*Unverified*") +
"\n";
messageBody += "User information:\n>" + description;
} else {
messageBody = "I'm afraid I can't do that as the ~limit~ user (<@" + userId + ">) does not exist.";
}
slacker.send_chat(current, messageBody, true);
}
})();