-
Notifications
You must be signed in to change notification settings - Fork 1
/
invocation.js
164 lines (138 loc) · 4.53 KB
/
invocation.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
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
import { GuildMember } from "discord.js";
import fs from "node:fs";
// extract user name from interaction
const getUserName = (interaction) => {
let tag = interaction.tag || interaction.user.tag;
if (interaction.member instanceof GuildMember) {
return interaction.user.username + "#" + interaction.member.user.id;
} else {
return interaction.user.username + "#" + interaction.user.id;
}
};
// if user not in invocations.json, add them
const addUser = (interaction, invocations) => {
const userName = getUserName(interaction);
if (!(userName in invocations)) {
invocations[userName] = {};
invocations[userName].commands = [];
invocations[userName].lifetimeInvocations = 0;
invocations[userName].commandInvocations = {};
invocations[userName].tier = "free";
invocations[userName].dayOfSignup = new Date().toISOString().slice(0, 10); // YYYY-MM-DD
}
return invocations;
};
// add invocation to invocations.json
const addInvocation = (
interaction,
invocations,
isButton = false,
query = null
) => {
const userName = getUserName(interaction);
if (isButton) {
// use interaction.message.content as query
query = interaction.message.content;
} else {
// use interaction.options.getString('input') as query
query = query || interaction.options.getString("input") || null;
}
invocations[userName].commands.push({
command: interaction.commandName,
time: new Date().toISOString(),
query: query,
});
// trim commands to last 10 if more than 10
if (invocations[userName].commands.length > 10) {
invocations[userName].commands = invocations[userName].commands.slice(-10);
}
if (interaction.commandName in invocations[userName].commandInvocations) {
invocations[userName].commandInvocations[interaction.commandName] += 1;
} else {
invocations[userName].commandInvocations[interaction.commandName] = 1;
}
invocations[userName].lifetimeInvocations += 1;
return invocations;
};
// save invocations.json
const saveInvocations = (invocations) => {
fs.writeFileSync("./invocations.json", JSON.stringify(invocations, null, 2));
};
// if 28 days have passed since user signup date, reset commandInvocations and reset dayOfSignup to today
const resetCommandInvocations = (invocations) => {
const today = new Date().toISOString().slice(0, 10);
const dayReset = 28;
for (const userName in invocations) {
const dayOfSignup = invocations[userName].dayOfSignup;
const daysSinceSignup = Math.floor(
(Date.parse(today) - Date.parse(dayOfSignup)) / 86400000
);
if (daysSinceSignup >= dayReset) {
invocations[userName].commandInvocations = {};
invocations[userName].dayOfSignup = today;
}
}
return invocations;
};
export const resetCommandInvocationsChoreWorkflow = () => {
let invocations = loadInvocations();
invocations = resetCommandInvocations(invocations);
saveInvocations(invocations);
};
// load invocations.json
const loadInvocations = () => {
try {
const data = fs.readFileSync("./invocations.json", "utf8");
return JSON.parse(data);
} catch (err) {
console.error(err);
return {};
}
};
const loadCommandLimits = () => {
try {
const data = fs.readFileSync("./command_limits.json", "utf8");
return JSON.parse(data);
} catch (err) {
console.error(err);
return {};
}
};
// main function
export const invocationWorkflow = async (
interaction,
isButton = false,
query = null
) => {
let invocations = loadInvocations();
invocations = addInvocation(interaction, invocations, isButton, query);
saveInvocations(invocations);
return invocations;
};
export const preWorkflow = async (interaction) => {
let invocations = loadInvocations();
invocations = addUser(interaction, invocations);
saveInvocations(invocations);
return checkCommandLimits(interaction, invocations);
};
const checkCommandLimits = async (interaction, invocations) => {
const commandLimits = loadCommandLimits();
const userName = getUserName(interaction);
const userInvocations = invocations[userName];
const commandName = interaction.commandName;
const tier = userInvocations.tier;
if (tier === "admin" || tier === "mod" || tier === "comped") {
return true;
}
if (commandName in commandLimits) {
const commandLimit =
commandLimits[commandName][
tier === "paid" ? "paid" : tier === "member" ? "member" : "free"
];
const commandInvocations = userInvocations.commandInvocations[commandName];
if (commandInvocations >= commandLimit) {
return false;
}
}
return true;
};