Compare commits

..

1 Commits

Author SHA1 Message Date
d3cbd29482 rotate avatars from a folder with fs watch 2026-03-09 14:27:44 -04:00
2 changed files with 7 additions and 9 deletions

View File

@@ -80,6 +80,7 @@ client.on(Events.MessageCreate, message => {
if (regexProfile) { if (regexProfile) {
const cocklink = convertURL(regexProfile[0], twitterRegex, "girlcockx.com") const cocklink = convertURL(regexProfile[0], twitterRegex, "girlcockx.com")
message.channel.send({ content: cocklink, flags: MessageFlags.SuppressNotifications, components: [swapRow] }) message.channel.send({ content: cocklink, flags: MessageFlags.SuppressNotifications, components: [swapRow] })
.catch(err => console.error("Failed to send converted link: " + (err.message || err)))
message.suppressEmbeds().catch(err => message.suppressEmbeds().catch(err =>
// this next bit just cuts down the error to the important part, which will usually end up being "no permissions" // this next bit just cuts down the error to the important part, which will usually end up being "no permissions"
console.error("Removing original embed failed: " + err.stack?.split('\n')[0] || err.message || String(err).split('\n')[0]) console.error("Removing original embed failed: " + err.stack?.split('\n')[0] || err.message || String(err).split('\n')[0])

View File

@@ -5,7 +5,6 @@ const config = require('../config.json');
const AVATAR_DIR = path.join(__dirname, '../avatars'); const AVATAR_DIR = path.join(__dirname, '../avatars');
let avatarFiles = []; let avatarFiles = [];
let currentAvatarIndex = 0; let currentAvatarIndex = 0;
let timer = null;
function loadAvatars() { function loadAvatars() {
try { try {
@@ -31,17 +30,19 @@ function loadAvatars() {
} }
} }
function rotateAvatar(client) { async function rotateAvatar(client) {
if (avatarFiles.length === 0) return; if (avatarFiles.length === 0) return;
const avatarFile = avatarFiles[currentAvatarIndex]; const avatarFile = avatarFiles[currentAvatarIndex];
const avatarPath = path.join(AVATAR_DIR, avatarFile); const avatarPath = path.join(AVATAR_DIR, avatarFile);
try { try {
client.user.setAvatar(avatarPath); await client.user.setAvatar(avatarPath);
console.log(`Avatars: Changed avatar to ${avatarFile}`); console.log(`Avatars: Changed avatar to ${avatarFile}`);
} catch (err) { } catch (err) {
console.error(`Avatars: Failed to set avatar to ${avatarFile}:`, err); console.error(`Avatars: Failed to set avatar to ${avatarFile}:`, err.message || err);
} finally { // discordjs is evil and deletes the token every time the avatar changes
client.rest.setToken(config.token);
} }
currentAvatarIndex = (currentAvatarIndex + 1) % avatarFiles.length; currentAvatarIndex = (currentAvatarIndex + 1) % avatarFiles.length;
@@ -50,7 +51,6 @@ function rotateAvatar(client) {
function init(client) { function init(client) {
loadAvatars(); loadAvatars();
// Watch for changes in the avatars directory
let fsWait = null; let fsWait = null;
fs.watch(AVATAR_DIR, (eventType, filename) => { fs.watch(AVATAR_DIR, (eventType, filename) => {
if (filename) { if (filename) {
@@ -67,11 +67,8 @@ function init(client) {
console.log(`Avatars: Starting avatar rotation every ${intervalMinutes} minutes`); console.log(`Avatars: Starting avatar rotation every ${intervalMinutes} minutes`);
// Initial rotation
rotateAvatar(client); rotateAvatar(client);
setInterval(() => rotateAvatar(client), intervalMs);
// make it wait every interval before rotating
timer = setInterval(() => rotateAvatar(client), intervalMs);
} }
module.exports = { init }; module.exports = { init };