// Debug const { DebugBuilder } = require("../utilities/debugBuilder"); const log = new DebugBuilder("server", "utils"); // Convert a JSON object to a buffer for the DB exports.JsonToBuffer = (jsonObject) => { return Buffer.from(JSON.stringify(jsonObject)) } // Convert a buffer from the DB to JSON object exports.BufferToJson = (buffer) => { return JSON.parse(buffer.toString()); } /** * **DISUSED** * * @param {string} presetName The present name to sanitize * @returns {string} The sanitized preset name to be used elsewhere */ exports.SanitizePresetName = (presetName) => { return String(presetName).toLowerCase().replace(/[\W_]+/g,"-") } /** * Get online, offline and total members in a guild. Optionally a group can be specified to get members' statuses. * * @param interaction Discord interaction object * @param param0.roleName {OPTIONAL} The role name to check the members in; Defaults to 'Bots' */ exports.getMembersInRole = async (interaction, roleName = "Bots" ) => { log.DEBUG("Fetching all members"); await interaction.guild.members.fetch() //cache all members in the server const role = await interaction.guild.roles.cache.find(role => role.name === roleName); //the role to check log.DEBUG("Role to check members from: ", role); log.DEBUG("Members of role: ", role.members); // This is not working, can't get the status of the users, rest of join is untested const onlineMembers = await role.members.filter(member => member.voice.channel !== null); const offlineMembers = await role.members.filter(member => member.voice.channel === null); const allMembers = await role.members; log.VERBOSE("All members: ", allMembers, onlineMembers, offlineMembers) return { 'online': onlineMembers, 'offline': offlineMembers, 'all': allMembers } } /** Find a key in an object by its value * * @param {*} object The object to search * @param {*} value The value to search the arrays in the object for * @returns The key of the object that contains the value */ exports.getKeyByArrayValue = (object, value) => { return Object.keys(object).find(key => object[key].includes(value)); } /** * Check to see if the input is a valid JSON string * * @param {*} str The string to check for valud JSON * @returns {true|false} */ exports.isJsonString = (str) => { try { JSON.parse(str); } catch (e) { return false; } return true; }