From 53c8ef01301e05bb920913a545a0c7917a7064f1 Mon Sep 17 00:00:00 2001 From: en3sis <940170+en3sis@users.noreply.github.com> Date: Tue, 6 Jun 2023 21:17:57 +0200 Subject: [PATCH] feat: persists bot presence, won't rewrite on restart, real time update --- src/controllers/bot/config.controller.ts | 21 +++++++++++++-------- src/events/ready.ts | 14 ++++++-------- src/realtime/presence.realtime.ts | 19 +++++++++++++++++++ 3 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 src/realtime/presence.realtime.ts diff --git a/src/controllers/bot/config.controller.ts b/src/controllers/bot/config.controller.ts index c17940c..7101ecd 100644 --- a/src/controllers/bot/config.controller.ts +++ b/src/controllers/bot/config.controller.ts @@ -1,3 +1,5 @@ +import { ActivityType } from 'discord.js' +import { Hans } from '../..' import supabase from '../../libs/supabase' import { pluginsList } from '../../models/plugins.model' import { Database } from '../../types/database.types' @@ -9,13 +11,6 @@ export type BotConfig = Database['public']['Tables']['configs']['Row'] */ export const insertConfiguration = async () => { try { - const activityName = !!process.env.ISDEV - ? { - activity_type: 3, - activity_name: 'you', - } - : {} - const { data, error } = await supabase .from('configs') .upsert( @@ -31,7 +26,6 @@ export const insertConfiguration = async () => { notify_channel_id: '905157473671975002', // Send notifications when the bot is online to this channel. perma_invite: 'https://discord.com/invite/sMmbbSefwH', website: 'https://github.com/en3sis/hans', - ...activityName, }, { onConflict: 'id' }, ) @@ -120,3 +114,14 @@ export const insertPlugins = async () => { console.log('❌ ERROR: insertPlugins(): ', error) } } + +export const setPresence = async (type: ActivityType, text: string): Promise => { + Hans.user.setPresence({ + activities: [ + { + type: type || Number(3), + name: text || 'you', + }, + ], + }) +} diff --git a/src/events/ready.ts b/src/events/ready.ts index b9b987d..155150e 100644 --- a/src/events/ready.ts +++ b/src/events/ready.ts @@ -3,10 +3,12 @@ import { getBotConfiguration, insertConfiguration, insertPlugins, + setPresence, } from '../controllers/bot/config.controller' import { insertAllGuilds } from '../controllers/bot/guilds.controller' import { notifyPulse } from '../controllers/events/ready.controller' import { CronJobsTasks } from '../controllers/tasks/cron-jobs' +import { configsRealtime } from '../realtime/presence.realtime' import { reportErrorToMonitoring } from '../utils/monitoring' module.exports = { @@ -34,14 +36,10 @@ module.exports = { // Init all cron jobs tasks await CronJobsTasks(Hans) - Hans.user.setPresence({ - activities: [ - { - type: Hans.settings.activity_type || 3, - name: Hans.settings.activity_name || 'you', - }, - ], - }) + // Set the bot presence to the default one. + await setPresence(Hans.settings?.activity_type ?? 3, Hans.settings?.activity_name ?? 'you') + + configsRealtime() } catch (error) { console.log('❌ ERROR: ready(): ', error) diff --git a/src/realtime/presence.realtime.ts b/src/realtime/presence.realtime.ts new file mode 100644 index 0000000..e1cfb62 --- /dev/null +++ b/src/realtime/presence.realtime.ts @@ -0,0 +1,19 @@ +import { setPresence } from '../controllers/bot/config.controller' +import supabase from '../libs/supabase' + +export const configsRealtime = () => { + return supabase + .channel('table-db-changes') + .on( + 'postgres_changes', + { + event: 'UPDATE', + schema: 'public', + table: 'configs', + }, + async (payload) => { + await setPresence(payload.new.activity_type, payload.new.activity_name) + }, + ) + .subscribe() +}