mirror of
https://github.com/ION606/selmer-bot-website.git
synced 2026-05-15 06:26:54 +00:00
Added support for Server Reminders
This commit is contained in:
+4
-1
@@ -230,8 +230,11 @@
|
|||||||
|
|
||||||
xhrsess.onloadend = (e) => {
|
xhrsess.onloadend = (e) => {
|
||||||
const sessionData = JSON.parse(xhrsess.response);
|
const sessionData = JSON.parse(xhrsess.response);
|
||||||
const userId = JSON.parse(sessionData.userId);
|
var userId = JSON.parse(sessionData.userId);
|
||||||
const delObjKeys = sessionStorage.getItem('delObjKeys');
|
const delObjKeys = sessionStorage.getItem('delObjKeys');
|
||||||
|
if (sessionStorage.getItem('ref') == 'guild') {
|
||||||
|
userId = serverSettings.Id;
|
||||||
|
}
|
||||||
|
|
||||||
//Send the data to the server
|
//Send the data to the server
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
|
|||||||
@@ -283,6 +283,7 @@
|
|||||||
|
|
||||||
w.sessionStorage.setItem("evlist", evjs);
|
w.sessionStorage.setItem("evlist", evjs);
|
||||||
w.sessionStorage.setItem('day', day);
|
w.sessionStorage.setItem('day', day);
|
||||||
|
w.sessionStorage.setItem('ref', 'user');
|
||||||
|
|
||||||
w.location.reload();
|
w.location.reload();
|
||||||
w.document.write('<title>Editing ${(new Date(Number(ev.time)))}</title>');
|
w.document.write('<title>Editing ${(new Date(Number(ev.time)))}</title>');
|
||||||
@@ -296,6 +297,7 @@
|
|||||||
if (!w) { w.close(); return alert("Your browser has JavaScript Disabled!"); }
|
if (!w) { w.close(); return alert("Your browser has JavaScript Disabled!"); }
|
||||||
|
|
||||||
w.sessionStorage.setItem("day", day);
|
w.sessionStorage.setItem("day", day);
|
||||||
|
w.sessionStorage.setItem("ref", 'user');
|
||||||
|
|
||||||
w.location.reload();
|
w.location.reload();
|
||||||
// w.document.write('<title>Editing ${(' + String(day) + ')}</title>');
|
// w.document.write('<title>Editing ${(' + String(day) + ')}</title>');
|
||||||
|
|||||||
+4
-2
@@ -279,6 +279,7 @@
|
|||||||
|
|
||||||
w.sessionStorage.setItem("evlist", evjs);
|
w.sessionStorage.setItem("evlist", evjs);
|
||||||
w.sessionStorage.setItem('day', day);
|
w.sessionStorage.setItem('day', day);
|
||||||
|
w.sessionStorage.setItem('ref', 'guild');
|
||||||
|
|
||||||
w.location.reload();
|
w.location.reload();
|
||||||
w.document.write('<title>Editing ${(new Date(Number(ev.time)))}</title>');
|
w.document.write('<title>Editing ${(new Date(Number(ev.time)))}</title>');
|
||||||
@@ -294,6 +295,7 @@
|
|||||||
if (!w) { w.close(); return alert("Your browser has JavaScript Disabled!"); }
|
if (!w) { w.close(); return alert("Your browser has JavaScript Disabled!"); }
|
||||||
|
|
||||||
w.sessionStorage.setItem("day", day);
|
w.sessionStorage.setItem("day", day);
|
||||||
|
w.sessionStorage.setItem('ref', 'guild');
|
||||||
|
|
||||||
w.location.reload();
|
w.location.reload();
|
||||||
// w.document.write('<title>Editing ${(' + String(day) + ')}</title>');
|
// w.document.write('<title>Editing ${(' + String(day) + ')}</title>');
|
||||||
@@ -472,12 +474,12 @@
|
|||||||
xhr2.open('get', 'http://www.selmerbot.com/getCal/', true);
|
xhr2.open('get', 'http://www.selmerbot.com/getCal/', true);
|
||||||
xhr2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
|
xhr2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
|
||||||
|
|
||||||
xhr2.setRequestHeader('userId', id);
|
xhr2.setRequestHeader('guildId', id);
|
||||||
|
|
||||||
xhr2.onloadend = (e) => {
|
xhr2.onloadend = (e) => {
|
||||||
const calList = new Map();
|
const calList = new Map();
|
||||||
|
|
||||||
const res = JSON.parse(xhr2.response);
|
const res = JSON.parse(xhr2.response); console.log(res, typeof res);
|
||||||
const times = res[0];
|
const times = res[0];
|
||||||
const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
|
const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
|
||||||
document.getElementById('monthName').innerText = `Editing Reminders for the Month of ${monthNames[(new Date()).getMonth()]}`;
|
document.getElementById('monthName').innerText = `Editing Reminders for the Month of ${monthNames[(new Date()).getMonth()]}`;
|
||||||
|
|||||||
@@ -153,24 +153,30 @@ app.get('/getChannels', async (req, res) => {
|
|||||||
|
|
||||||
|
|
||||||
app.get('/getCal', async (req, res) => {
|
app.get('/getCal', async (req, res) => {
|
||||||
const userId = req.headers.userid;
|
const userId = req.headers.userid || false;
|
||||||
|
const guildId = req.headers.guildid || false;
|
||||||
|
|
||||||
connection.then((client) => {
|
connection.then((client) => {
|
||||||
var times;
|
var times;
|
||||||
|
|
||||||
const dbo = client.db('main').collection('reminderKeys');
|
const dbo = client.db('main').collection('reminderKeys');
|
||||||
dbo.findOne({ userId: userId }).then((doc) => {
|
dbo.findOne({$or: [ {userId: userId}, {userId: guildId} ]}).then((doc) => {
|
||||||
if (!doc) { return res.send([[], []]); }
|
if (!doc) { return res.send([[], []]); }
|
||||||
times = doc.times;
|
times = doc.times;
|
||||||
let tbo = client.db('main').collection('reminders');
|
let tbo = client.db('main').collection('reminders');
|
||||||
|
|
||||||
tbo.find({ $where: function() { return (times.indexOf(this.time) != -1 && this.userId == userId); }}).toArray((err, docs) => {
|
// { $where: function() { return (times.indexOf(this.time) != -1 && this.userId == userId); }}
|
||||||
|
// tbo.find({time: {$in: times}}).toArray((err, docs) => { console.log("A", docs); throw 1; });
|
||||||
|
|
||||||
|
tbo.find({time: {$in: times}}).toArray((err, docs) => {
|
||||||
|
|
||||||
//There's gotta be a better way
|
//There's gotta be a better way
|
||||||
let newdoc = [];
|
let newdoc = [];
|
||||||
for (let i = 0; i < docs.length; i ++) {
|
for (let i = 0; i < docs.length; i ++) {
|
||||||
newdoc.push({});
|
newdoc.push({});
|
||||||
for (let j in docs[i]) {
|
for (let j in docs[i]) {
|
||||||
if (docs[i][j].userId == userId) {
|
if (!isNaN(j) && (docs[i][j].userId == userId || docs[i][j].guildId == guildId)) {
|
||||||
|
// console.log(`${docs[i][j].userId} == ${userId}`, `${docs[i][j].guildId} == ${guildId}`);
|
||||||
newdoc[i][j] = docs[i][j];
|
newdoc[i][j] = docs[i][j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -182,7 +188,6 @@ app.get('/getCal', async (req, res) => {
|
|||||||
//If there's nothing on that date, skip
|
//If there's nothing on that date, skip
|
||||||
// if (newdoc[i].amt == 0) { console.log(newdoc[i]); }
|
// if (newdoc[i].amt == 0) { console.log(newdoc[i]); }
|
||||||
}
|
}
|
||||||
|
|
||||||
res.send(JSON.stringify([times, newdoc]));
|
res.send(JSON.stringify([times, newdoc]));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -260,12 +265,19 @@ app.post('/sendData', async (req, res) => {
|
|||||||
// }); return console.log(obj.time);
|
// }); return console.log(obj.time);
|
||||||
doc.amt --;
|
doc.amt --;
|
||||||
|
|
||||||
|
// console.log(doc[obj.eventInd]); return;
|
||||||
|
var searchId;
|
||||||
|
if (doc[obj.eventInd].userId != null) {
|
||||||
|
searchId = doc[obj.eventInd].userId;
|
||||||
|
} else if (doc[obj.eventInd].guildId != null) {
|
||||||
|
searchId = doc[obj.eventInd].guildId;
|
||||||
|
}
|
||||||
|
|
||||||
kbo.findOne({ 'userId': doc[obj.eventInd].userId }).then((kdoc) => {
|
kbo.findOne({ 'userId': searchId }).then((kdoc) => {
|
||||||
if ((kdoc.times.length - 1) > 0) {
|
if ((kdoc.times.length - 1) > 0) {
|
||||||
kbo.updateOne({ 'userId': doc[obj.eventInd].userId }, {$pull: { times: obj.time }});
|
kbo.updateOne({ 'userId': searchId }, {$pull: { times: obj.time }});
|
||||||
} else {
|
} else {
|
||||||
kbo.deleteOne({ 'userId': doc[obj.eventInd].userId });
|
kbo.deleteOne({ 'userId': searchId });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -314,25 +326,30 @@ app.post('/sendData', async (req, res) => {
|
|||||||
//Reminder format = { time: 1212122, event: { guildId: "930148608400035860", userId: "12", name: "Some Generic Name", description: "Some description", offset: "15", link: "https://www.example.com" } }
|
//Reminder format = { time: 1212122, event: { guildId: "930148608400035860", userId: "12", name: "Some Generic Name", description: "Some description", offset: "15", link: "https://www.example.com" } }
|
||||||
app.post('/newCalEvent', async (req, res) => {
|
app.post('/newCalEvent', async (req, res) => {
|
||||||
if (req.headers.newcalevent) {
|
if (req.headers.newcalevent) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const obj = JSON.parse(req.headers.newcalevent);
|
const obj = JSON.parse(req.headers.newcalevent);
|
||||||
// console.log(obj.time, typeof obj.time); return;
|
// console.log(obj.time, typeof obj.time); return;
|
||||||
|
var searchId;
|
||||||
|
if (obj.event.userId != null) {
|
||||||
|
searchId = obj.event.userId;
|
||||||
|
} else if (obj.event.guildId != null) {
|
||||||
|
searchId = obj.event.guildId;
|
||||||
|
} else { return res.sendStatus(400); }
|
||||||
|
|
||||||
connection.then((client) => {
|
connection.then((client) => {
|
||||||
// Update the Key object first to check if the time is already there
|
// Update the Key object first to check if the time is already there
|
||||||
const kbo = client.db('main').collection('reminderKeys');
|
const kbo = client.db('main').collection('reminderKeys');
|
||||||
kbo.findOne(({ 'userId': obj.event.userId })).then((doc) => {
|
kbo.findOne({ 'userId': searchId }).then((doc) => {
|
||||||
|
|
||||||
if (doc) {
|
if (doc) {
|
||||||
if (doc.times.indexOf(obj.time) == -1) {
|
if (doc.times.indexOf(obj.time) == -1) {
|
||||||
kbo.updateOne({ 'userId': obj.event.userId }, { $push: { times: obj.time } })
|
kbo.updateOne({ 'userId': searchId }, { $push: { times: obj.time } })
|
||||||
.catch((err) => { console.error(err); res.sendStatus(500); });
|
.catch((err) => { console.error(err); res.sendStatus(500); });
|
||||||
} else {
|
} else {
|
||||||
return res.sendStatus(409);
|
return res.sendStatus(409);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
doc = { userId: obj.event.userId, times: [obj.time] }
|
doc = { 'userId': searchId, times: [obj.time] }
|
||||||
kbo.insertOne(doc);
|
kbo.insertOne(doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+23
-12
@@ -125,19 +125,23 @@
|
|||||||
|
|
||||||
|
|
||||||
function getSessionData() {
|
function getSessionData() {
|
||||||
var xhrsess = new XMLHttpRequest();
|
return new Promise((resolve, reject) => {
|
||||||
xhrsess.open('post', 'http://www.selmerbot.com/getSessionInfo/', true);
|
var xhrsess = new XMLHttpRequest();
|
||||||
xhrsess.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
|
xhrsess.open('post', 'http://www.selmerbot.com/getSessionInfo/', true);
|
||||||
xhrsess.setRequestHeader('session', window.localStorage.getItem('sessionId'));
|
xhrsess.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
|
||||||
|
xhrsess.setRequestHeader('session', window.localStorage.getItem('sessionId'));
|
||||||
|
|
||||||
//Reminder that this will return a map of strings (you'll have to use JSON.parse() again)
|
//Reminder that this will return a map of strings (you'll have to use JSON.parse() again)
|
||||||
xhrsess.onloadend = (e) => {
|
xhrsess.onloadend = (e) => {
|
||||||
return JSON.parse(xhrsess.response);
|
resolve(JSON.parse(xhrsess.response));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
xhrsess.send();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function sendData(event) {
|
async function sendData(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -174,12 +178,19 @@
|
|||||||
const d = new Date(new Date().getFullYear(), new Date().getMonth(), day, hour, minute);
|
const d = new Date(new Date().getFullYear(), new Date().getMonth(), day, hour, minute);
|
||||||
const serverSettings = JSON.parse(window.localStorage.getItem('serverInfo'));
|
const serverSettings = JSON.parse(window.localStorage.getItem('serverInfo'));
|
||||||
|
|
||||||
const sessionDataPromise = await getSessionData();
|
const sessionDataPromise = getSessionData();
|
||||||
sessionDataPromise.then((sessionData) => {
|
sessionDataPromise.then((sessionData) => {
|
||||||
const id = sessionData.userId;
|
var uid = null;
|
||||||
|
var gid = null;
|
||||||
|
|
||||||
|
if (sessionStorage.getItem("ref") == "user") {
|
||||||
|
uid = sessionData.userId;
|
||||||
|
} else if (serverSettings != undefined) {
|
||||||
|
gid = serverSettings.Id;
|
||||||
|
}
|
||||||
|
|
||||||
//Reminder format = { time: 1212122, event: { guildId: "930148608400035860", userId: "12", name: "Some Generic Name", description: "Some description", offset: "15", link: "https://www.example.com" location: "Some location" } }
|
//Reminder format = { time: 1212122, event: { guildId: "930148608400035860", userId: "12", name: "Some Generic Name", description: "Some description", offset: "15", link: "https://www.example.com" location: "Some location" } }
|
||||||
let obj = { time: String(d.getTime() - offsetTS), event: { guildId: serverSettings.Id, userId: id, name: name, description: desc, offset: offset, link: url, location: loc } }
|
let obj = { time: String(d.getTime() - offsetTS), event: { guildId: gid, userId: uid, name: name, description: desc, offset: offset, link: url, location: loc } }
|
||||||
|
|
||||||
//Send the data to the server
|
//Send the data to the server
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
|
|||||||
Reference in New Issue
Block a user