From 312f783bbcac23971561cf76b2e47e6bf30ce310 Mon Sep 17 00:00:00 2001 From: ION606 <58801387+ION606@users.noreply.github.com> Date: Fri, 19 Aug 2022 18:10:16 +0000 Subject: [PATCH] Added reminders integration a while ago and forgot to commit --- checkCal.js | 32 +++++++++--------- main.js | 95 +++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 82 insertions(+), 45 deletions(-) diff --git a/checkCal.js b/checkCal.js index 15a4d6f..54ba503 100644 --- a/checkCal.js +++ b/checkCal.js @@ -1,6 +1,9 @@ var request = require('request'); const { MongoClient, ServerApiVersion } = require("mongodb"); const mongouri = process.env.mongooseURI; +const clientMain = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 }); +const connection = clientMain.connect(); + function getDateInMin() { const d = new Date(); @@ -12,33 +15,24 @@ function getDateInMin() { function checkCal() { try { - const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 }); - client.connect(err => { - if (err) { return console.error(err); } - + connection.then(client => { const dbo = client.db('main').collection('reminders'); //Get all events happening this minute - //Set up the date/time - const time = getDateInMin(); dbo.findOne({ time: time }).then((docs) => { //Get All Events happening this MINUTE and put them into the following format - // { "0": { guildId: string, userId: string, name: string, description: string, location: string, time: string (in UTC format), offset: int (in ms) } } - // console.log(time, docs); if (!docs) { return; } - dbo.deleteOne({ time: time }); - + var m = {}; for (i in docs) { var doc = docs[i]; if (isNaN(Number(i))) { continue; } //Get the time out of ms - doc.time = time / 1000; + doc.time = (Number(time) + ((Number(doc.offset)/* + 1*/) * 60000)) / 1000; m[`${i}`] = doc; - //{ guildId: doc.guildId, userId: doc.userId, name: doc.name, description: doc.description, location: doc.location, time: time, offset: doc.offset } } var clientServerOptions = { @@ -58,15 +52,12 @@ function checkCal() { }); }); - client.close(); + // client.close(); } catch (err) { console.error(err); } } -//Start at exactly the minute mark -//???? - //Make sure the app doesn't go to sleep const express = require("express"); @@ -77,6 +68,15 @@ app.get('/', async (req, res) => { }) const listener = app.listen(process.env.PORT, () => { + + //Start at exactly the minute mark + while (true) { + let d = new Date(); + if (d.getSeconds() == 0) { + break; + } + } + console.log("Your app is listening on port " + listener.address().port); setInterval(checkCal, 60000); }); \ No newline at end of file diff --git a/main.js b/main.js index 6213e65..67699f2 100644 --- a/main.js +++ b/main.js @@ -5,7 +5,13 @@ 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"); @@ -27,11 +33,6 @@ const bot = new Client({ partials: ['CHANNEL'] }); -bot.on('ready', async () => { - console.log("Bot online!"); -}); -bot.login(token); - const app = express(); app.use(express.static("public")); app.use(bodyParser.urlencoded({ extended: true })); @@ -46,13 +47,7 @@ function createSubscription(obj) { const tier = plan.amount / 500; // if (tier != 1 && tier % 2 != 0) { throw `INCORRECT TIER (${tier}) from $${plan.amount}` }; //WRONG - const client = new MongoClient(mongouri, { - useNewUrlParser: true, - useUnifiedTopology: true, - serverApi: ServerApiVersion.v1, - }); - - client.connect(async (err) => { + connection.then(async (client) => { const d = new Date(); const startDateUTC = `${d.getUTCDay()}|${d.getUTCMonth()}|${d.getUTCFullYear()}`; const dbo = client.db("main").collection("authorized"); @@ -62,13 +57,7 @@ function createSubscription(obj) { function deleteSubscription(obj) { - const client = new MongoClient(mongouri, { - useNewUrlParser: true, - useUnifiedTopology: true, - serverApi: ServerApiVersion.v1, - }); - - client.connect(async (err) => { + 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 } }); @@ -144,6 +133,7 @@ app.post('/reminders', async (req, res) => { var guild; var reminder; var timeUTC; + var role; try { // reminder = JSON.parse(reminders); @@ -151,14 +141,47 @@ app.post('/reminders', async (req, res) => { for (i in reminders) { reminder = reminders[i]; - guild = bot.guilds.cache.get(reminder.guildId); - user = guild.members.cache.get(reminder.userId); + //Make sure there are no empty fields + if (reminder.link == "") { reminder.link = "N/A"; } + if (reminder.location == "") { reminder.location = "N/A"; } - if (!user || !guild) { console.error(`Unknown user (guildId: ${reminder.guildId} userId: ${reminder.userId})`); return res.sendStatus(500); } + 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} `; + } - let temp = `${reminder.name} is coming up in at `; + temp += `***${reminder.name}*** is coming up in on `; const embd = new MessageEmbed() .setAuthor({ name: "Selmer Bot", url: "", iconURL: bot.user.displayAvatarURL() }) .setTitle(temp) @@ -166,10 +189,19 @@ app.post('/reminders', async (req, res) => { .addFields( { name: 'Time', value: `` }, { name: 'Location', value: `${reminder.location}` }, - { name: 'Link', value: `${reminder.link}` } + { name: 'Link', value: `${reminder.link}` }, + { name: 'Offset', value: `${reminder.offset} minutes` } ); - user.send({ embeds: [embd] }); + 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); @@ -182,8 +214,13 @@ app.post('/reminders', async (req, res) => { app.get('/', async (req, res) => { res.sendStatus(200); -}) - -const listener = app.listen(process.env.PORT, () => { - console.log("Your app is listening on port " + listener.address().port); }); + +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); \ No newline at end of file