Added New Db Stuff
This commit is contained in:
76
events/ready/db-status.js
Normal file
76
events/ready/db-status.js
Normal file
@ -0,0 +1,76 @@
|
||||
const { Pool } = require('pg');
|
||||
|
||||
const cache = {
|
||||
global: [],
|
||||
byGuild: new Map(), // guildId -> rows[]
|
||||
ready: false
|
||||
};
|
||||
|
||||
function inWindow(row, now = new Date()) {
|
||||
if (row.starts_at && now < new Date(row.starts_at)) return false;
|
||||
if (row.ends_at && now > new Date(row.ends_at)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function pickWeighted(list) {
|
||||
if (!list.length) return null;
|
||||
const total = list.reduce((a, r) => a + (r.weight || 1), 0);
|
||||
let rnd = Math.random() * total;
|
||||
for (const r of list) {
|
||||
rnd -= (r.weight || 1);
|
||||
if (rnd <= 0) return r;
|
||||
}
|
||||
return list[0];
|
||||
}
|
||||
|
||||
async function loadAll(pool) {
|
||||
const { rows } = await pool.query(
|
||||
`SELECT id, name, activity_type, enabled, weight, scope, guild_id, starts_at, ends_at
|
||||
FROM bot_statuses
|
||||
WHERE enabled = TRUE`
|
||||
);
|
||||
|
||||
const now = new Date();
|
||||
cache.global = rows.filter(r => r.scope === 'global' && inWindow(r, now));
|
||||
cache.byGuild.clear();
|
||||
for (const r of rows) {
|
||||
if (r.scope === 'guild' && inWindow(r, now) && r.guild_id) {
|
||||
const arr = cache.byGuild.get(r.guild_id) || [];
|
||||
arr.push(r);
|
||||
cache.byGuild.set(r.guild_id, arr);
|
||||
}
|
||||
}
|
||||
cache.ready = true;
|
||||
}
|
||||
|
||||
async function initStatusCache(pool) {
|
||||
await loadAll(pool);
|
||||
|
||||
// Live refresh when table changes (if you created the trigger)
|
||||
const client = await pool.connect();
|
||||
await client.query('LISTEN status_change');
|
||||
client.on('notification', async () => {
|
||||
try {
|
||||
await loadAll(pool);
|
||||
console.log('[status] cache refreshed via NOTIFY');
|
||||
} catch (e) {
|
||||
console.error('[status] refresh error:', e);
|
||||
}
|
||||
});
|
||||
|
||||
// Safety: periodic refresh in case NOTIFY is missed
|
||||
setInterval(() => loadAll(pool).catch(() => {}), 5 * 60 * 1000);
|
||||
}
|
||||
|
||||
function getNextStatus(guildId) {
|
||||
const rows = (guildId && cache.byGuild.get(guildId))?.length
|
||||
? cache.byGuild.get(guildId)
|
||||
: cache.global;
|
||||
|
||||
return pickWeighted(rows || []);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
initStatusCache,
|
||||
getNextStatus
|
||||
};
|
||||
@ -1,41 +1,29 @@
|
||||
const { Client, Guild, Events, ActivityType, PresenceUpdateStatus } = require('discord.js');
|
||||
const { ActivityType, Events } = require('discord.js');
|
||||
const { getNextStatus } = require('./db-status');
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Client} client
|
||||
* @param {Guild} guild
|
||||
*/
|
||||
function formatStatusName(template, client) {
|
||||
return String(template || '')
|
||||
.replace(/\{guilds\}/g, String(client.guilds.cache.size));
|
||||
// Add more tokens as you like, e.g. {users}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
name: Events.ClientReady,
|
||||
async execute(client) {
|
||||
const guildsCount = client.guilds.cache.size;
|
||||
name: Events.ClientReady,
|
||||
async execute(client) {
|
||||
//console.log(`Ready as ${client.user.tag}`);
|
||||
|
||||
let status = [
|
||||
{
|
||||
name: "I am Live",
|
||||
type: ActivityType.Watching,
|
||||
},
|
||||
{
|
||||
name: "Choo Choo 🚂",
|
||||
},
|
||||
{
|
||||
name: 'Hippity Hoppity',
|
||||
},
|
||||
{
|
||||
name: `I am in ${guildsCount} Server(s)`,
|
||||
},
|
||||
{
|
||||
name: 'Yippe 🐻'
|
||||
},
|
||||
];
|
||||
setInterval(() => {
|
||||
// Global rotation (you can also do per-guild if you want)
|
||||
const row = getNextStatus(); // or per-guild: getNextStatus(someGuildId)
|
||||
if (!row) return;
|
||||
|
||||
|
||||
|
||||
|
||||
setInterval(() => {
|
||||
let random = Math.floor(Math.random() * status.length);
|
||||
client.user.setActivity(status[random]);
|
||||
}, 30000);
|
||||
}
|
||||
};
|
||||
const name = formatStatusName(row.name, client);
|
||||
try {
|
||||
client.user.setActivity({ name, type: row.activity_type ?? ActivityType.Watching });
|
||||
console.log(row)
|
||||
} catch (e) {
|
||||
console.error('setActivity error:', e);
|
||||
}
|
||||
}, 30_000);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user