discord-voice
Advanced tools
Comparing version 2.0.3 to 2.0.4
@@ -12,3 +12,3 @@ { | ||
}, | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "A complete framework to facilitate the tracking of user voice time using discord.js", | ||
@@ -65,3 +65,3 @@ "main": "index.js", | ||
"@types/node": "^14.0.5", | ||
"discord.js": "^13.0.1", | ||
"discord.js": "^13.1.0", | ||
"eslint": "^7.6.0", | ||
@@ -68,0 +68,0 @@ "typescript": "^4.2.3" |
const merge = require("deepmerge"); | ||
const Discord = require("discord.js"); | ||
const serialize = require("serialize-javascript"); | ||
const lodash = require("lodash"); | ||
const { EventEmitter } = require("events"); | ||
@@ -68,2 +69,10 @@ const { ConfigOptions, ConfigData, ConfigEditOptions } = require("./Constants.js"); | ||
/** | ||
* The channels to track (if trackAllChannels is true this will be ignored). | ||
* @type {Snowflake[]} | ||
*/ | ||
get channelIds() { | ||
return this.options.channelIds || this.manager.options.default.channelIds; | ||
} | ||
/** | ||
* The min amount of users to be in a channel to be tracked (0 is equal to no limit). | ||
@@ -349,3 +358,3 @@ * @type {Number} | ||
if (exemptChannel) return false; | ||
if (!this.trackAllChannels && !this.channelIds.includes(channel.id)) return false; | ||
if (!this.trackAllChannels && !lodash._.includes(this.channelIds, channel.id)) return false; | ||
if (this.minUserCountToParticipate > 0 && channel.members.size < this.minUserCountToParticipate) return false; | ||
@@ -352,0 +361,0 @@ if (this.maxUserCountToParticipate > 0 && channel.members.size > this.maxUserCountToParticipate) return false; |
const { EventEmitter } = require("events"); | ||
const merge = require("deepmerge"); | ||
const { writeFile, readFile, exists } = require("fs"); | ||
const { promisify } = require("util"); | ||
const writeFileAsync = promisify(writeFile); | ||
const existsAsync = promisify(exists); | ||
const readFileAsync = promisify(readFile); | ||
const { writeFile, readFile, access } = require('fs/promises'); | ||
const serialize = require("serialize-javascript"); | ||
const lodash = require("lodash"); | ||
@@ -258,3 +255,6 @@ const { defaultVoiceManagerOptions, defaultUserOptions, defaultConfigOptions, VoiceManagerOptions, UserOptions, ConfigOptions, UserData, ConfigData, UserEditOptions, ConfigEditOptions } = require("./Constants.js"); | ||
async deleteUser(userId, guildId) { | ||
await writeFileAsync(this.options.userStorage, JSON.stringify(this.users.map((user) => user.data)), "utf-8"); | ||
await writeFile(this.options.userStorage, | ||
JSON.stringify(this.users.map((user) => user.data), (_, v) => typeof v === 'bigint' ? serialize(v) : v), | ||
'utf-8' | ||
); | ||
this.refreshUserStorage(); | ||
@@ -270,3 +270,10 @@ return; | ||
async deleteConfig(guildId) { | ||
await writeFileAsync(this.options.configStorage, JSON.stringify(this.configs.map((config) => config.data)), "utf-8"); | ||
await writeFile( | ||
this.options.configStorage, | ||
JSON.stringify( | ||
this.configs.map((config) => config.data), | ||
(_, v) => (typeof v === "bigint" ? serialize(v) : v) | ||
), | ||
'utf-8' | ||
); | ||
this.refreshConfigStorage(); | ||
@@ -297,3 +304,10 @@ return; | ||
async editUser(_userId, _guildId, _userData) { | ||
await writeFileAsync(this.options.userStorage, JSON.stringify(this.users.map((user) => user.data)), "utf-8"); | ||
await writeFile( | ||
this.options.userStorage, | ||
JSON.stringify( | ||
this.users.map((user) => user.data), | ||
(_, v) => (typeof v === "bigint" ? serialize(v) : v) | ||
), | ||
'utf-8' | ||
); | ||
this.refreshUserStorage(); | ||
@@ -309,3 +323,10 @@ return; | ||
async editConfig(_guildId, _configData) { | ||
await writeFileAsync(this.options.storage, JSON.stringify(this.configs.map((config) => config.data)), "utf-8"); | ||
await writeFile( | ||
this.options.storage, | ||
JSON.stringify( | ||
this.configs.map((config) => config.data), | ||
(_, v) => (typeof v === "bigint" ? serialize(v) : v) | ||
), | ||
'utf-8' | ||
); | ||
this.refreshConfigStorage(); | ||
@@ -322,3 +343,10 @@ return; | ||
async saveUser(userId, guildId, userData) { | ||
await writeFileAsync(this.options.userStorage, JSON.stringify(this.users.map((user) => user.data)), "utf-8"); | ||
await writeFile( | ||
this.options.userStorage, | ||
JSON.stringify( | ||
this.users.map((user) => user.data), | ||
(_, v) => (typeof v === "bigint" ? serialize(v) : v) | ||
), | ||
'utf-8' | ||
); | ||
this.refreshUserStorage(); | ||
@@ -334,3 +362,10 @@ return; | ||
async saveConfig(guildId, configData) { | ||
await writeFileAsync(this.options.configStorage, JSON.stringify(this.configs.map((config) => config.data)), "utf-8"); | ||
await writeFile( | ||
this.options.configStorage, | ||
JSON.stringify( | ||
this.configs.map((config) => config.data), | ||
(_, v) => (typeof v === "bigint" ? serialize(v) : v) | ||
), | ||
'utf-8' | ||
); | ||
this.refreshConfigStorage(); | ||
@@ -345,8 +380,10 @@ return; | ||
async getAllUsers() { | ||
const storageExists = await existsAsync(this.options.userStorage); | ||
const storageExists = await access(this.options.userStorage) | ||
.then(() => true) | ||
.catch(() => false); | ||
if (!storageExists) { | ||
await writeFileAsync(this.options.userStorage, "[]", "utf-8"); | ||
await writeFile(this.options.userStorage, '[]', 'utf-8'); | ||
return []; | ||
} else { | ||
const storageContent = await readFileAsync(this.options.userStorage); | ||
const storageContent = await readFile(this.options.userStorage, (_, v) => (typeof v === "string" && /BigInt\("(-?\d+)"\)/.test(v) ? eval(v) : v)); | ||
try { | ||
@@ -360,8 +397,6 @@ const users = await JSON.parse(storageContent.toString()); | ||
} | ||
} catch (e) { | ||
if (e.message === "Unexpected end of JSON input") { | ||
} catch (err) { | ||
if (err.message === "Unexpected end of JSON input") { | ||
throw new SyntaxError("The storage file is not properly formatted (Unexpected end of JSON input)."); | ||
} else { | ||
throw e; | ||
} | ||
} else throw err; | ||
} | ||
@@ -376,8 +411,10 @@ } | ||
async getAllConfigs() { | ||
const storageExists = await existsAsync(this.options.configStorage); | ||
const storageExists = await access(this.options.configStorage) | ||
.then(() => true) | ||
.catch(() => false); | ||
if (!storageExists) { | ||
await writeFileAsync(this.options.configStorage, "[]", "utf-8"); | ||
await writeFile(this.options.configStorage, '[]', 'utf-8'); | ||
return []; | ||
} else { | ||
const storageContent = await readFileAsync(this.options.configStorage); | ||
const storageContent = await readFile(this.options.configStorage, (_, v) => (typeof v === "string" && /BigInt\("(-?\d+)"\)/.test(v) ? eval(v) : v)); | ||
try { | ||
@@ -391,8 +428,6 @@ const configs = await JSON.parse(storageContent.toString()); | ||
} | ||
} catch (e) { | ||
if (e.message === "Unexpected end of JSON input") { | ||
} catch (err) { | ||
if (err.message === "Unexpected end of JSON input") { | ||
throw new SyntaxError("The storage file is not properly formatted (Unexpected end of JSON input)."); | ||
} else { | ||
throw e; | ||
} | ||
} else throw err; | ||
} | ||
@@ -414,3 +449,3 @@ } | ||
} | ||
if (!(await config.checkMember(user.member)) || !(await config.checkChannel(user.channel))) return; | ||
if (!((await config.checkMember(user.member)) && (await config.checkChannel(user.channel)))) return; | ||
const oldUser = lodash._.cloneDeep(user); | ||
@@ -478,9 +513,9 @@ if (config.voiceTimeTrackingEnabled) { | ||
*/ | ||
async _checkUser(member) { | ||
let config = this.configs.find((g) => g.guildId === member.guild.id); | ||
async _checkUser(memberAndChannel) { | ||
let config = this.configs.find((g) => g.guildId === memberAndChannel.member.guild.id); | ||
if (!config) { | ||
config = await this.createConfig(member.guild.id); | ||
config = await this.createConfig(memberAndChannel.member.guild.id); | ||
} | ||
if (!(await config.checkMember(member)) || !(await config.checkChannel(member.channel))) return false; | ||
else return await this.createUser(member.id, member.guild.id); | ||
if (!((await config.checkMember(memberAndChannel.member)) && (await config.checkChannel(memberAndChannel.channel)))) return false; | ||
else return await this.createUser(memberAndChannel.member.id, memberAndChannel.member.guild.id); | ||
} | ||
@@ -499,3 +534,3 @@ /** | ||
} | ||
if (!(await config.checkMember(newState.member)) || !(await config.checkChannel(newState.channel))) return; | ||
if (!((await config.checkMember(newState.member)) && (await config.checkChannel(newState.channel)))) return; | ||
else return await this.createUser(newState.member.id, newState.member.guild.id); | ||
@@ -502,0 +537,0 @@ } |
@@ -80,3 +80,3 @@ const merge = require("deepmerge"); | ||
return this.guild.channels.cache | ||
.filter((c) => c.type == "voice" || c.type == "GUILD_VOICE") | ||
.filter((c) => c.type === "voice" || c.type === "GUILD_VOICE" || c.type === "GUILD_STAGE_VOICE") | ||
.map((voicechannel) => { | ||
@@ -86,3 +86,3 @@ return voicechannel.members | ||
if (!this.manager.users.find((u) => u.userId === x.id)) { | ||
this.manager._checkUser(x); | ||
this.manager._checkUser({ channel: voicechannel, member: x }); | ||
} | ||
@@ -89,0 +89,0 @@ if (x.id === this.userId) return { channel: voicechannel, member: x }; |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
85647
1476
12