pummmp.fun/scripts/set-role.ts
2026-07-04 12:49:09 -07:00

80 lines
2.1 KiB
TypeScript

import { MongoClient } from 'mongodb'
import * as dotenv from 'dotenv'
dotenv.config()
const MONGODB_URI = process.env.MONGODB_URI
if (!MONGODB_URI) {
console.error('Please define the MONGODB_URI environment variable in .env')
process.exit(1)
}
const roleMap: Record<string, string> = {
'admin': 'isAdmin',
'beta': 'isBetaTester',
'bug': 'isBugHunter',
'verified': 'verified'
}
async function setRole(usernameOrId: string, role: string, value: boolean) {
let client: MongoClient | null = null;
const dbField = roleMap[role.toLowerCase()]
if (!dbField) {
console.error(`Invalid role: ${role}. Valid roles: admin, beta, bug, verified`)
return
}
try {
client = await MongoClient.connect(MONGODB_URI!)
const db = client.db('pummmpfun')
console.log(`Searching for user: ${usernameOrId}...`)
let query: any = { name: usernameOrId }
if (/^\d{17,19}$/.test(usernameOrId)) {
query = { discordId: usernameOrId }
} else if (usernameOrId.includes('@')) {
// maybe email?
query = { email: usernameOrId }
}
const user = await db.collection('users').findOne(query)
if (!user) {
console.error(`User not found using query:`, query)
return
}
console.log(`Found user: ${user.name} (${user._id})`)
console.log(`Current ${role} status: ${user[dbField] || false}`)
console.log(`Setting ${role} (${dbField}) to: ${value}`)
const result = await db.collection('users').updateOne(
{ _id: user._id },
{ $set: { [dbField]: value } }
)
console.log(`Modified count: ${result.modifiedCount}`)
} catch (error) {
console.error('Error:', error)
} finally {
if (client) await client.close()
}
}
const args = process.argv.slice(2)
if (args.length < 2) {
console.log('Usage: npx ts-node scripts/set-role.ts <username_or_discord_id> <role> [true/false]')
console.log('Roles: admin, beta, bug, verified')
process.exit(1)
}
const targetUser = args[0]
const targetRole = args[1]
const targetValue = args[2] === 'false' ? false : true
setRole(targetUser, targetRole, targetValue)