⚡ improve invite generation
This commit is contained in:
		
							parent
							
								
									b86f99c4a5
								
							
						
					
					
						commit
						0be8c804b4
					
				| @ -6,26 +6,42 @@ import { random } from "../../../util/RandomInviteID"; | ||||
| 
 | ||||
| import { InviteCreateSchema } from "../../../schema/Invite"; | ||||
| 
 | ||||
| import { getPermission, Channel, Invite, InviteCreateEvent, emitEvent } from "@fosscord/util"; | ||||
| import { getPermission, Channel, Invite, InviteCreateEvent, emitEvent, User, Guild } from "@fosscord/util"; | ||||
| import { isTextChannel } from "./messages"; | ||||
| 
 | ||||
| const router: Router = Router(); | ||||
| 
 | ||||
| router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) => { | ||||
| 	const { user_id } = req; | ||||
| 	const { channel_id } = req.params; | ||||
| 	const channel = await Channel.findOneOrFail({ id: channel_id }); | ||||
| 	const channel = await Channel.findOneOrFail({ where: { id: channel_id }, select: ["id", "name", "type", "guild_id"] }); | ||||
| 	isTextChannel(channel.type); | ||||
| 
 | ||||
| 	if (!channel.guild_id) { | ||||
| 		throw new HTTPError("This channel doesn't exist", 404); | ||||
| 	} | ||||
| 	const { guild_id } = channel; | ||||
| 
 | ||||
| 	const permission = await getPermission(user_id, guild_id); | ||||
| 	const permission = await getPermission(user_id, guild_id, undefined, { | ||||
| 		guild_select: [ | ||||
| 			"banner", | ||||
| 			"description", | ||||
| 			"features", | ||||
| 			"icon", | ||||
| 			"id", | ||||
| 			"name", | ||||
| 			"nsfw", | ||||
| 			"nsfw_level", | ||||
| 			"splash", | ||||
| 			"vanity_url_code", | ||||
| 			"verification_level" | ||||
| 		] as (keyof Guild)[] | ||||
| 	}); | ||||
| 	permission.hasThrow("CREATE_INSTANT_INVITE"); | ||||
| 
 | ||||
| 	const expires_at = new Date(req.body.max_age * 1000 + Date.now()); | ||||
| 
 | ||||
| 	const invite = { | ||||
| 	const invite = await new Invite({ | ||||
| 		code: random(), | ||||
| 		temporary: req.body.temporary, | ||||
| 		uses: 0, | ||||
| @ -36,12 +52,14 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) | ||||
| 		guild_id, | ||||
| 		channel_id: channel_id, | ||||
| 		inviter_id: user_id | ||||
| 	}; | ||||
| 	}).save(); | ||||
| 	const data = invite.toJSON(); | ||||
| 	data.inviter = await User.getPublicUser(req.user_id); | ||||
| 	data.guild = permission.cache.guild; | ||||
| 	data.channel = channel; | ||||
| 
 | ||||
| 	await new Invite(invite).save(); | ||||
| 
 | ||||
| 	await emitEvent({ event: "INVITE_CREATE", data: invite, guild_id } as InviteCreateEvent); | ||||
| 	res.status(201).send(invite); | ||||
| 	await emitEvent({ event: "INVITE_CREATE", data, guild_id } as InviteCreateEvent); | ||||
| 	res.status(201).send(data); | ||||
| }); | ||||
| 
 | ||||
| router.get("/", async (req: Request, res: Response) => { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Flam3rboy
						Flam3rboy