-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsupabase-invocations.js
238 lines (191 loc) · 5.61 KB
/
supabase-invocations.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { GuildMember } from "discord.js";
import { createClient } from "@supabase/supabase-js";
// import config from "./config.json" assert { "type": "json" };
import dotenv from "dotenv";
dotenv.config();
const { supabaseUrl, supabaseKey } = process.env;
const supabase = createClient(supabaseUrl, supabaseKey, {
auth: {
persistSession: false,
},
});
// 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 table, add them
const addUser = async (interaction) => {
const userName = getUserName(interaction);
const { data, error } = await supabase
.from("invocations")
.select("*")
.eq("username", userName);
if (error) {
console.log(error);
return;
}
if (data.length === 0) {
const { data, error } = await supabase.from("invocations").insert([
{
username: userName,
commands: [],
commandInvocations: {},
dayOfSignup: new Date().toISOString().slice(0, 10), // YYYY-MM-DD
tier: "free",
lifetimeInvocations: 0,
},
]);
if (error) {
console.log(error);
return;
}
}
};
// add invocation to username in invocations table
const addInvocation = async (interaction, 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;
}
const { data, error } = await supabase
.from("invocations")
.select("*")
.eq("username", userName);
if (error) {
console.log(error);
return;
}
const user = data[0];
user.commands.push({
command: interaction.commandName,
time: new Date().toISOString(),
query: query,
});
// trim commands to last 10 if more than 10
if (user.commands.length > 10) {
user.commands = user.commands.slice(-10);
}
if (interaction.commandName in user.commandInvocations) {
user.commandInvocations[interaction.commandName] += 1;
} else {
user.commandInvocations[interaction.commandName] = 1;
}
user.lifetimeInvocations += 1;
const { insertData, insertError } = await supabase
.from("invocations")
.update({
commands: user.commands,
commandInvocations: user.commandInvocations,
lifetimeInvocations: user.lifetimeInvocations,
})
.eq("username", userName);
if (insertError) {
console.log(insertError);
return;
}
};
// 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 = async () => {
// fetch invocations from table where dayOfSignup is >= 28 days ago
const today = new Date().toISOString().slice(0, 10);
const dayReset = 28;
const daysAgo = new Date(new Date().setDate(new Date().getDate() - dayReset))
.toISOString()
.slice(0, 10);
const { data, error } = await supabase
.from("invocations")
.select("*")
.gte("dayOfSignup", daysAgo);
if (error) {
console.log(error);
return;
}
// reset commandInvocations and dayOfSignup to today
for (const user of data) {
user.commandInvocations = {};
user.dayOfSignup = today;
}
// update invocations table
const { insertData, insertError } = await supabase
.from("invocations")
.update(data);
if (insertError) {
console.log(insertError);
return;
}
};
export const resetCommandInvocationsChoreWorkflow = () => {
resetCommandInvocations();
};
const loadCommandLimits = async () => {
// fetch command limits from supabase command_limits table
const { data, error } = await supabase.from("command_limits").select("*");
if (error) {
console.log(error);
return;
}
const commandLimits = {};
for (const command of data) {
commandLimits[command.command] = {
free: command.free,
member: command.member,
paid: command.paid,
};
}
return commandLimits;
};
// main function
export const invocationWorkflowSB = async (
interaction,
isButton = false,
query = null
) => {
await addInvocation(interaction, isButton, query);
};
export const preWorkflowSB = async (interaction) => {
await addUser(interaction);
return checkCommandLimits(interaction);
};
const checkCommandLimits = async (interaction) => {
const commandLimits = await loadCommandLimits();
const userName = getUserName(interaction);
const { data, error } = await supabase
.from("invocations")
.select("*")
.eq("username", userName);
if (error) {
console.log(error);
return;
}
const invocations = data[0];
const userInvocations = invocations;
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;
};