callbacks handling
This commit is contained in:
parent
5568e1fb52
commit
b1caa85631
@ -41,7 +41,8 @@
|
|||||||
"permissions",
|
"permissions",
|
||||||
"webhooks",
|
"webhooks",
|
||||||
"wordwatcher",
|
"wordwatcher",
|
||||||
"users"
|
"users",
|
||||||
|
"callbacks"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"mariadb": {
|
"mariadb": {
|
||||||
|
@ -234,7 +234,9 @@ class DiscordClient extends Client {
|
|||||||
_createWrappers() {
|
_createWrappers() {
|
||||||
|
|
||||||
this.guilds.cache.forEach((guild) => {
|
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`);
|
this.logger.info(`Created guild wrappers`);
|
||||||
|
|
||||||
|
@ -13,12 +13,62 @@ class GuildWrapper {
|
|||||||
this.guild = guild;
|
this.guild = guild;
|
||||||
this.webhooks = new Collection();
|
this.webhooks = new Collection();
|
||||||
this.memberWrappers = new Collection();
|
this.memberWrappers = new Collection();
|
||||||
|
this.callbacks = new Collection();
|
||||||
|
|
||||||
this._settings = null;
|
this._settings = null;
|
||||||
this._permissions = 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) {
|
async checkInvite(code) {
|
||||||
|
|
||||||
// Is maintained by the utility hook
|
// Is maintained by the utility hook
|
||||||
|
Loading…
Reference in New Issue
Block a user