This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (70 loc) · 3.22 KB
/
index.js
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
// imports
require('dotenv').config()
const Discord = require('discord.js')
const { IANAZone, FixedOffsetZone } = require("luxon");
// functions
function validateTz(tz) {
const isValid = (IANAZone.isValidZone(tz) || (FixedOffsetZone.parseSpecifier(tz) !== null && FixedOffsetZone.parseSpecifier(tz).isValid));
return (isValid ? "Valid Timezone 🟢" : "Invalid Timezone 🔴");
}
function validateUrl(url) {
let urlPattern = new RegExp("(http|https)://(\\w+:{0,1}\\w*)?(\\S+)(:[0-9]+)?(/|/([\\w#!:.?+=&%!-/]))?");
return (urlPattern.test(url) ? "Valid URL 🟢" : "Invalid URL 🔴");
}
function validateCalId(calendarId) {
// regex filter groups
const groupCalId = RegExp("([a-z0-9]{26}@group.calendar.google.com)");
const cGroupCalId = RegExp("^(c_[a-z0-9]{26}@)");
const importCalId = RegExp("(^[a-z0-9]{32}@import.calendar.google.com)");
const gmailAddress = RegExp("^([a-z0-9.][email protected])");
const underscoreCalId = RegExp("^[a-z0-9](_[a-z0-9]{26}@)");
const domainCalId = RegExp("^([a-z0-9.]+_[a-z0-9]{26}@)");
const domainAddress = RegExp("(^[a-z0-9_.+-]+@[a-z0-9-]+.[a-z0-9-.]+$)");
// filter through regex
if (gmailAddress.test(calendarId)) { // matches gmail
} else if (importCalId.test(calendarId)) { // matches import ID
} else if (groupCalId.test(calendarId)) {
if (cGroupCalId.test(calendarId)) { // matches cGroup
} else if (domainCalId.test(calendarId)) {
return "Warning 🟠: GSuite/ Workplace - please see https://nilesbot.com/start/#gsuiteworkplace";
} else if (underscoreCalId.test(calendarId)) {
return "Warning 🟠: New Calendar Format - please see https://nilesbot.com/start/#new-calendar-format";
}
return "Passed 🟢: Valid Calendar ID"; // normal group id or any variation
} else if (domainAddress.test(calendarId)) {
return ("Warning 🟠: GSuite/ Workplace - please see https://nilesbot.com/start/#gsuiteworkplace");
} else {
return "Failed 🔴: Invalid Calendar ID"; // break and return false
}
return "Passed 🟢: Valid Calendar ID"; // if did not reach false
}
const client = new Discord.Client() // create client
client.on('ready', () => {
console.log('Ready')
client.user.setPresence({ // set presence
activity: { type: process.env.ACT_TYPE, name: process.env.ACT_NAME },
status: process.env.STATUS
})
})
client.login(process.env.TOKEN) // login
client.on('message', message => {
if (!message.author.bot && message.channel.type === "dm") { // check if sent by self & only over DMs
const args = message.content.trim().split(' ');
const command = args.shift().toLowerCase();
if (command === 'help') {
message.channel.send('Valid Commands: `url/u, tz/t calendar/cal/c help`')
} else if (args[0]) {
if (['calendar', 'cal', 'c'].includes(command)) { // clean url
message.channel.send(validateCalId(args[0]))
} else if (['url', 'u'].includes(command)) { // unshorten url
message.channel.send(validateUrl(args[0]))
} else if (['tz', 't'].includes(command)) {
message.channel.send(validateTz(args[0]))
}
} else {
message.channel.send("Please include value to test")
}
}})
process.on('uncaughtException', function(err) {
console.log(err)
})