Skip to content

Commit 3686ade

Browse files
authored
Merge pull request #42 from tiennou/feature/user-ban
Add banUser & unbanUser
2 parents 934705c + 1ce80d1 commit 3686ade

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/common.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,43 @@ module.exports = (config) => {
146146
}
147147
} catch (err) {
148148
}
149+
},
150+
async banUser (username, remove = false) {
151+
const user = await db.users.findOne({ username: username })
152+
if (!user) {
153+
return `Can't find user "${username}"`
154+
}
155+
156+
const _id = user._id
157+
if (!remove) {
158+
if (user.banned) {
159+
return `User "${username}" ${_id} is already banned.`
160+
}
161+
await db.users.update({ _id }, { $set: { active: 0, banned: true } })
162+
console.log(`Suspended user "${username}" ${_id}`)
163+
return `Suspended user "${username}" ${_id}`
164+
} else {
165+
await utils.respawnUser(_id)
166+
// Remove the bot user from the database
167+
await db.users.removeWhere({ _id })
168+
await db['users.code'].removeWhere({ user: _id })
169+
await env.del(env.keys.MEMORY + _id)
170+
await env.del(env.keys.MEMORY_SEGMENTS + _id)
171+
console.log(`Removed user "${username}" ${_id}`)
172+
return `Removed user "${username}" ${_id}`
173+
}
174+
},
175+
async unbanUser (username) {
176+
const user = await db.users.findOne({ username: username })
177+
if (!user) {
178+
return `Can't find user "${username}"`
179+
} else if (user.active !== 0) {
180+
return `User "${username}" ${user._id} is not banned.`
181+
}
182+
183+
await db.users.update({ _id: user._id }, { $set: { active: 10000, banned: false } })
184+
console.log(`Unbanned user "${username}" ${user._id}`)
185+
return `Unbanned user "${username}" ${user._id}`
149186
}
150187
})
151188

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

161201
Object.defineProperty(config.utils, '_help', {
162202
get () { // Using a getter here so that loaded services are also included

lib/services/whitelist/cronjobs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = (config) => {
55
const WHITELIST = JSON.parse(await env.get(env.keys.WHITELIST) || '[]').map(u => u.toLowerCase())
66
const users = await db.users.find()
77
for (const user of users) {
8-
const blocked = !(user.allowed || WHITELIST.length === 0 || (user.username && WHITELIST.includes(user.username.toLowerCase())))
8+
const blocked = !(user.allowed || !user.banned || WHITELIST.length === 0 || (user.username && WHITELIST.includes(user.username.toLowerCase())))
99
db.users.update({ _id: user._id }, { $set: { blocked } })
1010
}
1111
}]

0 commit comments

Comments
 (0)