Skip to content

Add banUser & unbanUser #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,43 @@ module.exports = (config) => {
}
} catch (err) {
}
},
async banUser (username, remove = false) {
const user = await db.users.findOne({ username: username })
if (!user) {
return `Can't find user "${username}"`
}

const _id = user._id
if (!remove) {
if (user.banned) {
return `User "${username}" ${_id} is already banned.`
}
await db.users.update({ _id }, { $set: { active: 0, banned: true } })
console.log(`Suspended user "${username}" ${_id}`)
return `Suspended user "${username}" ${_id}`
} else {
await utils.respawnUser(_id)
// Remove the bot user from the database
await db.users.removeWhere({ _id })
await db['users.code'].removeWhere({ user: _id })
await env.del(env.keys.MEMORY + _id)
await env.del(env.keys.MEMORY_SEGMENTS + _id)
console.log(`Removed user "${username}" ${_id}`)
return `Removed user "${username}" ${_id}`
}
},
async unbanUser (username) {
const user = await db.users.findOne({ username: username })
if (!user) {
return `Can't find user "${username}"`
} else if (user.active !== 0) {
return `User "${username}" ${user._id} is not banned.`
}

await db.users.update({ _id: user._id }, { $set: { active: 10000, banned: false } })
console.log(`Unbanned user "${username}" ${user._id}`)
return `Unbanned user "${username}" ${user._id}`
}
})

Expand All @@ -157,6 +194,9 @@ module.exports = (config) => {
config.utils.setSocketUpdateRate._help = 'setSocketUpdateRate(value) Sets socket update rate (in ms)'
config.utils.getSocketUpdateRate._help = 'getSocketUpdateRate() Returns current socket update rate'
config.utils.setShardName._help = 'setShardName(value) Sets the shard name'
config.utils.banUser._help = 'banUser(username, remove = false) Ban the specified user from the server.\n' +
'\tPassing `false` will suspend their CPU usage, `true` will delete their data entirely.'
config.utils.unbanUser._help = 'unbanUser(username) Unban the specified user from the server.'

Object.defineProperty(config.utils, '_help', {
get () { // Using a getter here so that loaded services are also included
Expand Down
2 changes: 1 addition & 1 deletion lib/services/whitelist/cronjobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = (config) => {
const WHITELIST = JSON.parse(await env.get(env.keys.WHITELIST) || '[]').map(u => u.toLowerCase())
const users = await db.users.find()
for (const user of users) {
const blocked = !(user.allowed || WHITELIST.length === 0 || (user.username && WHITELIST.includes(user.username.toLowerCase())))
const blocked = !(user.allowed || !user.banned || WHITELIST.length === 0 || (user.username && WHITELIST.includes(user.username.toLowerCase())))
db.users.update({ _id: user._id }, { $set: { blocked } })
}
}]
Expand Down