✨ createChannel()
This commit is contained in:
		
							parent
							
								
									8a13a1f2ea
								
							
						
					
					
						commit
						4715ef1eab
					
				| @ -13,6 +13,7 @@ import { HTTPError } from "lambert-server"; | ||||
| import { ChannelModifySchema } from "../../../schema/Channel"; | ||||
| import { emitEvent } from "../../../util/Event"; | ||||
| import { check } from "../../../util/instanceOf"; | ||||
| import { createChannel } from "../../../util/Channel"; | ||||
| const router = Router(); | ||||
| 
 | ||||
| router.get("/", async (req, res) => { | ||||
| @ -26,41 +27,7 @@ router.post("/", check(ChannelModifySchema), async (req, res) => { | ||||
| 	const { guild_id } = req.params; | ||||
| 	const body = req.body as ChannelModifySchema; | ||||
| 
 | ||||
| 	if (!body.permission_overwrites) body.permission_overwrites = []; | ||||
| 	if (!body.topic) body.topic = ""; | ||||
| 	if (!body.rate_limit_per_user) body.rate_limit_per_user = 0; | ||||
| 
 | ||||
| 	switch (body.type) { | ||||
| 		case ChannelType.DM: | ||||
| 		case ChannelType.GROUP_DM: | ||||
| 			throw new HTTPError("You can't create a dm channel in a guild"); | ||||
| 		// TODO:
 | ||||
| 		case ChannelType.GUILD_STORE: | ||||
| 			throw new HTTPError("Not yet supported"); | ||||
| 		case ChannelType.GUILD_NEWS: | ||||
| 		// TODO: check if guild is community server
 | ||||
| 	} | ||||
| 
 | ||||
| 	if (body.parent_id) { | ||||
| 		const exists = await ChannelModel.findOne({ id: body.parent_id }, { guild_id: true }).exec(); | ||||
| 		if (!exists) throw new HTTPError("Parent id channel doesn't exist", 400); | ||||
| 		if (exists.guild_id !== guild_id) throw new HTTPError("The category channel needs to be in the guild"); | ||||
| 	} | ||||
| 
 | ||||
| 	const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); | ||||
| 	if (!guild) throw new HTTPError("Guild not found", 404); | ||||
| 
 | ||||
| 	const channel = { | ||||
| 		...body, | ||||
| 		id: Snowflake.generate(), | ||||
| 		created_at: new Date(), | ||||
| 		guild_id, | ||||
| 		recipients: null | ||||
| 	}; | ||||
| 
 | ||||
| 	await new ChannelModel(channel).save(); | ||||
| 
 | ||||
| 	await emitEvent({ event: "CHANNEL_CREATE", data: channel, guild_id } as ChannelCreateEvent); | ||||
| 	const channel = await createChannel({ ...body, guild_id }, req.user_id); | ||||
| 
 | ||||
| 	res.json(channel); | ||||
| }); | ||||
|  | ||||
| @ -54,15 +54,4 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) | ||||
| 	return res.send(data); | ||||
| }); | ||||
| 
 | ||||
| router.get("/vanity-url", async (req: Request, res: Response) => { | ||||
| 	const { guild_id } = req.params; | ||||
| 
 | ||||
| 	const guild = await GuildModel.findOne({ id: guild_id }).exec(); | ||||
| 	if (!guild) throw new HTTPError("Guild does not exist", 404); | ||||
| 
 | ||||
| 	if (!guild.vanity_url) throw new HTTPError("This guild has no vanity url", 204); | ||||
| 
 | ||||
| 	return res.json(guild.vanity_url); | ||||
| }); | ||||
| 
 | ||||
| export default router; | ||||
|  | ||||
| @ -6,6 +6,7 @@ import { GuildCreateSchema } from "../../schema/Guild"; | ||||
| import Config from "../../util/Config"; | ||||
| import { getPublicUser } from "../../util/User"; | ||||
| import { addMember } from "../../util/Member"; | ||||
| import { createChannel } from "../../util/Channel"; | ||||
| 
 | ||||
| const router: Router = Router(); | ||||
| 
 | ||||
| @ -80,7 +81,8 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) = | ||||
| 		}).save() | ||||
| 	]); | ||||
| 
 | ||||
| 	await addMember(req.user_id, guild_id, { guild: guild_doc }); | ||||
| 	await createChannel({ name: "general", type: 0, guild_id, position: 0, permission_overwrites: [] }, req.user_id); | ||||
| 	await addMember(req.user_id, guild_id); | ||||
| 
 | ||||
| 	res.status(201).json({ id: guild.id }); | ||||
| }); | ||||
|  | ||||
| @ -7,7 +7,7 @@ export const InviteCreateSchema = { | ||||
| 	$temporary: Boolean, | ||||
| 	$unique: Boolean, | ||||
| 	$target_user: String, | ||||
| 	$target_user_type: Number, | ||||
| 	$target_user_type: Number | ||||
| }; | ||||
| export interface InviteCreateSchema { | ||||
| 	target_user_id?: String; | ||||
|  | ||||
| @ -1,34 +1,17 @@ | ||||
| export const RoleCreateSchema = { | ||||
| 	name: String, | ||||
| 	permissions: BigInt, | ||||
| 	color: Number,  | ||||
| 	hoist: Boolean, // whether the role should be displayed separately in the sidebar
 | ||||
| 	mentionable: Boolean // whether the role should be mentionable
 | ||||
| }; | ||||
| 
 | ||||
| export interface RoleCreateSchema { | ||||
| 	name: string, | ||||
| 	permissions: BigInt, | ||||
| 	color: number, | ||||
| 	hoist: boolean, // whether the role should be displayed separately in the sidebar
 | ||||
| 	mentionable: boolean // whether the role should be mentionable
 | ||||
| } | ||||
| 
 | ||||
| export const RoleModifySchema = { | ||||
| 	$name: String, | ||||
| 	$permissions: BigInt, | ||||
| 	$color: Number, | ||||
| 	$hoist: Boolean, // whether the role should be displayed separately in the sidebar
 | ||||
| 	$mentionable: Boolean, // whether the role should be mentionable
 | ||||
| 	$position: Number, | ||||
| 
 | ||||
| 	$position: Number | ||||
| }; | ||||
| 
 | ||||
| export interface RoleModifySchema { | ||||
| 	name?: string, | ||||
| 	permissions?: BigInt, | ||||
| 	color?: number, | ||||
| 	hoist?: boolean, // whether the role should be displayed separately in the sidebar
 | ||||
| 	mentionable?: boolean, // whether the role should be mentionable
 | ||||
| 	position?: number, | ||||
| 	name?: string; | ||||
| 	permissions?: BigInt; | ||||
| 	color?: number; | ||||
| 	hoist?: boolean; // whether the role should be displayed separately in the sidebar
 | ||||
| 	mentionable?: boolean; // whether the role should be mentionable
 | ||||
| 	position?: number; | ||||
| } | ||||
|  | ||||
| @ -1,8 +1,3 @@ | ||||
| import { getPermission } from "@fosscord/server-util"; | ||||
| import { Snowflake } from "@fosscord/server-util"; | ||||
| 
 | ||||
| async function main() { | ||||
| 	const t = await getPermission("811642917432066048", "812327318532915201"); | ||||
| 	console.log(t); | ||||
| } | ||||
| 
 | ||||
| main(); | ||||
| console.log(Snowflake.deconstruct("0")); | ||||
|  | ||||
							
								
								
									
										54
									
								
								src/util/Channel.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								src/util/Channel.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,54 @@ | ||||
| import { | ||||
| 	ChannelCreateEvent, | ||||
| 	ChannelModel, | ||||
| 	ChannelType, | ||||
| 	getPermission, | ||||
| 	GuildModel, | ||||
| 	Snowflake, | ||||
| 	TextChannel, | ||||
| 	VoiceChannel | ||||
| } from "@fosscord/server-util"; | ||||
| import { HTTPError } from "lambert-server"; | ||||
| import { emitEvent } from "./Event"; | ||||
| 
 | ||||
| // TODO: DM channel
 | ||||
| export async function createChannel(channel: Partial<TextChannel | VoiceChannel>, user_id: string = "0") { | ||||
| 	if (!channel.permission_overwrites) channel.permission_overwrites = []; | ||||
| 
 | ||||
| 	switch (channel.type) { | ||||
| 		case ChannelType.GUILD_TEXT: | ||||
| 		case ChannelType.GUILD_VOICE: | ||||
| 			break; | ||||
| 		case ChannelType.DM: | ||||
| 		case ChannelType.GROUP_DM: | ||||
| 			throw new HTTPError("You can't create a dm channel in a guild"); | ||||
| 		// TODO: check if guild is community server
 | ||||
| 		case ChannelType.GUILD_STORE: | ||||
| 		case ChannelType.GUILD_NEWS: | ||||
| 		default: | ||||
| 			throw new HTTPError("Not yet supported"); | ||||
| 	} | ||||
| 
 | ||||
| 	const permissions = await getPermission(user_id, channel.guild_id); | ||||
| 	permissions.hasThrow("MANAGE_CHANNELS"); | ||||
| 
 | ||||
| 	if (channel.parent_id) { | ||||
| 		const exists = await ChannelModel.findOne({ id: channel.parent_id }, { guild_id: true }).exec(); | ||||
| 		if (!exists) throw new HTTPError("Parent id channel doesn't exist", 400); | ||||
| 		if (exists.guild_id !== channel.guild_id) throw new HTTPError("The category channel needs to be in the guild"); | ||||
| 	} | ||||
| 
 | ||||
| 	// TODO: auto generate position
 | ||||
| 
 | ||||
| 	channel = await new ChannelModel({ | ||||
| 		...channel, | ||||
| 		id: Snowflake.generate(), | ||||
| 		created_at: new Date(), | ||||
| 		// @ts-ignore
 | ||||
| 		recipients: null | ||||
| 	}).save(); | ||||
| 
 | ||||
| 	await emitEvent({ event: "CHANNEL_CREATE", data: channel, guild_id: channel.guild_id } as ChannelCreateEvent); | ||||
| 
 | ||||
| 	return channel; | ||||
| } | ||||
| @ -5,7 +5,8 @@ import { Tuple } from "lambert-server"; | ||||
| import "missing-native-js-functions"; | ||||
| 
 | ||||
| export const OPTIONAL_PREFIX = "$"; | ||||
| export const EMAIL_REGEX = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||||
| export const EMAIL_REGEX = | ||||
| 	/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||||
| 
 | ||||
| export function check(schema: any) { | ||||
| 	return (req: Request, res: Response, next: NextFunction) => { | ||||
| @ -27,9 +28,9 @@ export function FieldErrors(fields: Record<string, { code?: string; message: str | ||||
| 			_errors: [ | ||||
| 				{ | ||||
| 					message, | ||||
| 					code: code || "BASE_TYPE_INVALID", | ||||
| 				}, | ||||
| 			], | ||||
| 					code: code || "BASE_TYPE_INVALID" | ||||
| 				} | ||||
| 			] | ||||
| 		})) | ||||
| 	); | ||||
| } | ||||
| @ -68,7 +69,7 @@ export function instanceOf( | ||||
| 		optional = false, | ||||
| 		errors = {}, | ||||
| 		req, | ||||
| 		ref, | ||||
| 		ref | ||||
| 	}: { path?: string; optional?: boolean; errors?: any; req: Request; ref?: { key: string | number; obj: any } } | ||||
| ): Boolean { | ||||
| 	if (!ref) ref = { obj: null, key: "" }; | ||||
| @ -131,7 +132,7 @@ export function instanceOf( | ||||
| 								optional, | ||||
| 								errors: errors[i], | ||||
| 								req, | ||||
| 								ref: { key: i, obj: value }, | ||||
| 								ref: { key: i, obj: value } | ||||
| 							}) === true | ||||
| 						) { | ||||
| 							delete errors[i]; | ||||
| @ -153,7 +154,7 @@ export function instanceOf( | ||||
| 					throw new FieldError( | ||||
| 						"BASE_TYPE_BAD_LENGTH", | ||||
| 						req.t("common:field.BASE_TYPE_BAD_LENGTH", { | ||||
| 							length: `${type.min} - ${type.max}`, | ||||
| 							length: `${type.min} - ${type.max}` | ||||
| 						}) | ||||
| 					); | ||||
| 				} | ||||
| @ -185,7 +186,7 @@ export function instanceOf( | ||||
| 							optional: OPTIONAL, | ||||
| 							errors: errors[newKey], | ||||
| 							req, | ||||
| 							ref: { key: newKey, obj: value }, | ||||
| 							ref: { key: newKey, obj: value } | ||||
| 						}) === true | ||||
| 					) { | ||||
| 						delete errors[newKey]; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Flam3rboy
						Flam3rboy