refactor whitelist
This commit is contained in:
		
							parent
							
								
									157f64d2ae
								
							
						
					
					
						commit
						f16563c301
					
				
					 3 changed files with 88 additions and 5 deletions
				
			
		
							
								
								
									
										20
									
								
								bot.js
									
										
									
									
									
								
							
							
						
						
									
										20
									
								
								bot.js
									
										
									
									
									
								
							|  | @ -9,6 +9,11 @@ import path from 'path'; | |||
| import fs from 'fs'; | ||||
| 
 | ||||
| const cfg = require('./config.json'); | ||||
| if (!fs.existsSync('./whitelist.json')) { | ||||
| 	fs.writeFileSync('./whitelist.json', '{}'); | ||||
| } | ||||
| 
 | ||||
| const whitelist = require('./whitelist.json'); | ||||
| const dir = dirname(import.meta.url); | ||||
| 
 | ||||
| const bot = new Eris(cfg.token); | ||||
|  | @ -24,23 +29,30 @@ const log = new Logger(filename(import.meta.url), global.ctx.log_level); | |||
| const parse = new CommandParser(global.ctx, cfg.prefix || undefined); | ||||
| 
 | ||||
| const checkUser = (user) => { | ||||
| 	if (cfg.user_whitelist.includes(user.id)) { | ||||
| 	if (cfg.user_whitelist.includes(user.id) || (whitelist.user && whitelist.user.includes(user.id))) { | ||||
| 		return true; | ||||
| 	} | ||||
| 	log.info(`user not on whitelist: ${user.username}`); | ||||
| 	return false; | ||||
| }; | ||||
| const checkGuild = (guild) => { | ||||
| 	if (cfg.whitelist.includes(guild.id)) { | ||||
| 	if (cfg.whitelist.includes(guild.id) || (whitelist.guild && whitelist.guild.includes(guild.id))) { | ||||
| 		return true; | ||||
| 	} | ||||
| 	log.info(`guild not on whitelist: ${guild.name}`); | ||||
| 	return false; | ||||
| }; | ||||
| 
 | ||||
| const saveWhitelist = () => { | ||||
| 	let data = JSON.stringify(whitelist); | ||||
| 	fs.writeFileSync('./whitelist.json', data); | ||||
| }; | ||||
| 
 | ||||
| global.ctx.whitelist = { | ||||
| 	guild: checkGuild, | ||||
| 	user: checkUser, | ||||
| 	save: saveWhitelist, | ||||
| 	wl: whitelist, | ||||
| }; | ||||
| 
 | ||||
| bot.on('ready', () => { | ||||
|  | @ -48,8 +60,6 @@ bot.on('ready', () => { | |||
| 	bot.guilds.forEach((guild) => checkGuild(guild)); | ||||
| }); | ||||
| 
 | ||||
| bot.on('guildAvaliable', (guild) => checkGuild(guild)); | ||||
| 
 | ||||
| bot.on('messageCreate', (msg) => parse.parseMsg(msg, global.ctx)); | ||||
| 
 | ||||
| bot.on('error', (err) => log.error(err.toString())); | ||||
|  | @ -64,7 +74,7 @@ async function load_commands() { | |||
| 		try { | ||||
| 			obj = await import('file:////' + p); | ||||
| 		} catch (e) { | ||||
| 			log.warn(`loading file ${file}, ran into issue: ${e.message}`); | ||||
| 			log.warn(`loading file ${file}, ran into issue: ${e.stack}`); | ||||
| 			continue; | ||||
| 		} | ||||
| 		if (obj.default != undefined) { | ||||
|  |  | |||
							
								
								
									
										72
									
								
								cmd/perm.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								cmd/perm.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,72 @@ | |||
| import { CommandInitializer, Command } from '../parser.js'; | ||||
| 
 | ||||
| const initializer = new CommandInitializer(); | ||||
| 
 | ||||
| class WhitelistUser extends Command { | ||||
| 	name = 'wu'; | ||||
| 	whitelist = true; | ||||
| 	func = async function (msg, args, ctx) { | ||||
| 		let user = await ctx.bot.users.get(args[0].trim()); | ||||
| 		this.log.debug(msg.channel.guild.members); | ||||
| 		this.log.debug(user); | ||||
| 		if (!user) { | ||||
| 			user = msg.channel.guild.members.get(args[0].trim()); | ||||
| 			this.log.debug(user); | ||||
| 			if (!user) { | ||||
| 				user = (await msg.channel.guild.fetchMembers({ userIDs: [args[0]] }))[0]; | ||||
| 				this.log.debug(user); | ||||
| 			} | ||||
| 		} | ||||
| 		if (user.username) { | ||||
| 			if (!ctx.whitelist.wl) { | ||||
| 				ctx.whitelist.wl = { | ||||
| 					user: [], | ||||
| 					guild: [], | ||||
| 				}; | ||||
| 			} | ||||
| 			let list = ctx.whitelist.wl.user || []; | ||||
| 			if (!list.includes(args[0])) { | ||||
| 				list.push(args[0]); | ||||
| 				ctx.whitelist.wl.user = list; | ||||
| 				ctx.whitelist.save(); | ||||
| 				msg.channel.createMessage(`added user "${user.username}#${user.discriminator}" (${args[0]}) to whitelist`); | ||||
| 			} else { | ||||
| 				msg.channel.createMessage('user already whitelisted'); | ||||
| 			} | ||||
| 		} else { | ||||
| 			msg.channel.createMessage(`user with id ${args[0]} could not be found`); | ||||
| 		} | ||||
| 	}; | ||||
| } | ||||
| initializer.addCommand(new WhitelistUser()); | ||||
| 
 | ||||
| class WhitelistGuild extends Command { | ||||
| 	name = 'wg'; | ||||
| 	whitelist = true; | ||||
| 	func = async function (msg, args, ctx) { | ||||
| 		let guild = await ctx.bot.guilds.get(args[0]); | ||||
| 		if (guild.name) { | ||||
| 			if (!ctx.whitelist.wl) { | ||||
| 				ctx.whitelist.wl = { | ||||
| 					user: [], | ||||
| 					guild: [], | ||||
| 				}; | ||||
| 			} | ||||
| 			let list = ctx.whitelist.wl.guild || []; | ||||
| 			if (!list.includes(args[0])) { | ||||
| 				list.push(args[0]); | ||||
| 				ctx.whitelist.wl.guild = list; | ||||
| 				ctx.whitelist.save(); | ||||
| 				msg.channel.createMessage(`added guild "${guild.name}" (${args[0]}) to whitelist`); | ||||
| 			} else { | ||||
| 				msg.channel.createMessage('guild already whitelisted'); | ||||
| 			} | ||||
| 		} else { | ||||
| 			msg.channel.createMessage(`guild with id ${args[0]} could not be found`); | ||||
| 		} | ||||
| 	}; | ||||
| } | ||||
| 
 | ||||
| initializer.addCommand(new WhitelistGuild()); | ||||
| 
 | ||||
| export default initializer; | ||||
							
								
								
									
										1
									
								
								whitelist.json
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								whitelist.json
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1 @@ | |||
| {"guild":["656579275008245830","754155880173404260"],"user":["123601647258697730","240240073386229760","285424490916216832","529534860243632134"]} | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue