Added ILLEGAL_CHANNEL_NAMES and NULL_CHANNEL_NAMES guild feature flags

This commit is contained in:
Madeline 2022-01-14 01:20:26 +11:00 committed by Erkin Alp Güney
parent f9ff5b35f3
commit 35c7489f72
3 changed files with 406 additions and 332 deletions

View File

@ -3,7 +3,7 @@ import { BaseClass } from "./BaseClass";
import { Guild } from "./Guild";
import { PublicUserProjection, User } from "./User";
import { HTTPError } from "lambert-server";
import { containsAll, emitEvent, getPermission, Snowflake, trimSpecial } from "../util";
import { containsAll, emitEvent, getPermission, Snowflake, trimSpecial, InvisibleCharacters } from "../util";
import { ChannelCreateEvent, ChannelRecipientRemoveEvent } from "../interfaces";
import { Recipient } from "./Recipient";
import { Message } from "./Message";
@ -147,6 +147,7 @@ export class Channel extends BaseClass {
skipExistsCheck?: boolean;
skipPermissionCheck?: boolean;
skipEventEmit?: boolean;
skipNameChecks?: boolean;
}
) {
if (!opts?.skipPermissionCheck) {
@ -155,6 +156,23 @@ export class Channel extends BaseClass {
permissions.hasThrow("MANAGE_CHANNELS");
}
if (!opts?.skipNameChecks) {
const guild = await Guild.findOneOrFail({ id: channel.guild_id });
if (!guild.features.includes("ILLEGAL_CHANNEL_NAMES") && channel.name) {
for (var character of InvisibleCharacters)
channel.name = channel.name.split(character).join("-");
channel.name = channel.name.split(/\-+/g).join("-"); //replace multiple occurances with just one
channel.name = channel.name.split("-").filter(Boolean).join("-"); //trim '-' character
}
if (!guild.features.includes("NULL_CHANNEL_NAMES")) {
if (channel.name) channel.name = channel.name.trim();
if (!channel.name) throw new HTTPError("Channel name cannot be empty.");
}
}
switch (channel.type) {
case ChannelType.GUILD_TEXT:
case ChannelType.GUILD_VOICE:
@ -191,10 +209,10 @@ export class Channel extends BaseClass {
new Channel(channel).save(),
!opts?.skipEventEmit
? emitEvent({
event: "CHANNEL_CREATE",
data: channel,
guild_id: channel.guild_id,
} as ChannelCreateEvent)
event: "CHANNEL_CREATE",
data: channel,
guild_id: channel.guild_id,
} as ChannelCreateEvent)
: Promise.resolve(),
]);

View File

@ -0,0 +1,55 @@
export const InvisibleCharacters = [
"\t",
" ",
"­",
"͏",
"؜",
"",
"",
"",
"",
"",
" ",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
" ",
"",
"",
"",
"",
"𝅙",
"𝅳",
"𝅴",
"𝅵",
"𝅶",
"𝅷",
"𝅸",
"𝅹",
"𝅺"
]

View File

@ -18,3 +18,4 @@ export * from "./Snowflake";
export * from "./String";
export * from "./Array";
export * from "./TraverseDirectory";
export * from "./InvisibleCharacters";