Skip to content

Commit

Permalink
Stub the code in util/discord for server prefs work (#15, #30, #31, #49
Browse files Browse the repository at this point in the history
…). One more focused session should finish all this up
  • Loading branch information
jakethedev committed May 26, 2021
1 parent ec1c576 commit 59d8149
Showing 1 changed file with 68 additions and 4 deletions.
72 changes: 68 additions & 4 deletions util/discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ 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`
const DEFAULT_CHANNELS = [
"bot",
"botspam",
"battles",
"beat-challenge-submissions",
"beat-battle",
"submissions"
]

// Live cache for fast access
let serverPrefs = {}
Expand All @@ -17,20 +25,76 @@ try {
serverPrefs = {}
}

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

function _initServerPrefs(serverKey){
//TODO verify this looks good
serverPrefs[serverKey] = {
'name': serverName?,
'botmodroles': [],
'channels': {}
}
for (let chanName in DEFAULT_CHANNELS){
if (chanName in server){
serverPrefs[serverKey]['channels'][CHANNELID] = {
'votemax': 3,
'rules': 'No rules saved for this channel yet',
'winners': []
}
}
}
}

function _getServerData(discordJsMsg){
//TODO this is very broken
const serverKey = discordJsMsg.server
if (!discordJsMsg in serverPrefs){
_initServer(serverKey)
}
return serverPrefs[serverKey]
}

// Splitting this out for cleaner code
function _authorHasBotModRole(discordJsMsg){
// TODO #15 and #49
const prefs = _getServerData(discordJsMsg.server)
for (let modRole of prefs['botmodroles']) {
if (discordJsMsg.author.roles.includes(modRole)) {
return true
}
}
return false
}

exports.updateBotModRoles = function(input, msg) {
// TODO add and remove logic #49
}

exports.listenToChannel = function(input, msg) {
// TODO #30
}

exports.stopListeningToChannel = function(input, msg) {
// TODO #31
}

// 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'])
//TODO: const hasBotRole = msg.member.roles.any()
return isAdmin // || hasBotRole
// Channel managers and admins get highest priority
if (discordJsMsg.member.permissions.any(['ADMINISTRATOR', 'MANAGE_CHANNELS'])) {
return true
}
if (_authorHasBotModRole(discordJsMsg)){
return true
}
return false
}

exports.getRoleFromMessageGuild = function(roleinput, message){
Expand Down

0 comments on commit 59d8149

Please sign in to comment.