From 8d340db915fc65d55a6383e047a80c6b27535f88 Mon Sep 17 00:00:00 2001 From: Kira Date: Sat, 14 Feb 2026 02:45:39 -0500 Subject: [PATCH] rotate avatars from a folder with fs watch --- .dockerignore | 3 +- .gitignore | 3 +- config.json.default | 1 + docker-compose.yml.default | 3 +- index.js | 2 + lib/avatarRotation.js | 77 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 lib/avatarRotation.js diff --git a/.dockerignore b/.dockerignore index 204bd2e..bd36d2c 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,4 @@ node_modules .git -config.json \ No newline at end of file +config.json +avatars \ No newline at end of file diff --git a/.gitignore b/.gitignore index 68fb955..a39ff5d 100644 --- a/.gitignore +++ b/.gitignore @@ -141,4 +141,5 @@ dist *.code-workspace config.json -state.json \ No newline at end of file +state.json +avatars \ No newline at end of file diff --git a/config.json.default b/config.json.default index 85cef1d..b4065ec 100644 --- a/config.json.default +++ b/config.json.default @@ -1,6 +1,7 @@ { "token": "bot token go here", "status": "looking for x.com links", + "avatarInterval": 15, "parentsAndOrGuardians": [ "230659159450845195", "297983197990354944" diff --git a/docker-compose.yml.default b/docker-compose.yml.default index 0a5edd5..92d1d49 100644 --- a/docker-compose.yml.default +++ b/docker-compose.yml.default @@ -3,4 +3,5 @@ services: restart: unless-stopped build: . volumes: - - ./config.json:/usr/src/bot/config.json \ No newline at end of file + - ./config.json:/usr/src/bot/config.json + - ./avatars:/usr/src/bot/avatars \ No newline at end of file diff --git a/index.js b/index.js index 48affec..255a1e2 100644 --- a/index.js +++ b/index.js @@ -68,6 +68,8 @@ client.on(Events.InteractionCreate, async interaction => { client.once(Events.ClientReady, readyClient => { console.log(`Discord: Connected as ${readyClient.user.tag}`); client.user.setActivity(config.status, { type: ActivityType.Custom }); + const avatarRotation = require('./lib/avatarRotation'); + avatarRotation.init(client); }); client.login(config.token); diff --git a/lib/avatarRotation.js b/lib/avatarRotation.js new file mode 100644 index 0000000..8f30179 --- /dev/null +++ b/lib/avatarRotation.js @@ -0,0 +1,77 @@ +const fs = require('fs'); +const path = require('path'); +const config = require('../config.json'); + +const AVATAR_DIR = path.join(__dirname, '../avatars'); +let avatarFiles = []; +let currentAvatarIndex = 0; +let timer = null; + +function loadAvatars() { + try { + if (!fs.existsSync(AVATAR_DIR)) { + fs.mkdirSync(AVATAR_DIR); + console.log('Avatars: Created avatars directory'); + return; + } + + const files = fs.readdirSync(AVATAR_DIR); + avatarFiles = files.filter(file => /\.(jpg|jpeg|png|gif|webp)$/i.test(file)); + + // shuffle + for (let i = avatarFiles.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [avatarFiles[i], avatarFiles[j]] = [avatarFiles[j], avatarFiles[i]]; + } + + console.log(`Avatars: Loaded ${avatarFiles.length} avatars`); + currentAvatarIndex = 0; // Reset index on reload + } catch (err) { + console.error('Avatars: Error loading avatars:', err); + } +} + +function rotateAvatar(client) { + if (avatarFiles.length === 0) return; + + const avatarFile = avatarFiles[currentAvatarIndex]; + const avatarPath = path.join(AVATAR_DIR, avatarFile); + + try { + client.user.setAvatar(avatarPath); + console.log(`Avatars: Changed avatar to ${avatarFile}`); + } catch (err) { + console.error(`Avatars: Failed to set avatar to ${avatarFile}:`, err); + } + + currentAvatarIndex = (currentAvatarIndex + 1) % avatarFiles.length; +} + +function init(client) { + loadAvatars(); + + // Watch for changes in the avatars directory + let fsWait = null; + fs.watch(AVATAR_DIR, (eventType, filename) => { + if (filename) { + if (fsWait) clearTimeout(fsWait); + fsWait = setTimeout(() => { + console.log(`Avatars: Detected change in avatars directory`); + loadAvatars(); + }, 1000); + } + }); + + const intervalMinutes = config.avatarInterval || 15; + const intervalMs = intervalMinutes * 60 * 1000; + + console.log(`Avatars: Starting avatar rotation every ${intervalMinutes} minutes`); + + // Initial rotation + rotateAvatar(client); + + // make it wait every interval before rotating + timer = setInterval(() => rotateAvatar(client), intervalMs); +} + +module.exports = { init };