Skip to content

Commit

Permalink
Merge pull request #58 from en3sis/feat/persist-bot-presence
Browse files Browse the repository at this point in the history
feat: persists bot presence, won't rewrite on restart, real time update
  • Loading branch information
en3sis authored Jun 6, 2023
2 parents e6ab7b1 + 53c8ef0 commit fd05f1e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
21 changes: 13 additions & 8 deletions src/controllers/bot/config.controller.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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(
Expand All @@ -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' },
)
Expand Down Expand Up @@ -120,3 +114,14 @@ export const insertPlugins = async () => {
console.log('❌ ERROR: insertPlugins(): ', error)
}
}

export const setPresence = async (type: ActivityType, text: string): Promise<void> => {
Hans.user.setPresence({
activities: [
{
type: type || Number(3),
name: text || 'you',
},
],
})
}
14 changes: 6 additions & 8 deletions src/events/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)

Expand Down
19 changes: 19 additions & 0 deletions src/realtime/presence.realtime.ts
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit fd05f1e

Please sign in to comment.