|
| 1 | +const fs = require("fs"); |
| 2 | + |
| 3 | +const DISBOARD_ID = "302050872383242240"; |
| 4 | +const BUMP_COOLDOWN = 2 * 60 * 60 * 1000; // 2 hours in ms |
| 5 | +const CONFIG_FILE = "./bump-config.json"; |
| 6 | + |
| 7 | +// In-memory store: guildId -> { channelId, roleId, timer } |
| 8 | +const guildConfig = new Map(); |
| 9 | + |
| 10 | +// ─── Persistence ──────────────────────────────────────────────────────────── |
| 11 | + |
| 12 | +function loadConfig() { |
| 13 | + if (!fs.existsSync(CONFIG_FILE)) return; |
| 14 | + try { |
| 15 | + const data = JSON.parse(fs.readFileSync(CONFIG_FILE, "utf8")); |
| 16 | + for (const [guildId, config] of Object.entries(data)) { |
| 17 | + guildConfig.set(guildId, { ...config, timer: null }); |
| 18 | + } |
| 19 | + console.log(`[BumpReminder] Loaded config for ${guildConfig.size} guild(s).`); |
| 20 | + } catch (err) { |
| 21 | + console.error("[BumpReminder] Failed to load config:", err); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function saveConfig() { |
| 26 | + const data = {}; |
| 27 | + for (const [guildId, config] of guildConfig.entries()) { |
| 28 | + data[guildId] = { channelId: config.channelId, roleId: config.roleId }; |
| 29 | + } |
| 30 | + fs.writeFileSync(CONFIG_FILE, JSON.stringify(data, null, 2)); |
| 31 | +} |
| 32 | + |
| 33 | +// ─── Setup ────────────────────────────────────────────────────────────────── |
| 34 | + |
| 35 | +function setupBumpReminder(guildId, channelId, roleId) { |
| 36 | + const existing = guildConfig.get(guildId); |
| 37 | + if (existing?.timer) clearTimeout(existing.timer); |
| 38 | + |
| 39 | + guildConfig.set(guildId, { channelId, roleId, timer: null }); |
| 40 | + saveConfig(); |
| 41 | +} |
| 42 | + |
| 43 | +function removeBumpReminder(guildId) { |
| 44 | + const existing = guildConfig.get(guildId); |
| 45 | + if (existing?.timer) clearTimeout(existing.timer); |
| 46 | + guildConfig.delete(guildId); |
| 47 | + saveConfig(); |
| 48 | +} |
| 49 | + |
| 50 | +function getConfig(guildId) { |
| 51 | + return guildConfig.get(guildId) ?? null; |
| 52 | +} |
| 53 | + |
| 54 | +// ─── Message Handler ───────────────────────────────────────────────────────── |
| 55 | + |
| 56 | +async function handleMessage(message) { |
| 57 | + // Only care about DISBOARD messages in guilds |
| 58 | + if (message.author.id !== DISBOARD_ID) return; |
| 59 | + if (!message.guild) return; |
| 60 | + if (!message.embeds.length) return; |
| 61 | + |
| 62 | + const embed = message.embeds[0]; |
| 63 | + const isBumpSuccess = embed.description?.includes("Bump done"); |
| 64 | + if (!isBumpSuccess) return; |
| 65 | + |
| 66 | + const config = guildConfig.get(message.guild.id); |
| 67 | + if (!config) return; |
| 68 | + |
| 69 | + // Clear any old pending timer |
| 70 | + if (config.timer) clearTimeout(config.timer); |
| 71 | + |
| 72 | + // Send confirmation |
| 73 | + const channel = message.guild.channels.cache.get(config.channelId); |
| 74 | + if (!channel) return; |
| 75 | + |
| 76 | + await channel |
| 77 | + .send({ |
| 78 | + embeds: [ |
| 79 | + { |
| 80 | + color: 0x57f287, // green |
| 81 | + description: "✅ **Server bumped!** I'll remind you when it's time to bump again.", |
| 82 | + footer: { text: "Reminder in 2 hours" }, |
| 83 | + timestamp: new Date(Date.now() + BUMP_COOLDOWN).toISOString(), |
| 84 | + }, |
| 85 | + ], |
| 86 | + }) |
| 87 | + .catch(() => {}); |
| 88 | + |
| 89 | + // Schedule reminder |
| 90 | + config.timer = setTimeout(async () => { |
| 91 | + const rolePing = config.roleId ? `<@&${config.roleId}>` : ""; |
| 92 | + await channel |
| 93 | + .send({ |
| 94 | + content: rolePing || null, |
| 95 | + embeds: [ |
| 96 | + { |
| 97 | + color: 0xfee75c, // yellow |
| 98 | + title: "⏰ Time to bump the server!", |
| 99 | + description: "Run </bump:947088344167366698> to keep us at the top of **DISBOARD**.", |
| 100 | + footer: { text: "Bump now to bring in new members!" }, |
| 101 | + }, |
| 102 | + ], |
| 103 | + }) |
| 104 | + .catch(() => {}); |
| 105 | + |
| 106 | + config.timer = null; |
| 107 | + }, BUMP_COOLDOWN); |
| 108 | +} |
| 109 | + |
| 110 | +module.exports = { loadConfig, setupBumpReminder, removeBumpReminder, getConfig, handleMessage }; |
0 commit comments