Repository navigation
Expand file tree
/
Copy pathcountdownHelper.js
More file actions
170 lines (139 loc) · 4.95 KB
/
Copy pathcountdownHelper.js
File metadata and controls
170 lines (139 loc) · 4.95 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import chrono from "chrono-node";
import { computeTimeDiff } from "./computeTimeDiff.js";
import { t, availableLanguages } from "./lang.js";
export const parseInline = command => {
if (!command) return null;
let commands = [];
let parts = [];
for (let i = 1; i <= 5; i++) {
const match = command.match(/^(.*?)!!countdown ([^!]+)!(.*)$/ims);
if (match?.length !== 4) break;
parts.push(match[1]);
commands.push(match[2].trim().toLowerCase());
command = match[3];
}
if (parts.length) parts.push(command);
else return null;
return { commands, parts };
};
const findRoleId = (commandArray, guild) => {
let tag = commandArray.shift();
// Check if mention
if (tag.match(/^<@(&|!)?[0-9]{17,19}>$/)) return tag;
// Check if snowflake
if (tag.match(/^[0-9]{17,19}$/))
// Return role if found. If not, assume user snowflake.
return guild.roles.cache.find(role => role.id === tag) || `<@${tag}>`;
// Try to match role name with id.
while (commandArray.length) {
const roleId = guild.roles.cache.find(
role => role.name.toLowerCase().replace(/ /g, "") === tag
);
if (roleId) return roleId;
tag += commandArray.shift();
}
return null;
};
export const getTag = (command, message) => {
const commandArray = command.toLowerCase().split(/ +/);
const tagdirective = commandArray.shift();
// Check if tag directive exists
if (!tagdirective.startsWith("tag")) return { command, tag: null };
// Check if tagging specific role
if (tagdirective === "tag") {
if (commandArray.length < 2)
return { error: "Syntax: `!countdown tag <rolename> <date/time>`" };
const tagId = findRoleId(commandArray, message.guild);
return tagId
? { command: commandArray.join(" "), tag: `${tagId}` }
: { error: "Specified role was not found." };
}
if (
["tageveryone", "taghere"].includes(tagdirective) &&
!message.channel.permissionsFor(message.author).has("MENTION_EVERYONE")
)
return {
error: `You cannot use \`${tagdirective}\` as you do not seem to have the required permissions.`,
};
// prettier-ignore
const tag = tagdirective === "tageveryone" ? "@everyone" :
tagdirective === "taghere" ? "@here" :
tagdirective === "tagme" ? `${message.author}` : null;
return tag
? { command: commandArray.join(" "), tag }
: { error: "Accepted tags: tagme/taghere/tageveryone" };
};
const maxTimeEnd = new Date();
maxTimeEnd.setFullYear(maxTimeEnd.getFullYear() + 2);
export const computeCountdown = (command, message) => {
const countObj = {};
// Support different languages
const commandPieces = command.toLowerCase().split(/ +/);
const langIndex = commandPieces.findIndex(word =>
availableLanguages.includes(word.toLowerCase())
);
if (langIndex != -1) {
countObj.lang = commandPieces[langIndex].toLowerCase();
commandPieces.splice(langIndex, 1);
command = commandPieces.join(" ");
}
// Get tag if present
const tagObj = getTag(command, message);
if (tagObj.error) return tagObj;
command = tagObj.command;
if (tagObj.tag) countObj.tag = tagObj.tag;
if (["infinity", "\u221E", "\u267E\uFE0F"].includes(command)) return { error: "Seriously? -_-" };
// Parse time and check if valid
const timeEnd = chrono.parseDate(command);
if (!timeEnd) return { error: "Invalid date/time." };
const timeLeft = timeEnd - Date.now();
countObj.timeEnd = timeEnd;
// Check if date/time in the past
if (timeLeft < 0)
return {
error:
"You are trying to set a countdown to a date/time in the past. " +
"A common reason for this is the lack of year.",
};
if (timeEnd > maxTimeEnd)
return {
error:
":eyes: That's too far away in the future. Currently, countdowns are limited to 2 years.",
};
if (timeLeft < 55000)
return { error: "Too short! Countdowns must be higher than a minute!", ephemeral: countObj };
return countObj;
};
export const assembleInlineMessage = (timers, parts) => {
let nextUpdate = null;
let priority = 0;
let finishedTimers = [];
const assembled = timers.map((timer, index) => {
const timeEnd = new Date(timer.timeEnd);
const timeLeft = timeEnd - Date.now();
let diff;
// if countdown is done
if (timeLeft < 10000) {
diff = t("inlineNoMinutes", timer.lang);
finishedTimers.push(index);
} else {
const { humanDiff, timeLeftForNextUpdate } = computeTimeDiff(timeLeft, timer.lang);
diff = humanDiff;
// If next update is the last one, prioritize it
if (!timeLeftForNextUpdate) priority = 10;
if (!nextUpdate) nextUpdate = timeEnd;
// Get nextUpdate time
const timerNextUpdate = timeEnd - timeLeftForNextUpdate;
nextUpdate = Math.min(nextUpdate, timerNextUpdate);
}
return parts[index] + diff;
});
// console.log(assembled);
assembled.push(parts[parts.length - 1]);
return {
assembledMessage: assembled.join(""),
nextUpdate,
priority,
finishedTimers,
};
};