🐛 fix Guild + Channel create

This commit is contained in:
Flam3rboy 2021-08-14 13:56:46 +02:00
parent 6db7620582
commit 96080e8786
3 changed files with 15 additions and 5 deletions

View File

@ -18,6 +18,7 @@ export const ChannelModifySchema = {
} }
], ],
$parent_id: String, $parent_id: String,
$id: String, // kept for backwards compatibility does nothing (need for guild create)
$nsfw: Boolean $nsfw: Boolean
}; };
@ -46,6 +47,7 @@ export interface ChannelModifySchema {
deny: bigint; deny: bigint;
}[]; }[];
parent_id?: string; parent_id?: string;
id?: string; // is not used (only for guild create)
nsfw?: boolean; nsfw?: boolean;
} }

View File

@ -1,11 +1,12 @@
import { ChannelSchema, GuildChannel } from "@fosscord/util"; import { ChannelSchema, GuildChannel } from "@fosscord/util";
import { Length } from "../util/instanceOf"; import { Length } from "../util/instanceOf";
import { ChannelModifySchema } from "./Channel";
export const GuildCreateSchema = { export const GuildCreateSchema = {
name: new Length(String, 2, 100), name: new Length(String, 2, 100),
$region: String, // auto complete voice region of the user $region: String, // auto complete voice region of the user
$icon: String, $icon: String,
$channels: [Object], $channels: [ChannelModifySchema],
$guild_template_code: String, $guild_template_code: String,
$system_channel_id: String, $system_channel_id: String,
$rules_channel_id: String $rules_channel_id: String
@ -15,7 +16,7 @@ export interface GuildCreateSchema {
name: string; name: string;
region?: string; region?: string;
icon?: string; icon?: string;
channels?: GuildChannel[]; channels?: ChannelModifySchema[];
guild_template_code?: string; guild_template_code?: string;
system_channel_id?: string; system_channel_id?: string;
rules_channel_id?: string; rules_channel_id?: string;

View File

@ -13,7 +13,14 @@ import {
import { HTTPError } from "lambert-server"; import { HTTPError } from "lambert-server";
// TODO: DM channel // TODO: DM channel
export async function createChannel(channel: Partial<TextChannel | VoiceChannel>, user_id: string = "0") { export async function createChannel(
channel: Partial<TextChannel | VoiceChannel>,
user_id: string = "0",
opts?: {
keepId?: boolean;
skipExistsCheck?: boolean;
}
) {
// Always check if user has permission first // Always check if user has permission first
const permissions = await getPermission(user_id, channel.guild_id); const permissions = await getPermission(user_id, channel.guild_id);
permissions.hasThrow("MANAGE_CHANNELS"); permissions.hasThrow("MANAGE_CHANNELS");
@ -21,7 +28,7 @@ export async function createChannel(channel: Partial<TextChannel | VoiceChannel>
switch (channel.type) { switch (channel.type) {
case ChannelType.GUILD_TEXT: case ChannelType.GUILD_TEXT:
case ChannelType.GUILD_VOICE: case ChannelType.GUILD_VOICE:
if (channel.parent_id) { if (channel.parent_id && !opts?.skipExistsCheck) {
const exists = await ChannelModel.findOne({ id: channel.parent_id }, { guild_id: true }).exec(); 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) 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"); if (exists.guild_id !== channel.guild_id) throw new HTTPError("The category channel needs to be in the guild");
@ -44,7 +51,7 @@ export async function createChannel(channel: Partial<TextChannel | VoiceChannel>
channel = await new ChannelModel({ channel = await new ChannelModel({
...channel, ...channel,
id: Snowflake.generate(), ...(!opts?.keepId && { id: Snowflake.generate() }),
created_at: new Date(), created_at: new Date(),
// @ts-ignore // @ts-ignore
recipient_ids: null recipient_ids: null