Skip to content

Commit

Permalink
Quick jab at persistence in util/discord for server preferences, shou…
Browse files Browse the repository at this point in the history
…ld refactor to a dao layer soon
  • Loading branch information
jakethedev committed May 25, 2021
1 parent 1a1d6a0 commit ec1c576
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions util/discord.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
const fs = require('fs')
const _cacheFile = 'serverprefs.json'
const debug = msg => console.log(`sprefs: ${msg}`)

// Public constants for main bot.js process to watch for
const LISTEN_KEY = `heylisten`
const DEAFEN_KEY = `begonebot`

// Live cache for fast access
let serverPrefs = {}

// Load old cache on init
try {
serverPrefs = JSON.parse(fs.readFileSync(_cacheFile))
} catch (error) {
debug(`if ENOENT this is totally ok - could not load JSON from old cache: ${error}`)
serverPrefs = {}
}

// Simple persistence layer
function _savePreferences(){
try {
fs.writeFileSync(_cacheFile, JSON.stringify(serverPrefs, null, 2))
} catch(error) {
debug(`error saving cache: ${error}`)
}
}

// Relies on discord permission scheme: https://discord.com/developers/docs/topics/permissions
exports.isPowerfulMember = function(discordJsMsg){
const isAdmin = discordJsMsg.member.permissions.any(['ADMINISTRATOR', 'MANAGE_CHANNELS'])
Expand Down

0 comments on commit ec1c576

Please sign in to comment.