diff --git a/Xiao.js b/Xiao.js index 498a95e5..ee9d6dcd 100644 --- a/Xiao.js +++ b/Xiao.js @@ -130,6 +130,7 @@ client.on('clientReady', async () => { const isWearingHat = await client.redis.db.get('hat'); if (!isWearingHat) await client.redis.db.set('hat', false); client.avatarChanger.isWearingHat = isWearingHat === 'true'; + await client.avatarChanger.checkForUpdates(); client.avatarChanger.setInterval(); // Set up disabled commands diff --git a/package.json b/package.json index 9e484d2a..3d8fbd21 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "160.0.1", + "version": "160.0.2", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/structures/AvatarChanger.js b/structures/AvatarChanger.js index a43c2066..ca007df9 100644 --- a/structures/AvatarChanger.js +++ b/structures/AvatarChanger.js @@ -32,6 +32,28 @@ module.exports = class AvatarChanger { await this.client.user.setAvatar(image); } + async checkForUpdates() { + const today = new Date(); + const holiday = this.isHoliday(today); + if (holiday && !this.isWearingHat) { + let { hat } = holiday; + if (Array.isArray(hat)) hat = hat[Math.floor(Math.random() * hat.length)]; + try { + await this.setAvatar(hat); + this.client.logger.info(`[AVATAR] Updated avatar to ${hat}!`); + } catch (err) { + this.client.logger.error(`[AVATAR] Failed to update avatar.\n${err.stack}`); + } + } else if (this.isWearingHat && !holiday) { + try { + await this.setAvatar(); + this.client.logger.info('[AVATAR] Reset avatar to default.'); + } catch (err) { + this.client.logger.error(`[AVATAR] Failed to update avatar.\n${err.stack}`); + } + } + } + setInterval() { const now = new Date(); const midnight = new Date(Date.UTC( @@ -42,19 +64,7 @@ module.exports = class AvatarChanger { )); const msUntilNext = midnight - now; setTimeout(() => { - const today = new Date(); - const holiday = this.isHoliday(today); - if (holiday && !this.holiday) { - let { hat } = holiday; - if (Array.isArray(hat)) hat = hat[Math.floor(Math.random() * hat.length)]; - this.setAvatar(hat) - .then(() => this.client.logger.info(`[AVATAR] Updated avatar to ${hat}!`)) - .catch(err => this.client.logger.error(`[AVATAR] Failed to update avatar.\n${err.stack}`)); - } else if (this.isWearingHat) { - this.setAvatar() - .then(() => this.client.logger.info('[AVATAR] Reset avatar to default.')) - .catch(err => this.client.logger.error(`[AVATAR] Failed to update avatar.\n${err.stack}`)); - } + this.checkForUpdates(); this.setInterval(); }, msUntilNext); }