From b1caa85631ccf0e894df54430c824e688f58acde Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Fri, 6 May 2022 18:42:36 +0300 Subject: [PATCH] callbacks handling --- options.json | 3 +- src/structure/DiscordClient.js | 4 +- src/structure/client/wrappers/GuildWrapper.js | 50 +++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/options.json b/options.json index cd02799..3e39724 100644 --- a/options.json +++ b/options.json @@ -41,7 +41,8 @@ "permissions", "webhooks", "wordwatcher", - "users" + "users", + "callbacks" ] }, "mariadb": { diff --git a/src/structure/DiscordClient.js b/src/structure/DiscordClient.js index 736d836..1a038bf 100644 --- a/src/structure/DiscordClient.js +++ b/src/structure/DiscordClient.js @@ -234,7 +234,9 @@ class DiscordClient extends Client { _createWrappers() { this.guilds.cache.forEach((guild) => { - this.guildWrappers.set(guild.id, new GuildWrapper(this, guild)); + const wrapper = new GuildWrapper(this, guild); + this.guildWrappers.set(guild.id, wrapper); + wrapper.loadCallbacks(); }); this.logger.info(`Created guild wrappers`); diff --git a/src/structure/client/wrappers/GuildWrapper.js b/src/structure/client/wrappers/GuildWrapper.js index d2ba0f9..9446137 100644 --- a/src/structure/client/wrappers/GuildWrapper.js +++ b/src/structure/client/wrappers/GuildWrapper.js @@ -13,12 +13,62 @@ class GuildWrapper { this.guild = guild; this.webhooks = new Collection(); this.memberWrappers = new Collection(); + this.callbacks = new Collection(); this._settings = null; this._permissions = null; } + async loadCallbacks() { + const data = await this.client.mongodb.callbacks.find({ guild: this.id }); + for (const cb of data) { + await this.createCallback(cb, false); + } + } + + async createReminder(time, { user, channel, reminder }) { + const type = 'reminder'; + const now = Date.now(); + const id = `${type}:${user.id}:${now}`; + const data = { user: user.id, channel: channel.id, reminder, id, guild: this.id, type, time: time * 1000, created: now }; + await this.createCallback(data); + } + + async createCallback(data, update = true) { + const handler = this[`_${data.type}`];//.bind(this); + if (!handler) throw new Error(`Invalid callback type`); + + const now = Date.now(); + const time = data.created + data.time; + const diff = time - now; + console.log(diff); + if (diff < 5000) return this._reminder(data); + + const cb = { timeout: setTimeout(handler.bind(this), diff, data), data }; + this.callbacks.set(data.id, cb); + if(update) await this.client.mongodb.callbacks.updateOne({ id: data.id, guild: this.id }, data); + } + + async removeCallback(id) { + const cb = this.callbacks.get(id); + if(cb) clearTimeout(cb.timeout); + this.callbacks.delete(id); + await this.client.mongodb.callbacks.deleteOne({ guild: this.id, id }); + } + + async _reminder({ reminder, user, channel, id }) { + channel = await this.resolveChannel(channel); + if (channel) await channel.send({ + content: `<@${user}>`, + embeds: [{ + title: this.format('GENERAL_REMINDER_TITLE'), + description: reminder, + color: 619452 + }] }); + await this.removeCallback(id); + } + async checkInvite(code) { // Is maintained by the utility hook