This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
231 lines (188 loc) · 6.93 KB
/
main.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
const path = require("path");
const express = require("express");
const stripe = require("stripe")(process.env.APIKey);
const { MongoClient, ServerApiVersion } = require("mongodb");
const { Client, Intents, MessageEmbed } = require('discord.js');
const mongouri = process.env.mongooseURI;
const token = process.env.token;
const client = new MongoClient(mongouri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1,
});
const connection = client.connect();
// Use body-parser to retrieve the raw body as a buffer
const bodyParser = require("body-parser");
const endpointSecret = process.env.webhooksecret;
const bot = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_VOICE_STATES,
Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Intents.FLAGS.GUILD_PRESENCES,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGE_TYPING,
],
partials: ['CHANNEL']
});
const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
//Adding the "add customer" function is unecessary as it is handled on the Heroku server
function createSubscription(obj) {
const customer = obj.customer;
const plan = obj.plan;
//$5 = tier 1, $10 = tier 2, etc...CHANGE IF ANY VARIEATIONS ARE ADDED!!!
const tier = plan.amount / 500;
// if (tier != 1 && tier % 2 != 0) { throw `INCORRECT TIER (${tier}) from $${plan.amount}` }; //WRONG
connection.then(async (client) => {
const d = new Date();
const startDateUTC = `${d.getUTCDay()}|${d.getUTCMonth()}|${d.getUTCFullYear()}`;
const dbo = client.db("main").collection("authorized");
dbo.updateOne({ stripeID: customer }, { $set: { startDateUTC: startDateUTC, paid: true, tier: tier } });
});
}
function deleteSubscription(obj) {
connection.then(async (client) => {
const customer = obj.customer;
const dbo = client.db("main").collection("authorized");
dbo.updateOne({ stripeID: customer }, { $set: { startDateUTC: null, paid: false, tier: 0 } });
});
}
function changeSubscription(obj) {
const upStates = ['trialing', 'active'];
const downStates = ['incomplete', 'incomplete_expired', 'past_due', 'canceled', 'unpaid'];
const status = obj.status.trim();
if (upStates.includes(status)) {
createSubscription(obj);
} else if (downStates.includes(status)) {
deleteSubscription(obj);
} else { console.log(`ERR!\nSTATE ${status} NOT IN ANY STATE LIST!`); }
}
// Match the raw body to content type application/json
app.post("/webhooks",
bodyParser.raw({ type: "application/json" }), (request, response) => {
const sig = request.headers["stripe-signature"];
let event;
try {
event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
} catch (err) {
return response.status(400).send(`Webhook Error: ${err.message}`);
}
if (!event) {
return response.send("Unknown webhook!");
}
// Handle the event
switch (event.type) {
case "customer.subscription.created":
createSubscription(event.data.object, true);
break;
case "customer.subscription.updated":
// changeSubscription(event.data.object);
// console.log("Implement later (changing tiers, etc)");
changeSubscription(event.data.object);
break;
case "customer.subscription.deleted":
deleteSubscription(event.data.object, false);
break;
// ... handle other event types
default:
// Unexpected event type
return response.status(400).end();
}
// Return a response to acknowledge receipt of the event
response.json({ received: true });
}
);
//ReminderS format [{ guildId: string, userId: string, event: [name, description, location], time: string (in UTC format), offset: int (in ms) }]
app.post('/reminders', async (req, res) => {
const code = req.headers['botcode'];
if (code != process.env.accesscode) {
return res.sendStatus(500);
}
const reminders = JSON.parse(req.headers['reminders'])
// return console.log(JSON.parse(req.headers['reminders']), typeof JSON.parse(req.headers['reminders']));
var user;
var guild;
var reminder;
var timeUTC;
var role;
try {
// reminder = JSON.parse(reminders);
for (i in reminders) {
reminder = reminders[i];
//Make sure there are no empty fields
if (reminder.link == "") { reminder.link = "N/A"; }
if (reminder.location == "") { reminder.location = "N/A"; }
const p = new Promise((resolve, reject) => {
//The reminder is pinging a guild
if (reminder.guildId != null) {
guild = bot.guilds.cache.get(reminder.guildId);
connection.then((client) => {
const dbo = client.db(reminder.guildId).collection('SETUP');
dbo.findOne({ _id: "announcement" }).then((doc) => {
if (!doc || !doc.role || !doc.channel) {
reject(null);
return;
} else {
user = guild.channels.cache.get(doc.channel);
role = guild.roles.cache.get(doc.role);
resolve([user, role]);
}
})
})
} else {
bot.users.fetch(reminder.userId).then((user) => { resolve([user, null]); })
// user = guild.members.cache.get(reminder.userId);
}
});
p.then((data) => {
user = data[0];
role = data[1];
timeUTC = Number(reminder.time) + Number(reminder.offset);
let temp = "";
let c = "";
if (role != undefined) {
c = `${role} `;
}
temp += `***${reminder.name}*** is coming up in <t:${timeUTC}:R> on <t:${timeUTC}:F>`;
const embd = new MessageEmbed()
.setAuthor({ name: "Selmer Bot", url: "", iconURL: bot.user.displayAvatarURL() })
.setTitle(temp)
.setDescription(`Description: ${reminder.description}`)
.addFields(
{ name: 'Time', value: `<t:${timeUTC}:F>` },
{ name: 'Location', value: `${reminder.location}` },
{ name: 'Link', value: `${reminder.link}` },
{ name: 'Offset', value: `${reminder.offset} minutes` }
);
if (c != "") {
user.send({ content: c, embeds: [embd] });
} else {
user.send({ embeds: [embd] });
}
}).catch(() => {
console.error(`Unknown user (guildId: ${reminder.guildId} userId: ${reminder.userId})`); res.sendStatus(500);
})
return;
}
res.sendStatus(200);
} catch (err) {
console.error(err);
res.sendStatus(500);
}
});
app.get('/', async (req, res) => {
res.sendStatus(200);
});
bot.on('ready', async () => {
const listener = app.listen(process.env.PORT, () => {
console.log("Your app is listening on port " + listener.address().port);
});
console.log("Bot online!");
});
bot.login(token);