Implement community "create one for me"

This commit is contained in:
Madeline 2023-08-10 20:34:23 +10:00
parent 25099aecb7
commit 326bf08df0
No known key found for this signature in database
GPG Key ID: 1958E017C36F2E47
2 changed files with 86 additions and 12 deletions

View File

@ -18,11 +18,13 @@
import { route } from "@spacebar/api"; import { route } from "@spacebar/api";
import { import {
Channel,
DiscordApiErrors, DiscordApiErrors,
Guild, Guild,
GuildUpdateEvent, GuildUpdateEvent,
GuildUpdateSchema, GuildUpdateSchema,
Member, Member,
Permissions,
SpacebarApiErrors, SpacebarApiErrors,
emitEvent, emitEvent,
getPermission, getPermission,
@ -155,6 +157,76 @@ router.patch(
guild.features = body.features; 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 // TODO: check if body ids are valid
guild.assign(body); guild.assign(body);

View File

@ -16,6 +16,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { HTTPError } from "lambert-server";
import { import {
Column, Column,
Entity, Entity,
@ -24,26 +25,25 @@ import {
OneToMany, OneToMany,
RelationId, RelationId,
} from "typeorm"; } from "typeorm";
import { BaseClass } from "./BaseClass"; import { DmChannelDTO } from "../dtos";
import { Guild } from "./Guild"; import { ChannelCreateEvent, ChannelRecipientRemoveEvent } from "../interfaces";
import { PublicUserProjection, User } from "./User";
import { HTTPError } from "lambert-server";
import { import {
InvisibleCharacters,
Snowflake,
containsAll, containsAll,
emitEvent, emitEvent,
getPermission, getPermission,
Snowflake,
trimSpecial, trimSpecial,
InvisibleCharacters,
} from "../util"; } from "../util";
import { ChannelCreateEvent, ChannelRecipientRemoveEvent } from "../interfaces"; import { BaseClass } from "./BaseClass";
import { Recipient } from "./Recipient"; import { Guild } from "./Guild";
import { Invite } from "./Invite";
import { Message } from "./Message"; import { Message } from "./Message";
import { ReadState } from "./ReadState"; import { ReadState } from "./ReadState";
import { Invite } from "./Invite"; import { Recipient } from "./Recipient";
import { PublicUserProjection, User } from "./User";
import { VoiceState } from "./VoiceState"; import { VoiceState } from "./VoiceState";
import { Webhook } from "./Webhook"; import { Webhook } from "./Webhook";
import { DmChannelDTO } from "../dtos";
export enum ChannelType { export enum ChannelType {
GUILD_TEXT = 0, // a text channel within a guild GUILD_TEXT = 0, // a text channel within a guild
@ -302,8 +302,10 @@ export class Channel extends BaseClass {
: channel.position) || 0, : channel.position) || 0,
}; };
const ret = Channel.create(channel);
await Promise.all([ await Promise.all([
Channel.create(channel).save(), ret.save(),
!opts?.skipEventEmit !opts?.skipEventEmit
? emitEvent({ ? emitEvent({
event: "CHANNEL_CREATE", event: "CHANNEL_CREATE",
@ -313,7 +315,7 @@ export class Channel extends BaseClass {
: Promise.resolve(), : Promise.resolve(),
]); ]);
return channel; return ret;
} }
static async createDMChannel( static async createDMChannel(