-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
119 lines (106 loc) ยท 9.41 KB
/
utils.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
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
import 'dotenv/config';
import axios from 'axios';
import { verifyKey } from 'discord-interactions';
import addDays from 'date-fns/addDays/index.js'
const BWI_USER_ID = '467323668507131904';
const KAV_USER_ID = '694510056217247795';
export function VerifyDiscordRequest(clientKey) {
return function (req, res, buf) {
const signature = req.get('X-Signature-Ed25519');
const timestamp = req.get('X-Signature-Timestamp');
const isValidRequest = verifyKey(buf, signature, timestamp, clientKey);
if (!isValidRequest) {
res.status(401).send('Bad request signature');
throw new Error('Bad request signature');
}
}
}
export async function DiscordRequest(endpoint, options) {
const url = `https://discord.com/api/v10/${endpoint}`;
const headers = {
Authorization: `Bot ${process.env.DISCORD_TOKEN}`,
'Content-Type': 'application/json; charset=UTF-8',
'User-Agent': 'DiscordBot (https://github.com/discord/discord-example-app, 1.0.0)',
};
try {
return await axios({ url, headers, ...options });
} catch (e) {
throw e;
}
}
export function getRandomBoolean() {
return Math.floor(Math.random() * 2) === 0;
}
export function getRandomEmoji() {
const emojiList = [ '๐','๐','๐','๐','โบ','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐ณ','๐','๐','๐','๐','๐','๐ฃ','๐ข','๐','๐ญ','๐ช','๐ฅ','๐ฐ','๐
','๐','๐ฉ','๐ซ','๐จ','๐ฑ','๐ ','๐ก','๐ค','๐','๐','๐','๐ท','๐','๐ด','๐ต','๐ฒ','๐','๐ฆ','๐ง','๐','๐ฟ','๐ฎ','๐ฌ','๐','๐','๐ฏ','๐ถ','๐','๐','๐','๐ฒ','๐ณ','๐ฎ','๐ท','๐','๐ถ','๐ฆ','๐ง','๐จ','๐ฉ','๐ด','๐ต','๐ฑ','๐ผ','๐ธ','๐บ','๐ธ','๐ป','๐ฝ','๐ผ','๐','๐ฟ','๐น','๐พ','๐น','๐บ','๐','๐','๐','๐','๐ฝ','๐ฉ','๐ฅ','โจ','๐','๐ซ','๐ฅ','๐ข','๐ฆ','๐ง','๐ค','๐จ','๐','๐','๐','๐
','๐','๐','๐','๐','๐','โ','โ','๐','โ','๐','๐','๐','๐','๐','๐','๐','โ','๐','๐ช','๐ถ','๐','๐','๐ซ','๐ช','๐ฌ','๐ญ','๐','๐','๐ฏ','๐','๐
','๐','๐','๐','๐','๐
','๐ฐ','๐','๐','๐','๐ฉ','๐','๐','๐','๐','๐ก','๐ ','๐ข','๐','๐','๐','๐','๐ฝ','๐','๐','๐','๐ผ','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','โค','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐ค','๐ฅ','๐ฌ','๐ฃ','๐ญ','๐ถ','๐บ','๐ฑ','๐ญ','๐น','๐ฐ','๐ธ','๐ฏ','๐จ','๐ป','๐ท','๐ฝ','๐ฎ','๐','๐ต','๐','๐ด','๐','๐','๐ผ','๐ง','๐ฆ','๐ค','๐ฅ','๐ฃ','๐','๐','๐ข','๐','๐','๐','๐','๐','๐','๐','๐ ','๐','๐ฌ','๐ณ','๐','๐','๐','๐','๐','๐
','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐ฒ','๐ก','๐','๐ซ','๐ช','๐','๐','๐ฉ','๐พ','๐','๐ธ','๐ท','๐','๐น','๐ป','๐บ','๐','๐','๐','๐ฟ','๐พ','๐','๐ต','๐ด','๐ฒ','๐ณ','๐ฐ','๐ฑ','๐ผ','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐ ','โญ','โ','โ
','โ','โก','โ','โ','โ','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐ป','๐
','๐','๐','๐','๐','๐','๐','๐','๐ฎ','๐ฅ','๐ท','๐น','๐ผ','๐ฟ','๐','๐ฝ','๐พ','๐ป','๐ฑ','โ','๐','๐','๐ ','๐ก','๐บ','๐ป','๐','๐','๐','๐','๐','๐','๐ข','๐ฃ','โณ','โ','โฐ','โ','๐','๐','๐','๐','๐','๐','๐ก','๐ฆ','๐','๐
','๐','๐','๐','๐','๐','๐ฟ','๐ฝ','๐ง','๐ฉ','๐จ','๐ช','๐ฌ','๐ฃ','๐ซ','๐ช','๐','๐','๐ฐ','๐ด','๐ต','๐ท','๐ถ','๐ณ','๐ธ','๐ฒ','๐ง','๐ฅ','๐ค','โ','๐ฉ','๐จ','๐ฏ','๐ซ','๐ช','๐ฌ','๐ญ','๐ฎ','๐ฆ','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐
','๐','๐','๐','๐','โ','๐','๐','โ','โ','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐ฌ','๐ญ','๐ฐ','๐จ','๐ฌ','๐ค','๐ง','๐ผ','๐ต','๐ถ','๐น','๐ป','๐บ','๐ท','๐ธ','๐พ','๐ฎ','๐','๐ด','๐','๐ฒ','๐ฏ','๐','๐','โฝ','โพ','๐พ','๐ฑ','๐','๐ณ','โณ','๐ต','๐ด','๐','๐','๐','๐ฟ','๐','๐','๐','๐ฃ','โ','๐ต','๐ถ','๐ผ','๐บ','๐ป','๐ธ','๐น','๐ท','๐ด','๐','๐','๐','๐','๐','๐','๐','๐ค','๐ฑ','๐ฃ','๐ฅ','๐','๐','๐','๐','๐ฒ','๐ข','๐ก','๐ณ','๐','๐ฉ','๐ฎ','๐ฆ','๐จ','๐ง','๐','๐ฐ','๐ช','๐ซ','๐ฌ','๐ญ','๐ฏ','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐ ','๐','๐
','๐ฝ','๐ ','๐ก','๐ซ','๐ข','๐ฃ','๐ฅ','๐ฆ','๐ช','๐ฉ','๐จ','๐','โช','๐ฌ','๐ค','๐','๐','๐ฏ','๐ฐ','โบ','๐ญ','๐ผ','๐พ','๐ป','๐','๐
','๐','๐ฝ','๐','๐ ','๐ก','โฒ','๐ข','๐ข','โต','๐ค','๐ฃ','โ','๐','โ','๐บ','๐','๐','๐','๐','๐','๐','๐','๐
','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐จ','๐','๐','๐','๐','๐','๐ฒ','๐ก','๐','๐ ','๐','๐','๐','๐ซ','๐ฆ','๐ฅ','โ ','๐ง','๐ฐ','โฝ','๐ฎ','๐ฐ','โจ','๐ฟ','๐ช','๐ญ','๐','๐ฉ','โฌ','โฌ','โฌ
','โก','๐ ','๐ก','๐ค','โ','โ','โ','โ','โ','โ','๐','โ','โถ','๐ผ','๐ฝ','โฉ','โช','โน','โช','โฉ','โซ','โฌ','โคต','โคด','๐','๐','๐','๐','๐','๐','๐','๐','๐','๐ถ','๐ฆ','๐','๐ฏ','๐ณ','๐ต','๐ด','๐ฒ','๐','๐น','๐บ','๐ถ','๐','๐ป','๐น','๐บ','๐ผ','๐พ','๐ฐ','๐ฎ','๐
ฟ','โฟ','๐ญ','๐ท','๐ธ','๐','โ','๐','๐','๐
','๐','๐','ใ','ใ','๐','๐','๐','๐ซ','๐','๐ต','๐ฏ','๐ฑ','๐ณ','๐ท','๐ธ','โ','โณ','โ','โ','โ
','โด'];
return Math.floor(Math.random() * 5) === 1 ? '๐ซฆ' : emojiList[Math.floor(Math.random() * emojiList.length)];
}
export function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
export async function getCompliment() {
const complimentRes = await axios.get('https://8768zwfurd.execute-api.us-east-1.amazonaws.com/v1/compliments');
let compliment = complimentRes.data.toLowerCase();
const flow = getRandomBoolean();
compliment = capitalize(
compliment
.replace('you are', flow ? 'Flow is' : 'Kavi is')
.replace('you\'re', flow ? 'Flow is' : 'Kavi is')
.replace('you were', flow ? 'Flow was' : 'Kavi was')
.replace('you have', flow ? 'Flow has' : 'Kavi has')
.replace('your', flow ? 'Flow\'s' : 'Kavi\'s')
);
if (compliment.startsWith('you')) {
compliment = compliment.replace('you', flow ? 'Flow, you' : 'Kavi, you')
}
// if (compliment.includes('Flow has')) {
// const nonBodyParts = ['heart', 'mind', 'personality', 'radiance', 'ideas', 'thoughts', 'strengths', 'brain'];
// const bodyPartInCompliment = nonBodyParts.filter(t => compliment.includes(t)).length === 0;
// if (bodyPartInCompliment) {
// const complimentWords = compliment.split(' ');
// complimentWords[complimentWords.length - 1] = `~~${complimentWords[complimentWords.length - 1]}~~ ${nonBodyParts[Math.floor(Math.random() * 4)]}`;
// compliment = complimentWords.join(' ');
// }
// }
if (compliment.includes('9 out of 10')) {
compliment += '. David Tennant agrees with them';
}
return compliment;
}
export const FULL_DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
export const DAYS_INITIALS = ['Su', 'M', 'T', 'W', 'Th', 'F', 'Sa'];
export function getDateFromInput(input, timezone, userId) {
let dateString = input.trim();
dateString = dateString.replace('PM', 'pm');
dateString = dateString.replace('AM', 'am');
if (dateString.match(/\s\d+\s*[a|p]m/)) {
dateString = dateString.replace(/\s*pm/, ':00 pm');
dateString = dateString.replace(/\s*am/, ':00 am');
}
if (dateString.match(/\d[a|p]m/)) {
dateString = dateString.replace('pm', ' pm');
dateString = dateString.replace('am', ' am');
}
let timeEndIndex = dateString.indexOf(' am') === -1 ? dateString.indexOf(' pm') : dateString.indexOf(' am')
timeEndIndex += 3;
const game = timeEndIndex >= dateString.length ? '' : dateString.substring(timeEndIndex).trim();
dateString = dateString.substring(0, timeEndIndex).trim();
const curMonth = (new Date()).getMonth() + 1;
let defaultTz = (curMonth >= 3 && curMonth < 11 ? ' edt' : ' est')
if (userId == BWI_USER_ID || userId == KAV_USER_ID) {
defaultTz = (curMonth >= 3 && curMonth < 11 ? ' cdt' : ' cst')
}
dateString += timezone ? ` ${timezone}` : defaultTz;
let date = new Date(dateString);
if (isNaN(date.getTime())) {
let dateIndex = -1;
DAYS_INITIALS.forEach((day, i) => {
if (dateString.toLowerCase().startsWith(day.toLowerCase())) {
dateIndex = i;
}
});
const resultDate = addDays(new Date(), dateIndex - new Date().getDay()).toLocaleDateString();
date = new Date(resultDate + ' ' + dateString.split(' ').slice(1).join(' '));
}
return !isNaN(date.getTime()) ? `${FULL_DAYS[date.getHours() > 5 ? date.getDay() : (date.getDay() - 1 < 0 ? 6 : date.getDay() - 1)]} \\<t:${date.getTime() / 1000}:t>: ${game}` : 'Invalid Date passed in';
}