From 326bf08df071111d30af258eb1eb043e4b9433a0 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Thu, 10 Aug 2023 20:34:23 +1000 Subject: Implement community "create one for me" --- src/api/routes/guilds/#guild_id/index.ts | 72 ++++++++++++++++++++++++++++++++ src/util/entities/Channel.ts | 26 ++++++------ 2 files changed, 86 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/api/routes/guilds/#guild_id/index.ts b/src/api/routes/guilds/#guild_id/index.ts index 86777b36..6feb0a83 100644 --- a/src/api/routes/guilds/#guild_id/index.ts +++ b/src/api/routes/guilds/#guild_id/index.ts @@ -18,11 +18,13 @@ import { route } from "@spacebar/api"; import { + Channel, DiscordApiErrors, Guild, GuildUpdateEvent, GuildUpdateSchema, Member, + Permissions, SpacebarApiErrors, emitEvent, getPermission, @@ -155,6 +157,76 @@ router.patch( guild.features = body.features; } + if (body.public_updates_channel_id == "1") { + // move all channels up 1 + await Channel.createQueryBuilder("channels") + .where({ guild: { id: guild_id } }) + .update({ position: () => "position + 1" }) + .execute(); + + // create an updates channel for them + await Channel.createChannel( + { + name: "moderator-only", + guild_id: guild.id, + position: 0, + type: 0, + permission_overwrites: [ + // remove SEND_MESSAGES from @everyone + { + id: guild.id, + allow: "0", + deny: Permissions.FLAGS.VIEW_CHANNEL.toString(), + type: 0, + }, + ], + }, + undefined, + { skipPermissionCheck: true }, + ); + } else if (body.public_updates_channel_id != undefined) { + // ensure channel exists in this guild + await Channel.findOneOrFail({ + where: { guild_id, id: body.public_updates_channel_id }, + select: { id: true }, + }); + } + + if (body.rules_channel_id == "1") { + // move all channels up 1 + await Channel.createQueryBuilder("channels") + .where({ guild: { id: guild_id } }) + .update({ position: () => "position + 1" }) + .execute(); + + // create a rules for them + await Channel.createChannel( + { + name: "rules", + guild_id: guild.id, + position: 0, + type: 0, + permission_overwrites: [ + // remove SEND_MESSAGES from @everyone + { + id: guild.id, + allow: "0", + deny: Permissions.FLAGS.SEND_MESSAGES.toString(), + type: 0, + }, + ], + }, + undefined, + { skipPermissionCheck: true }, + ); + } else if (body.rules_channel_id != undefined) { + // ensure channel exists in this guild + await Channel.findOneOrFail({ + where: { guild_id, id: body.rules_channel_id }, + select: { id: true }, + }); + } + // TODO: check if body ids are valid guild.assign(body); diff --git a/src/util/entities/Channel.ts b/src/util/entities/Channel.ts index 38627c39..19952bc2 100644 --- a/src/util/entities/Channel.ts +++ b/src/util/entities/Channel.ts @@ -16,6 +16,7 @@ along with this program. If not, see . */ +import { HTTPError } from "lambert-server"; import { Column, Entity, @@ -24,26 +25,25 @@ import { OneToMany, RelationId, } from "typeorm"; -import { BaseClass } from "./BaseClass"; -import { Guild } from "./Guild"; -import { PublicUserProjection, User } from "./User"; -import { HTTPError } from "lambert-server"; +import { DmChannelDTO } from "../dtos"; +import { ChannelCreateEvent, ChannelRecipientRemoveEvent } from "../interfaces"; import { + InvisibleCharacters, + Snowflake, containsAll, emitEvent, getPermission, - Snowflake, trimSpecial, - InvisibleCharacters, } from "../util"; -import { ChannelCreateEvent, ChannelRecipientRemoveEvent } from "../interfaces"; -import { Recipient } from "./Recipient"; +import { BaseClass } from "./BaseClass"; +import { Guild } from "./Guild"; +import { Invite } from "./Invite"; import { Message } from "./Message"; import { ReadState } from "./ReadState"; -import { Invite } from "./Invite"; +import { Recipient } from "./Recipient"; +import { PublicUserProjection, User } from "./User"; import { VoiceState } from "./VoiceState"; import { Webhook } from "./Webhook"; -import { DmChannelDTO } from "../dtos"; export enum ChannelType { GUILD_TEXT = 0, // a text channel within a guild @@ -302,8 +302,10 @@ export class Channel extends BaseClass { : channel.position) || 0, }; + const ret = Channel.create(channel); + await Promise.all([ - Channel.create(channel).save(), + ret.save(), !opts?.skipEventEmit ? emitEvent({ event: "CHANNEL_CREATE", @@ -313,7 +315,7 @@ export class Channel extends BaseClass { : Promise.resolve(), ]); - return channel; + return ret; } static async createDMChannel( -- cgit 1.4.1