Created
August 30, 2017 15:10
-
-
Save TheOnlyArtz/38955a4760f2fbd2a0c8321c49fee29a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function reminder(client) { | |
setInterval(async () => { | |
/** | |
* @param {String} reminders - the table | |
* @param {String} NONE - the Index value | |
* @param {String} guildID - Table's index | |
*/ | |
let arr = await r.table('reminders').getAll("NONE", {index : "guildID"}).run(); | |
//Check if the array is empty | |
if (arr[0].inQueue[0]) { | |
for (var i = 0; i < arr[0].inQueue.length; i++) { | |
/** | |
* @var {Array|Object|String} - Get the guildID from the object | |
* @var {Array|Object|String} - Get the userID from the object | |
*/ | |
const guildID = arr[0].inQueue[i]['guildID']; | |
const userID = arr[0].inQueue[i]['userID']; | |
const usersUnix = await r.table('reminders').getAll(guildID, userID, {index: "guildID", index: "userID"}).run(); | |
// Check for the time and where to trigger the reminder | |
if (usersUnix[0].time &&(usersUnix[0].time < Date.now())) { | |
try { | |
let guild = client.guilds.get(guildID); | |
let member = await guild.fetchMember(userID); | |
client.channels.get(usersUnix[0].channelID).send('**Your reminder:** ' + member + ' ' + usersUnix[0].content); | |
} catch (e) { | |
logger.error(e) | |
} | |
await r.table('reminders').getAll(guildID, userID, {index: "guildID", index: "userID"}) | |
.delete() | |
.run() | |
let appendToArray = (table, uArray) => r.table(table) | |
.filter({guildID : "NONE"}) | |
.update(object => ({ [uArray]: object(uArray) | |
.default([]).deleteAt(i) })) | |
.run(); | |
appendToArray('reminders', 'inQueue') | |
} | |
} | |
} | |
}, 5 * 1000); | |
} | |
module.exports = reminder; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment